use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.
the class AbstractApplicationContext method registerListeners.
/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
use of org.springframework.context.ApplicationEvent in project cxf by apache.
the class SpringBus method setApplicationContext.
/**
* {@inheritDoc}
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
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 pentaho-platform by pentaho.
the class PentahoSessionStartupAuthenticationSuccessListenerTest method testOnApplicationEvent_onlyInteractiveAuthenticationSuccessEvent.
@Test
public void testOnApplicationEvent_onlyInteractiveAuthenticationSuccessEvent() {
ApplicationEvent event = new InteractiveAuthenticationSuccessEvent(authentication, InteractiveAuthenticationSuccessEvent.class);
PentahoSessionStartupAuthenticationSuccessListener listener = new PentahoSessionStartupAuthenticationSuccessListener();
listener.onApplicationEvent(event);
System.out.flush();
assertNotNull(baos);
assertTrue(baos.toString().contains("calling PentahoSystem.sessionStartup"));
}
use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.
the class ConnectionEventTests method testOutboundGatewayNoConnectionEvents.
@Test
public void testOutboundGatewayNoConnectionEvents() {
TcpOutboundGateway gw = new TcpOutboundGateway();
AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(Object event) {
}
@Override
public void publishEvent(ApplicationEvent event) {
theEvent.set(event);
}
});
gw.setConnectionFactory(ccf);
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
gw.start();
Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
gw.onMessage(message);
assertNotNull(theEvent.get());
TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertEquals("bar", event.getConnectionId());
MessagingException messagingException = (MessagingException) event.getCause();
assertSame(message, messagingException.getFailedMessage());
assertEquals("Cannot correlate response - no pending reply for bar", messagingException.getMessage());
message = new GenericMessage<String>("foo");
gw.onMessage(message);
assertNotNull(theEvent.get());
event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertNull(event.getConnectionId());
messagingException = (MessagingException) event.getCause();
assertSame(message, messagingException.getFailedMessage());
assertEquals("Cannot correlate response - no connection id", messagingException.getMessage());
gw.stop();
ccf.stop();
}
use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.
the class ConnectionEventTests method testOutboundChannelAdapterNoConnectionEvents.
@Test
public void testOutboundChannelAdapterNoConnectionEvents() {
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) {
@Override
public void run() {
}
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
scf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(Object event) {
}
@Override
public void publishEvent(ApplicationEvent event) {
theEvent.set(event);
}
});
handler.setConnectionFactory(scf);
handler.start();
Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
try {
handler.handleMessage(message);
fail("expected exception");
} catch (MessageHandlingException e) {
assertThat(e.getMessage(), Matchers.containsString("Unable to find outbound socket"));
}
assertNotNull(theEvent.get());
TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertEquals("bar", event.getConnectionId());
assertSame(message, ((MessagingException) event.getCause()).getFailedMessage());
}
Aggregations