use of org.springframework.context.event.ApplicationEventMulticaster in project spring-framework by spring-projects.
the class AbstractApplicationContext method initApplicationEventMulticaster.
/**
* Initialize the ApplicationEventMulticaster.
* Uses SimpleApplicationEventMulticaster if none defined in the context.
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster = beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
} else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ApplicationEventMulticaster with name '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "': using default [" + this.applicationEventMulticaster + "]");
}
}
}
use of org.springframework.context.event.ApplicationEventMulticaster in project spring-integration by spring-projects.
the class ApplicationEventListeningMessageProducerTests method testInt2935CheckRetrieverCache.
@Test
@SuppressWarnings({ "unchecked", "serial" })
public void testInt2935CheckRetrieverCache() {
GenericApplicationContext ctx = TestUtils.createTestApplicationContext();
ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
QueueChannel channel = new QueueChannel();
ApplicationEventListeningMessageProducer listenerMessageProducer = new ApplicationEventListeningMessageProducer();
listenerMessageProducer.setOutputChannel(channel);
listenerMessageProducer.setEventTypes(TestApplicationEvent2.class);
beanFactory.registerSingleton("testListenerMessageProducer", listenerMessageProducer);
AtomicInteger listenerCounter = new AtomicInteger();
beanFactory.registerSingleton("testListener", new TestApplicationListener(listenerCounter));
ctx.refresh();
ApplicationEventMulticaster multicaster = ctx.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
Map<?, ?> retrieverCache = TestUtils.getPropertyValue(multicaster, "retrieverCache", Map.class);
ctx.publishEvent(new TestApplicationEvent1());
/*
* Previously, the retrieverCache grew unnecessarily; the adapter was added to the cache for each event type,
* event if not supported.
*/
assertEquals(2, retrieverCache.size());
for (Map.Entry<?, ?> entry : retrieverCache.entrySet()) {
Class<? extends ApplicationEvent> event = TestUtils.getPropertyValue(entry.getKey(), "eventType.resolved", Class.class);
assertThat(event, Matchers.is(Matchers.isOneOf(ContextRefreshedEvent.class, TestApplicationEvent1.class)));
Set<?> listeners = TestUtils.getPropertyValue(entry.getValue(), "applicationListenerBeans", Set.class);
assertEquals(1, listeners.size());
assertEquals("testListener", listeners.iterator().next());
}
TestApplicationEvent2 event2 = new TestApplicationEvent2();
ctx.publishEvent(event2);
assertEquals(3, retrieverCache.size());
for (Map.Entry<?, ?> entry : retrieverCache.entrySet()) {
Class<?> event = TestUtils.getPropertyValue(entry.getKey(), "eventType.resolved", Class.class);
if (TestApplicationEvent2.class.isAssignableFrom(event)) {
Set<?> listeners = TestUtils.getPropertyValue(entry.getValue(), "applicationListenerBeans", Set.class);
assertEquals(2, listeners.size());
for (Object listener : listeners) {
assertThat((String) listener, Matchers.is(Matchers.isOneOf("testListenerMessageProducer", "testListener")));
}
break;
}
}
ctx.publishEvent(new ApplicationEvent("Some event") {
});
assertEquals(4, listenerCounter.get());
final Message<?> receive = channel.receive(10);
assertNotNull(receive);
assertSame(event2, receive.getPayload());
assertNull(channel.receive(1));
ctx.close();
}
use of org.springframework.context.event.ApplicationEventMulticaster in project spring-framework by spring-projects.
the class ApplicationListenerDetector method postProcessBeforeDestruction.
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) {
if (bean instanceof ApplicationListener) {
try {
ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
multicaster.removeApplicationListener((ApplicationListener<?>) bean);
multicaster.removeApplicationListenerBean(beanName);
} catch (IllegalStateException ex) {
// ApplicationEventMulticaster not initialized yet - no need to remove a listener
}
}
}
use of org.springframework.context.event.ApplicationEventMulticaster in project com.revolsys.open by revolsys.
the class DispatcherServlet method destroy.
@Override
public void destroy() {
super.destroy();
final WebApplicationContext webApplicationContext = getWebApplicationContext();
if (webApplicationContext instanceof AbstractApplicationContext) {
final AbstractApplicationContext cwac = (AbstractApplicationContext) webApplicationContext;
cwac.getApplicationListeners().clear();
if (cwac.isActive()) {
final ApplicationEventMulticaster eventMultiCaster = cwac.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
eventMultiCaster.removeAllListeners();
}
}
}
Aggregations