Search in sources :

Example 6 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project java-chassis by ServiceComb.

the class CseApplicationListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent) {
        ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
        //TODO to load when webapplication context is used for discovery client, need to check if can use the order and undo this change with proper fix.
        if (!isInit) {
            try {
                BeanUtils.setContext(applicationContext);
                bootListenerList = applicationContext.getBeansOfType(BootListener.class).values();
                triggerEvent(EventType.BEFORE_HANDLER);
                HandlerConfigUtils.init();
                triggerEvent(EventType.AFTER_HANDLER);
                triggerEvent(EventType.BEFORE_PRODUCER_PROVIDER);
                producerProviderManager.init();
                triggerEvent(EventType.AFTER_PRODUCER_PROVIDER);
                triggerEvent(EventType.BEFORE_CONSUMER_PROVIDER);
                consumerProviderManager.init();
                triggerEvent(EventType.AFTER_CONSUMER_PROVIDER);
                triggerEvent(EventType.BEFORE_TRANSPORT);
                transportManager.init();
                triggerEvent(EventType.AFTER_TRANSPORT);
                schemaListenerManager.notifySchemaListener();
                triggerEvent(EventType.BEFORE_REGISTRY);
                RegistryUtils.init();
                triggerEvent(EventType.AFTER_REGISTRY);
                // TODO 服务优雅退出
                if (applicationContext instanceof AbstractApplicationContext) {
                    ((AbstractApplicationContext) applicationContext).registerShutdownHook();
                }
                isInit = true;
            } catch (Exception e) {
                LOGGER.error("cse init failed, {}", FortifyUtils.getErrorInfo(e));
            }
        }
    } else if (event instanceof ContextClosedEvent) {
        LOGGER.warn("cse is closing now...");
        RegistryUtils.destory();
        isInit = false;
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 7 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 8 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 9 with ContextClosedEvent

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

the class LoggingApplicationListenerTests method closingContextCleansUpLoggingSystem.

@Test
public void closingContextCleansUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(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) Matchers.containsString(org.hamcrest.Matchers.containsString) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Aggregations

ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)9 Test (org.junit.Test)7 ApplicationRegistrator (de.codecentric.boot.admin.client.registration.ApplicationRegistrator)3 RegistrationApplicationListener (de.codecentric.boot.admin.client.registration.RegistrationApplicationListener)3 TaskScheduler (org.springframework.scheduling.TaskScheduler)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)2 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)2 ApplicationContext (org.springframework.context.ApplicationContext)1 ContextRefreshedEvent (org.springframework.context.event.ContextRefreshedEvent)1 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1