@Override public void onApplicationEvent(ContextClosedEvent event) { Map<String, OSS> ossClientMap = event.getApplicationContext() .getBeansOfType(OSS.class); log.info("{} OSSClients will be shutdown soon", ossClientMap.size()); ossClientMap.keySet().forEach(beanName -> { log.info("shutdown ossClient: {}", beanName); ossClientMap.get(beanName).shutdown(); }); } }
publishEvent(new ContextClosedEvent(this));
@EventListener @Order(Ordered.LOWEST_PRECEDENCE) public void onClosedContext(ContextClosedEvent event) { if (event.getApplicationContext().getParent() == null || "bootstrap".equals(event.getApplicationContext().getParent().getId())) { stopRegisterTask(); if (autoDeregister) { registrator.deregister(); } } }
publishEvent(new ContextClosedEvent(this));
@EventListener(ContextClosedEvent.class) public void onApplicationEvent(ContextClosedEvent event) { if (event.getApplicationContext() == context) { stop(); } } }
@Test public void simpleApplicationEventMulticasterWithErrorHandler() { @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class); ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext()); SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster(); smc.setErrorHandler(TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER); smc.addApplicationListener(listener); willThrow(new RuntimeException()).given(listener).onApplicationEvent(evt); smc.multicastEvent(evt); }
@Override public void onApplicationEvent(ContextClosedEvent event) { ConfigurableApplicationContext context = this.childContext.get(); if ((context != null) && (event.getApplicationContext() == context.getParent()) && context.isActive()) { context.close(); } }
@Test public void multicastSimpleEvent() { multicastEvent(true, ApplicationListener.class, new ContextRefreshedEvent(new StaticApplicationContext()), null); multicastEvent(true, ApplicationListener.class, new ContextClosedEvent(new StaticApplicationContext()), null); }
@Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationStartingEvent) { onApplicationStartingEvent((ApplicationStartingEvent) event); } else if (event instanceof ApplicationEnvironmentPreparedEvent) { onApplicationEnvironmentPreparedEvent( (ApplicationEnvironmentPreparedEvent) event); } else if (event instanceof ApplicationPreparedEvent) { onApplicationPreparedEvent((ApplicationPreparedEvent) event); } else if (event instanceof ContextClosedEvent && ((ContextClosedEvent) event) .getApplicationContext().getParent() == null) { onContextClosedEvent(); } else if (event instanceof ApplicationFailedEvent) { onApplicationFailedEvent(); } }
@Test public void simpleApplicationEventMulticasterWithException() { @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class); ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext()); SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster(); smc.addApplicationListener(listener); RuntimeException thrown = new RuntimeException(); willThrow(thrown).given(listener).onApplicationEvent(evt); try { smc.multicastEvent(evt); fail("Should have thrown RuntimeException"); } catch (RuntimeException ex) { assertSame(thrown, ex); } }
@Override public void onApplicationEvent(ContextClosedEvent event) { if (applicationContext == event.getApplicationContext()) { getScheduledExecutor().shutdown(); } }
@Test public void simpleApplicationEventMulticasterWithTaskExecutor() { @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class); ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext()); SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster(); smc.setTaskExecutor(new Executor() { @Override public void execute(Runnable command) { command.run(); command.run(); } }); smc.addApplicationListener(listener); smc.multicastEvent(evt); verify(listener, times(2)).onApplicationEvent(evt); }
@Override public void onApplicationEvent(ContextClosedEvent event) { Map<String, OSS> ossClientMap = event.getApplicationContext() .getBeansOfType(OSS.class); logger.info("{} OSSClients will be shutdown soon", ossClientMap.size()); ossClientMap.keySet().forEach(beanName -> { logger.info("shutdown ossClient: {}", beanName); ossClientMap.get(beanName).shutdown(); }); } }
publishEvent(new ContextClosedEvent(this));
public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextClosedEvent) { ApplicationContext context = ((ContextClosedEvent) event).getApplicationContext(); System.out.println("Trying to get the bean names..."); String[] beanNamesForType = context.getBeanNamesForType(OsgiServiceProxyFactoryBean.class, true, false); for (int i = 0; i < beanNamesForType.length; i++) { String string = beanNamesForType[i]; System.out.println("found importers ... " + string); } if (log.isDebugEnabled()) log.debug("Resetting importers (on shutdown): " + beanNamesForType); // for (Iterator iterator = beansOfType.values().iterator(); iterator.hasNext();) { // OsgiServiceProxyFactoryBean type = (OsgiServiceProxyFactoryBean) iterator.next(); // type.setTimeout(0); // } } } }
@Override public void onApplicationEvent(Object event) { if (event instanceof StartupEvent) { eventPublisher.publishEvent(new ContextStartedEvent( applicationContext )); } else if (event instanceof ShutdownEvent) { eventPublisher.publishEvent(new ContextClosedEvent( applicationContext )); } } }
public void onApplicationEvent(ContextClosedEvent event) { if( event.getApplicationContext() == context ) { stop(); } }
publishEvent(new ContextClosedEvent(this));
@EventListener public void handleContextStoppedEvent(final ContextClosedEvent contextStoppedEvent) { if (parentContext == contextStoppedEvent.getApplicationContext()) { publisher.publishEvent(new ProcessApplicationStoppedEvent(contextStoppedEvent)); } }
public void testBootstrapAndShutdown() throws Exception { // now bring up the bootstrap ApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML); // the folder should be gone assertFalse("Folder was not deleted by bootstrap", dir.exists()); // now create the folder again dir.mkdir(); assertTrue("Directory not created", dir.exists()); // announce that the context is closing ctx.publishEvent(new ContextClosedEvent(ctx)); // the folder should be gone assertFalse("Folder was not deleted by shutdown", dir.exists()); }