use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.
the class AbstractApplicationContext method publishEvent.
/**
* Publish the given event to all listeners.
* @param event the event to publish (may be an {@link ApplicationEvent}
* or a payload object to be turned into a {@link PayloadApplicationEvent})
* @param eventType the resolved event type, if known
* @since 4.2
*/
protected void publishEvent(Object event, ResolvableType eventType) {
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
}
// Decorate event as an ApplicationEvent if necessary
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
} else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
}
}
// Multicast right now if possible - or lazily once the multicaster is initialized
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
} else {
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
// Publish event via parent context as well...
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
} else {
this.parent.publishEvent(event);
}
}
}
use of org.springframework.context.ApplicationEvent in project camel by apache.
the class EventEndpoint method createProducer.
public Producer createProducer() throws Exception {
ObjectHelper.notNull(applicationContext, "applicationContext");
return new DefaultProducer(this) {
public void process(Exchange exchange) throws Exception {
ApplicationEvent event = toApplicationEvent(exchange);
applicationContext.publishEvent(event);
}
};
}
use of org.springframework.context.ApplicationEvent in project pentaho-platform by pentaho.
the class PentahoSessionStartupAuthenticationSuccessListenerTest method testOnApplicationEvent_AuthenticationEvent.
@Test
public void testOnApplicationEvent_AuthenticationEvent() {
ApplicationEvent event = mock(ApplicationEvent.class);
PentahoSessionStartupAuthenticationSuccessListener listener = new PentahoSessionStartupAuthenticationSuccessListener();
listener.onApplicationEvent(event);
System.out.flush();
assertNotNull(baos);
assertFalse(baos.toString().contains("calling PentahoSystem.sessionStartup"));
}
use of org.springframework.context.ApplicationEvent in project pentaho-platform by pentaho.
the class PublishedBeanRegistry method registerFactory.
public static void registerFactory(ApplicationContext applicationContext) {
Object markerBean = null;
try {
// The marker may not be present if there are no published beans from this factory.
markerBean = applicationContext.getBean(Const.FACTORY_MARKER);
} catch (NoSuchBeanDefinitionException ignored) {
// ignore
}
if (markerBean == null) {
// applicationContext
return;
}
factoryMarkerCache.put(applicationContext, markerBean);
final ConfigurableApplicationContext listableBeanFactory = (ConfigurableApplicationContext) applicationContext;
List<IPentahoObjectRegistration> registrationList = new ArrayList<>();
handleRegistry.put(listableBeanFactory, registrationList);
Map<Class<?>, List<String>> classListMap = classToBeanMap.get(markerBean);
for (Map.Entry<Class<?>, List<String>> classListEntry : classListMap.entrySet()) {
Class<?> clazz = classListEntry.getKey();
for (String beanName : classListEntry.getValue()) {
IPentahoObjectRegistration iPentahoObjectRegistration = PentahoSystem.registerReference(new SpringPentahoObjectReference(listableBeanFactory, beanName, clazz, null, listableBeanFactory.getBeanFactory().getBeanDefinition(beanName)));
registrationList.add(iPentahoObjectRegistration);
}
}
listableBeanFactory.addApplicationListener(new ApplicationListener() {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if (applicationEvent instanceof ContextClosedEvent) {
for (IPentahoObjectRegistration iPentahoObjectRegistration : handleRegistry.get(listableBeanFactory)) {
iPentahoObjectRegistration.remove();
}
}
}
});
}
use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.
the class ApplicationEventPublishingMessageHandlerTests method messagingEvent.
@Test
public void messagingEvent() throws InterruptedException {
TestApplicationEventPublisher publisher = new TestApplicationEventPublisher();
ApplicationEventPublishingMessageHandler handler = new ApplicationEventPublishingMessageHandler();
handler.setApplicationEventPublisher(publisher);
assertNull(publisher.getLastEvent());
Message<?> message = new GenericMessage<String>("testing");
handler.handleMessage(message);
ApplicationEvent event = publisher.getLastEvent();
assertEquals(MessagingEvent.class, event.getClass());
assertEquals(message, ((MessagingEvent) event).getMessage());
}
Aggregations