Search in sources :

Example 16 with ContextClosedEvent

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

the class RegistrationApplicationListenerTest method test_deregister.

@Test
public void test_deregister() throws Exception {
    ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
    TaskScheduler scheduler = mock(TaskScheduler.class);
    RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
    listener.setAutoDeregister(true);
    listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
    verify(registrator).deregister();
}
Also used : RegistrationApplicationListener(de.codecentric.boot.admin.client.registration.RegistrationApplicationListener) ApplicationRegistrator(de.codecentric.boot.admin.client.registration.ApplicationRegistrator) TaskScheduler(org.springframework.scheduling.TaskScheduler) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Example 17 with ContextClosedEvent

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

the class RegistrationApplicationListenerTest method test_no_register_after_close.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test_no_register_after_close() throws Exception {
    ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
    TaskScheduler scheduler = mock(TaskScheduler.class);
    RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
    ScheduledFuture task = mock(ScheduledFuture.class);
    when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
    listener.onApplicationReady(new ApplicationReadyEvent(mock(SpringApplication.class), null, mock(ConfigurableWebApplicationContext.class)));
    verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
    listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
    verify(task).cancel(true);
}
Also used : RegistrationApplicationListener(de.codecentric.boot.admin.client.registration.RegistrationApplicationListener) ApplicationRegistrator(de.codecentric.boot.admin.client.registration.ApplicationRegistrator) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) TaskScheduler(org.springframework.scheduling.TaskScheduler) ScheduledFuture(java.util.concurrent.ScheduledFuture) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Example 18 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project incubator-servicecomb-java-chassis by apache.

the class RawSpringMvcIntegrationTest method shutdown.

@AfterClass
public static void shutdown() throws Exception {
    CseApplicationListener cal = BeanUtils.getBean("org.apache.servicecomb.core.CseApplicationListener");
    ContextClosedEvent event = new ContextClosedEvent(BeanUtils.getContext());
    cal.onApplicationEvent(event);
}
Also used : CseApplicationListener(org.apache.servicecomb.core.CseApplicationListener) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) AfterClass(org.junit.AfterClass)

Example 19 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project Saturn by vipshop.

the class EmbeddedSpringSaturnApplication method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    try {
        if (event instanceof ContextRefreshedEvent) {
            ContextRefreshedEvent contextRefreshedEvent = (ContextRefreshedEvent) event;
            applicationContext = contextRefreshedEvent.getApplicationContext();
            if (embeddedSaturn == null) {
                embeddedSaturn = new EmbeddedSaturn();
                embeddedSaturn.setSaturnApplication(this);
                embeddedSaturn.start();
            }
        } else if (event instanceof ContextClosedEvent) {
            if (embeddedSaturn != null) {
                embeddedSaturn.stopGracefully();
                embeddedSaturn = null;
            }
        }
    } catch (Exception e) {
        logger.warn("exception happened on event: " + event, e);
        if (!ignoreExceptions) {
            throw new RuntimeException(e);
        }
    }
}
Also used : EmbeddedSaturn(com.vip.saturn.embed.EmbeddedSaturn) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 20 with ContextClosedEvent

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

the class LoggingApplicationListenerTests method closingContextCleansUpLoggingSystem.

@Test
void closingContextCleansUpLoggingSystem() {
    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();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(loggingSystem.cleanedUp).isTrue();
}
Also used : ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.jupiter.api.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