Search in sources :

Example 1 with ContextClosedEvent

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);
}
Also used : ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ScheduledFuture(java.util.concurrent.ScheduledFuture) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.Test)

Example 2 with ContextClosedEvent

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();
}
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 3 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 4 with ContextClosedEvent

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);
    }
}
Also used : ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 5 with ContextClosedEvent

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);
}
Also used : CseApplicationListener(org.apache.servicecomb.core.CseApplicationListener) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) AfterClass(org.junit.AfterClass)

Aggregations

ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)23 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)6 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)6 ApplicationContext (org.springframework.context.ApplicationContext)5 ContextRefreshedEvent (org.springframework.context.event.ContextRefreshedEvent)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 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 Field (java.lang.reflect.Field)1