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;
}
}
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();
}
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);
}
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();
}
Aggregations