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();
}
}
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;
}
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);
}
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();
}
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));
}
Aggregations