Search in sources :

Example 6 with Lifecycle

use of org.springframework.context.Lifecycle in project spring-framework by spring-projects.

the class DefaultLifecycleProcessor method doStop.

/**
	 * Stop the specified bean as part of the given set of Lifecycle beans,
	 * making sure that any beans that depends on it are stopped first.
	 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
	 * @param beanName the name of the bean to stop
	 */
private void doStop(Map<String, ? extends Lifecycle> lifecycleBeans, final String beanName, final CountDownLatch latch, final Set<String> countDownBeanNames) {
    Lifecycle bean = lifecycleBeans.remove(beanName);
    if (bean != null) {
        String[] dependentBeans = this.beanFactory.getDependentBeans(beanName);
        for (String dependentBean : dependentBeans) {
            doStop(lifecycleBeans, dependentBean, latch, countDownBeanNames);
        }
        try {
            if (bean.isRunning()) {
                if (bean instanceof SmartLifecycle) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Asking bean '" + beanName + "' of type [" + bean.getClass() + "] to stop");
                    }
                    countDownBeanNames.add(beanName);
                    ((SmartLifecycle) bean).stop(new Runnable() {

                        @Override
                        public void run() {
                            latch.countDown();
                            countDownBeanNames.remove(beanName);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Bean '" + beanName + "' completed its stop procedure");
                            }
                        }
                    });
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Stopping bean '" + beanName + "' of type [" + bean.getClass() + "]");
                    }
                    bean.stop();
                    if (logger.isDebugEnabled()) {
                        logger.debug("Successfully stopped bean '" + beanName + "'");
                    }
                }
            } else if (bean instanceof SmartLifecycle) {
                // don't wait for beans that aren't running
                latch.countDown();
            }
        } catch (Throwable ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to stop bean '" + beanName + "'", ex);
            }
        }
    }
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle)

Example 7 with Lifecycle

use of org.springframework.context.Lifecycle in project spring-framework by spring-projects.

the class DefaultLifecycleProcessorTests method singleLifecycleShutdown.

@Test
public void singleLifecycleShutdown() throws Exception {
    CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
    Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
    StaticApplicationContext context = new StaticApplicationContext();
    context.getBeanFactory().registerSingleton("bean", bean);
    context.refresh();
    assertFalse(bean.isRunning());
    bean.start();
    assertTrue(bean.isRunning());
    context.stop();
    assertEquals(1, stoppedBeans.size());
    assertFalse(bean.isRunning());
    assertEquals(bean, stoppedBeans.get(0));
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 8 with Lifecycle

use of org.springframework.context.Lifecycle in project spring-framework by spring-projects.

the class DefaultLifecycleProcessorTests method mixedShutdown.

@Test
public void mixedShutdown() throws Exception {
    CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
    Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
    Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
    Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
    Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
    Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
    Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
    Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
    StaticApplicationContext context = new StaticApplicationContext();
    context.getBeanFactory().registerSingleton("bean1", bean1);
    context.getBeanFactory().registerSingleton("bean2", bean2);
    context.getBeanFactory().registerSingleton("bean3", bean3);
    context.getBeanFactory().registerSingleton("bean4", bean4);
    context.getBeanFactory().registerSingleton("bean5", bean5);
    context.getBeanFactory().registerSingleton("bean6", bean6);
    context.getBeanFactory().registerSingleton("bean7", bean7);
    context.refresh();
    assertTrue(bean2.isRunning());
    assertTrue(bean3.isRunning());
    assertTrue(bean5.isRunning());
    assertTrue(bean6.isRunning());
    assertTrue(bean7.isRunning());
    assertFalse(bean1.isRunning());
    assertFalse(bean4.isRunning());
    bean1.start();
    bean4.start();
    assertTrue(bean1.isRunning());
    assertTrue(bean4.isRunning());
    context.stop();
    assertFalse(bean1.isRunning());
    assertFalse(bean2.isRunning());
    assertFalse(bean3.isRunning());
    assertFalse(bean4.isRunning());
    assertFalse(bean5.isRunning());
    assertFalse(bean6.isRunning());
    assertFalse(bean7.isRunning());
    assertEquals(7, stoppedBeans.size());
    assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
    assertEquals(500, getPhase(stoppedBeans.get(1)));
    assertEquals(1, getPhase(stoppedBeans.get(2)));
    assertEquals(0, getPhase(stoppedBeans.get(3)));
    assertEquals(0, getPhase(stoppedBeans.get(4)));
    assertEquals(-1, getPhase(stoppedBeans.get(5)));
    assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

Lifecycle (org.springframework.context.Lifecycle)8 SmartLifecycle (org.springframework.context.SmartLifecycle)6 LinkedHashMap (java.util.LinkedHashMap)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Test (org.junit.Test)2 Status (com.alibaba.dubbo.common.status.Status)1 Method (java.lang.reflect.Method)1 Before (org.junit.Before)1 ApplicationContext (org.springframework.context.ApplicationContext)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1