Search in sources :

Example 21 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project cxf by apache.

the class SpringBus method onApplicationEvent.

public void onApplicationEvent(ApplicationEvent event) {
    if (ctx == null) {
        return;
    }
    boolean doIt = false;
    ApplicationContext ac = ctx;
    while (ac != null) {
        if (event.getSource() == ac) {
            doIt = true;
            break;
        }
        ac = ac.getParent();
    }
    if (doIt) {
        if (event instanceof ContextRefreshedEvent) {
            if (getState() != BusState.RUNNING) {
                initialize();
            }
        } else if (event instanceof ContextClosedEvent && getState() == BusState.RUNNING) {
            // The bus could be create by using SpringBusFactory.createBus("/cxf.xml");
            // Just to make sure the shutdown is called rightly
            shutdown();
        }
    }
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 22 with ContextClosedEvent

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

the class RegistrationApplicationListenerTest method should_cancel_register_task_on_context_close.

@Test
public void should_cancel_register_task_on_context_close() {
    ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
    ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
    RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
    ScheduledFuture<?> task = mock(ScheduledFuture.class);
    when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(Duration.ofSeconds(10)))).then((invocation) -> task);
    listener.onApplicationReady(new ApplicationReadyEvent(mock(SpringApplication.class), null, mock(ConfigurableWebApplicationContext.class)));
    verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(Duration.ofSeconds(10)));
    listener.onClosedContext(new ContextClosedEvent(mock(WebApplicationContext.class)));
    verify(task).cancel(true);
}
Also used : ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.jupiter.api.Test)

Example 23 with ContextClosedEvent

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

the class RegistrationApplicationListenerTest method should_deregister_when_autoDeregister_and_parent_is_bootstrap_contex.

@Test
public void should_deregister_when_autoDeregister_and_parent_is_bootstrap_contex() {
    ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
    ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
    RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
    listener.setAutoDeregister(true);
    ApplicationContext parentContext = mock(ApplicationContext.class);
    when(parentContext.getId()).thenReturn("bootstrap");
    ApplicationContext mockContext = mock(ApplicationContext.class);
    when(mockContext.getParent()).thenReturn(parentContext);
    listener.onClosedContext(new ContextClosedEvent(mockContext));
    verify(registrator).deregister();
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent) Test(org.junit.jupiter.api.Test)

Example 24 with ContextClosedEvent

use of org.springframework.context.event.ContextClosedEvent in project pentaho-platform by pentaho.

the class PublishedBeanRegistry method registerFactory.

public static void registerFactory(ApplicationContext applicationContext) {
    Object markerBean = null;
    try {
        // The marker may not be present if there are no published beans from this factory.
        markerBean = applicationContext.getBean(Const.FACTORY_MARKER);
    } catch (NoSuchBeanDefinitionException ignored) {
    // ignore
    }
    if (markerBean == null) {
        // applicationContext
        return;
    }
    factoryMarkerCache.put(applicationContext, markerBean);
    final ConfigurableApplicationContext listableBeanFactory = (ConfigurableApplicationContext) applicationContext;
    List<IPentahoObjectRegistration> registrationList = new ArrayList<>();
    handleRegistry.put(listableBeanFactory, registrationList);
    Map<Class<?>, List<String>> classListMap = classToBeanMap.get(markerBean);
    for (Map.Entry<Class<?>, List<String>> classListEntry : classListMap.entrySet()) {
        Class<?> clazz = classListEntry.getKey();
        for (String beanName : classListEntry.getValue()) {
            IPentahoObjectRegistration iPentahoObjectRegistration = PentahoSystem.registerReference(new SpringPentahoObjectReference(listableBeanFactory, beanName, clazz, null, listableBeanFactory.getBeanFactory().getBeanDefinition(beanName)));
            registrationList.add(iPentahoObjectRegistration);
        }
    }
    listableBeanFactory.addApplicationListener(new ApplicationListener() {

        @Override
        public void onApplicationEvent(ApplicationEvent applicationEvent) {
            if (applicationEvent instanceof ContextClosedEvent) {
                for (IPentahoObjectRegistration iPentahoObjectRegistration : handleRegistry.get(listableBeanFactory)) {
                    iPentahoObjectRegistration.remove();
                }
            }
        }
    });
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ArrayList(java.util.ArrayList) ApplicationEvent(org.springframework.context.ApplicationEvent) ApplicationListener(org.springframework.context.ApplicationListener) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

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