Search in sources :

Example 1 with ApplicationEventMulticaster

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 + "]");
        }
    }
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 2 with 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();
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) QueueChannel(org.springframework.integration.channel.QueueChannel) ApplicationEvent(org.springframework.context.ApplicationEvent) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Example 3 with ApplicationEventMulticaster

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
        }
    }
}
Also used : ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) ApplicationListener(org.springframework.context.ApplicationListener)

Example 4 with ApplicationEventMulticaster

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();
        }
    }
}
Also used : ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

ApplicationEventMulticaster (org.springframework.context.event.ApplicationEventMulticaster)4 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)2 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ApplicationListener (org.springframework.context.ApplicationListener)1 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1