Search in sources :

Example 16 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project tomee by apache.

the class SpringBus method setApplicationContext.

/**
 * {@inheritDoc}
 */
public void setApplicationContext(ApplicationContext applicationContext) {
    ctx = (AbstractApplicationContext) applicationContext;
    @SuppressWarnings("rawtypes") ApplicationListener listener = new ApplicationListener() {

        public void onApplicationEvent(ApplicationEvent event) {
            SpringBus.this.onApplicationEvent(event);
        }
    };
    ctx.addApplicationListener(listener);
    ApplicationContext ac = applicationContext.getParent();
    while (ac != null) {
        if (ac instanceof AbstractApplicationContext) {
            ((AbstractApplicationContext) ac).addApplicationListener(listener);
        }
        ac = ac.getParent();
    }
    // set the classLoader extension with the application context classLoader
    setExtension(applicationContext.getClassLoader(), ClassLoader.class);
    setExtension(new ConfigurerImpl(applicationContext), Configurer.class);
    ResourceManager m = getExtension(ResourceManager.class);
    m.addResourceResolver(new BusApplicationContextResourceResolver(applicationContext));
    setExtension(applicationContext, ApplicationContext.class);
    ConfiguredBeanLocator loc = getExtension(ConfiguredBeanLocator.class);
    if (!(loc instanceof SpringBeanLocator)) {
        setExtension(new SpringBeanLocator(applicationContext, this), ConfiguredBeanLocator.class);
    }
    if (getState() != BusState.RUNNING) {
        initialize();
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ConfiguredBeanLocator(org.apache.cxf.configuration.ConfiguredBeanLocator) ConfigurerImpl(org.apache.cxf.configuration.spring.ConfigurerImpl) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationEvent(org.springframework.context.ApplicationEvent) ResourceManager(org.apache.cxf.resource.ResourceManager)

Example 17 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class EventPublicationInterceptor method invoke.

@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object retVal = invocation.proceed();
    Assert.state(this.applicationEventClassConstructor != null, "No ApplicationEvent class set");
    ApplicationEvent event = (ApplicationEvent) this.applicationEventClassConstructor.newInstance(invocation.getThis());
    Assert.state(this.applicationEventPublisher != null, "No ApplicationEventPublisher available");
    this.applicationEventPublisher.publishEvent(event);
    return retVal;
}
Also used : ApplicationEvent(org.springframework.context.ApplicationEvent) Nullable(org.springframework.lang.Nullable)

Example 18 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithTaskExecutor.

@Test
public void simpleApplicationEventMulticasterWithTaskExecutor() {
    @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
    ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
    SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
    smc.setTaskExecutor(command -> {
        command.run();
        command.run();
    });
    smc.addApplicationListener(listener);
    smc.multicastEvent(evt);
    verify(listener, times(2)).onApplicationEvent(evt);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 19 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class ApplicationContextEventTests method lambdaAsListenerWithJava8StyleClassCastMessage.

@Test
public void lambdaAsListenerWithJava8StyleClassCastMessage() {
    StaticApplicationContext context = new StaticApplicationContext();
    ApplicationListener<ApplicationEvent> listener = event -> {
        throw new ClassCastException(event.getClass().getName());
    };
    context.addApplicationListener(listener);
    context.refresh();
    context.publishEvent(new MyEvent(context));
    context.close();
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Ordered(org.springframework.core.Ordered) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MethodInvocation(org.aopalliance.intercept.MethodInvocation) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) BDDMockito.given(org.mockito.BDDMockito.given) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) TestBean(org.springframework.beans.testfixture.beans.TestBean) BeanThatBroadcasts(org.springframework.context.testfixture.beans.BeanThatBroadcasts) ResolvableType(org.springframework.core.ResolvableType) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Order(org.springframework.core.annotation.Order) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Set(java.util.Set) BeansException(org.springframework.beans.BeansException) Mockito.times(org.mockito.Mockito.times) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationContext(org.springframework.context.ApplicationContext) BeanThatListens(org.springframework.context.testfixture.beans.BeanThatListens) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) ApplicationEvent(org.springframework.context.ApplicationEvent) List(java.util.List) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ReflectionUtils(org.springframework.util.ReflectionUtils) ProxyFactory(org.springframework.aop.framework.ProxyFactory) TaskUtils(org.springframework.scheduling.support.TaskUtils) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Mockito.mock(org.mockito.Mockito.mock) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.jupiter.api.Test)

Example 20 with ApplicationEvent

use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.

the class ApplicationContextEventTests method simpleApplicationEventMulticasterWithException.

@Test
public void simpleApplicationEventMulticasterWithException() {
    @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
    ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
    SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
    smc.addApplicationListener(listener);
    RuntimeException thrown = new RuntimeException();
    willThrow(thrown).given(listener).onApplicationEvent(evt);
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> smc.multicastEvent(evt)).satisfies(ex -> assertThat(ex).isSameAs(thrown));
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationEvent (org.springframework.context.ApplicationEvent)44 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)16 Test (org.junit.Test)14 Test (org.junit.jupiter.api.Test)11 ApplicationListener (org.springframework.context.ApplicationListener)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 ArrayList (java.util.ArrayList)7 PayloadApplicationEvent (org.springframework.context.PayloadApplicationEvent)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 ApplicationContext (org.springframework.context.ApplicationContext)6 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Socket (java.net.Socket)4 List (java.util.List)4 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)4 IOException (java.io.IOException)3 InOrder (org.mockito.InOrder)3 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)3 ApplicationStartedEvent (org.springframework.boot.context.event.ApplicationStartedEvent)3