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