use of org.springframework.context.event.ContextClosedEvent in project spring-boot-admin by codecentric.
the class StatusUpdateApplicationListenerTest method test_start_stop.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test_start_stop() throws Exception {
StatusUpdater statusUpdater = mock(StatusUpdater.class);
ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(statusUpdater, 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.onContextClosed(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
verify(task).cancel(true);
}
use of org.springframework.context.event.ContextClosedEvent in project spring-boot-admin by codecentric.
the class RegistrationApplicationListenerTest method test_no_deregister.
@Test
public void test_no_deregister() throws Exception {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
TaskScheduler scheduler = mock(TaskScheduler.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
verify(registrator, never()).deregister();
}
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-framework by spring-projects.
the class AbstractApplicationContext method doClose.
/**
* Actually performs context closing: publishes a ContextClosedEvent and
* destroys the singletons in the bean factory of this application context.
* <p>Called by both {@code close()} and a JVM shutdown hook, if any.
* @see org.springframework.context.event.ContextClosedEvent
* @see #destroyBeans()
* @see #close()
* @see #registerShutdownHook()
*/
protected void doClose() {
if (this.active.get() && this.closed.compareAndSet(false, true)) {
if (logger.isInfoEnabled()) {
logger.info("Closing " + this);
}
LiveBeansView.unregisterApplicationContext(this);
try {
// Publish shutdown event.
publishEvent(new ContextClosedEvent(this));
} catch (Throwable ex) {
logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);
}
// Stop all Lifecycle beans, to avoid delays during individual destruction.
try {
getLifecycleProcessor().onClose();
} catch (Throwable ex) {
logger.warn("Exception thrown from LifecycleProcessor on context close", ex);
}
// Destroy all cached singletons in the context's BeanFactory.
destroyBeans();
// Close the state of this context itself.
closeBeanFactory();
// Let subclasses do some final clean-up if they wish...
onClose();
this.active.set(false);
}
}
use of org.springframework.context.event.ContextClosedEvent in project incubator-servicecomb-java-chassis by apache.
the class RawSpringMvcSimplifiedMappingAnnotationIntegrationTest 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);
}
Aggregations