Search in sources :

Example 11 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method closingChildContextDoesNotCleanUpLoggingSystem.

@Test
void closingChildContextDoesNotCleanUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.bootstrapContext, this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils.getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    GenericApplicationContext childContext = new GenericApplicationContext();
    childContext.setParent(this.context);
    multicastEvent(new ContextClosedEvent(childContext));
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(loggingSystem.cleanedUp).isTrue();
    childContext.close();
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.jupiter.api.Test)

Example 12 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project spring-boot by spring-projects.

the class LoggingApplicationListenerTests method bridgeHandlerLifecycle.

@Test
void bridgeHandlerLifecycle() {
    assertThat(bridgeHandlerInstalled()).isTrue();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(bridgeHandlerInstalled()).isFalse();
}
Also used : ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.jupiter.api.Test)

Example 13 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project tomee by apache.

the class SpringBus method onApplicationEvent.

public void onApplicationEvent(ApplicationEvent event) {
    if (ctx == null) {
        return;
    }
    boolean doIt = false;
    ApplicationContext ac = ctx;
    while (ac != null) {
        if (event.getSource() == ac) {
            doIt = true;
            break;
        }
        ac = ac.getParent();
    }
    if (doIt) {
        if (event instanceof ContextRefreshedEvent) {
            if (getState() != BusState.RUNNING) {
                initialize();
            }
        } else if (event instanceof ContextClosedEvent && getState() == BusState.RUNNING) {
            // The bus could be create by using SpringBusFactory.createBus("/cxf.xml");
            // Just to make sure the shutdown is called rightly
            shutdown();
        }
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 14 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project alfresco-repository by Alfresco.

the class SafeApplicationEventMulticaster method multicastEvent.

@Override
public void multicastEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent && event.getSource() == this.appContext) {
        this.isApplicationStarted = true;
        for (ApplicationEvent queuedEvent : this.queuedEvents) {
            multicastEventInternal(queuedEvent);
        }
        this.queuedEvents.clear();
        multicastEventInternal(event);
    } else if (event instanceof ContextClosedEvent && event.getSource() == this.appContext) {
        this.isApplicationStarted = false;
        multicastEventInternal(event);
    } else if (this.isApplicationStarted) {
        multicastEventInternal(event);
    } else {
        this.queuedEvents.add(event);
    }
}
Also used : ApplicationEvent(org.springframework.context.ApplicationEvent) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 15 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project data-prep by Talend.

the class DistributedLockWatcherTest method shouldReleaseOnClose.

@Test
public void shouldReleaseOnClose() throws Exception {
    // given
    final LockFactory delegate = mock(LockFactory.class);
    final DistributedLock mock1 = mock(DistributedLock.class);
    when(mock1.getKey()).thenReturn("1234");
    final DistributedLock mock2 = mock(DistributedLock.class);
    when(mock2.getKey()).thenReturn("5678");
    when(delegate.getLock(eq("1234"))).thenReturn(mock1);
    when(delegate.getLock(eq("5678"))).thenReturn(mock2);
    doAnswer(invocation -> {
        // on purpose unchecked exception
        throw new RuntimeException();
    }).when(mock2).unlock();
    final DistributedLockWatcher watcher = new DistributedLockWatcher(delegate);
    watcher.getLock("1234");
    watcher.getLock("5678");
    // when
    watcher.onApplicationEvent(new ContextClosedEvent(new AnnotationConfigApplicationContext()));
    // then
    verify(mock1, times(1)).unlock();
    verify(mock2, times(1)).unlock();
    assertEquals(0, watcher.getLocks().size());
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Aggregations

ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)24 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)6 ContextRefreshedEvent (org.springframework.context.event.ContextRefreshedEvent)6 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)6 ApplicationContext (org.springframework.context.ApplicationContext)5 ApplicationRegistrator (de.codecentric.boot.admin.client.registration.ApplicationRegistrator)3 RegistrationApplicationListener (de.codecentric.boot.admin.client.registration.RegistrationApplicationListener)3 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)3 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)3 TaskScheduler (org.springframework.scheduling.TaskScheduler)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 CseApplicationListener (org.apache.servicecomb.core.CseApplicationListener)2 AfterClass (org.junit.AfterClass)2 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)2 ApplicationEvent (org.springframework.context.ApplicationEvent)2 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 EmbeddedSaturn (com.vip.saturn.embed.EmbeddedSaturn)1