Search in sources :

Example 21 with ApplicationEvent

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);
        }
    }
}
Also used : PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent)

Example 22 with ApplicationEvent

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);
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) DefaultProducer(org.apache.camel.impl.DefaultProducer) ApplicationEvent(org.springframework.context.ApplicationEvent)

Example 23 with ApplicationEvent

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"));
}
Also used : ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.Test)

Example 24 with ApplicationEvent

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();
                }
            }
        }
    });
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ArrayList(java.util.ArrayList) ApplicationEvent(org.springframework.context.ApplicationEvent) ApplicationListener(org.springframework.context.ApplicationListener) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 25 with ApplicationEvent

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());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ApplicationEvent(org.springframework.context.ApplicationEvent) Test(org.junit.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