Search in sources :

Example 1 with SimpleApplicationEventMulticaster

use of org.springframework.context.event.SimpleApplicationEventMulticaster 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 SimpleApplicationEventMulticaster

use of org.springframework.context.event.SimpleApplicationEventMulticaster in project metacat by Netflix.

the class CommonServerConfig method asyncEventMulticaster.

/**
 * A multicast (async) event publisher to replace the synchronous one used by Spring via the ApplicationContext.
 *
 * @param taskExecutor The task executor to use
 * @return The application event multicaster to use
 */
@Bean
public ApplicationEventMulticaster asyncEventMulticaster(final TaskExecutor taskExecutor) {
    final SimpleApplicationEventMulticaster applicationEventMulticaster = new SimpleApplicationEventMulticaster();
    applicationEventMulticaster.setTaskExecutor(taskExecutor);
    return applicationEventMulticaster;
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with SimpleApplicationEventMulticaster

use of org.springframework.context.event.SimpleApplicationEventMulticaster in project spring-integration by spring-projects.

the class MessageBusParserTests method testMulticasterIsSyncByDefault.

@Test
public void testMulticasterIsSyncByDefault() {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("messageBusWithDefaults.xml", this.getClass());
    SimpleApplicationEventMulticaster multicaster = (SimpleApplicationEventMulticaster) context.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME);
    DirectFieldAccessor accessor = new DirectFieldAccessor(multicaster);
    Object taskExecutor = accessor.getPropertyValue("taskExecutor");
    if (SpringVersion.getVersion().startsWith("2")) {
        assertEquals(SyncTaskExecutor.class, taskExecutor.getClass());
    } else {
        assertNull(taskExecutor);
    }
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 4 with SimpleApplicationEventMulticaster

use of org.springframework.context.event.SimpleApplicationEventMulticaster in project spring-integration by spring-projects.

the class ApplicationEventListeningMessageProducerTests method payloadExpressionEvaluatedAgainstApplicationEvent.

@Test
public void payloadExpressionEvaluatedAgainstApplicationEvent() {
    QueueChannel channel = new QueueChannel();
    ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer();
    adapter.setPayloadExpression(PARSER.parseExpression("'received: ' + source"));
    adapter.setOutputChannel(channel);
    GenericApplicationContext ctx = TestUtils.createTestApplicationContext();
    ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
    beanFactory.registerSingleton(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, new SimpleApplicationEventMulticaster(beanFactory));
    adapter.setBeanFactory(beanFactory);
    beanFactory.registerSingleton("testListenerMessageProducer", adapter);
    adapter.afterPropertiesSet();
    ctx.refresh();
    Message<?> message1 = channel.receive(0);
    // ContextRefreshedEvent
    assertNotNull(message1);
    assertTrue(message1.getPayload().toString().contains("org.springframework.integration.test.util.TestUtils$TestApplicationContext"));
    adapter.onApplicationEvent(new TestApplicationEvent1());
    adapter.onApplicationEvent(new TestApplicationEvent2());
    Message<?> message2 = channel.receive(20);
    assertNotNull(message2);
    assertEquals("received: event1", message2.getPayload());
    Message<?> message3 = channel.receive(20);
    assertNotNull(message3);
    assertEquals("received: event2", message3.getPayload());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) QueueChannel(org.springframework.integration.channel.QueueChannel) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.Test)

Example 5 with SimpleApplicationEventMulticaster

use of org.springframework.context.event.SimpleApplicationEventMulticaster in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method multicastEvent.

private void multicastEvent(ApplicationListener<?> listener, ApplicationEvent event) {
    SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
    multicaster.addApplicationListener(listener);
    multicaster.multicastEvent(event);
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster)

Aggregations

SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)11 Test (org.junit.Test)4 Bean (org.springframework.context.annotation.Bean)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 ApplicationEnvironmentPreparedEvent (org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ApplicationListener (org.springframework.context.ApplicationListener)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ApplicationEventMulticaster (org.springframework.context.event.ApplicationEventMulticaster)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1