Search in sources :

Example 6 with SimpleApplicationEventMulticaster

use of org.springframework.context.event.SimpleApplicationEventMulticaster in project SpringBoot-Learning by LuoLiangDSGA.

the class AsynchronousSpringEventConfig method simpleApplicationEventMulticaster.

@Bean(name = "applicationEventMulticaster")
public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
    SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
    eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
    return eventMulticaster;
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) Bean(org.springframework.context.annotation.Bean)

Example 7 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
    assertThat(message1).isNotNull();
    assertThat(message1.getPayload().toString().contains("org.springframework.integration.test.util.TestUtils$TestApplicationContext")).isTrue();
    adapter.onApplicationEvent(new TestApplicationEvent1());
    adapter.onApplicationEvent(new TestApplicationEvent2());
    Message<?> message2 = channel.receive(20);
    assertThat(message2).isNotNull();
    assertThat(message2.getPayload()).isEqualTo("received: event1");
    Message<?> message3 = channel.receive(20);
    assertThat(message3).isNotNull();
    assertThat(message3.getPayload()).isEqualTo("received: event2");
    ctx.close();
}
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 8 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")) {
        assertThat(taskExecutor.getClass()).isEqualTo(SyncTaskExecutor.class);
    } else {
        assertThat(taskExecutor).isNull();
    }
    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 9 with SimpleApplicationEventMulticaster

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

the class DelegatingApplicationListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        List<ApplicationListener<ApplicationEvent>> delegates = getListeners(((ApplicationEnvironmentPreparedEvent) event).getEnvironment());
        if (delegates.isEmpty()) {
            return;
        }
        this.multicaster = new SimpleApplicationEventMulticaster();
        for (ApplicationListener<ApplicationEvent> listener : delegates) {
            this.multicaster.addApplicationListener(listener);
        }
    }
    if (this.multicaster != null) {
        this.multicaster.multicastEvent(event);
    }
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationEvent(org.springframework.context.ApplicationEvent) ApplicationEnvironmentPreparedEvent(org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent)

Example 10 with SimpleApplicationEventMulticaster

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

the class CommonServerConfig method applicationEventMulticaster.

/**
     * 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 applicationEventMulticaster(final TaskExecutor taskExecutor) {
    final SimpleApplicationEventMulticaster applicationEventMulticaster = new SimpleApplicationEventMulticaster();
    applicationEventMulticaster.setTaskExecutor(taskExecutor);
    return applicationEventMulticaster;
}
Also used : SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) Bean(org.springframework.context.annotation.Bean)

Aggregations

SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)15 Bean (org.springframework.context.annotation.Bean)7 Test (org.junit.Test)4 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)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 QueueChannel (org.springframework.integration.channel.QueueChannel)1 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)1