Search in sources :

Example 1 with ApplicationEvent

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

Example 2 with ApplicationEvent

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();
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) 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 3 with ApplicationEvent

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

Example 4 with ApplicationEvent

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();
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) ApplicationEvent(org.springframework.context.ApplicationEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TcpOutboundGateway(org.springframework.integration.ip.tcp.TcpOutboundGateway) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Test(org.junit.Test)

Example 5 with ApplicationEvent

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());
}
Also used : TcpSendingMessageHandler(org.springframework.integration.ip.tcp.TcpSendingMessageHandler) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEvent(org.springframework.context.ApplicationEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) 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