@Test public void singleSmartLifecycleAutoStartup() throws Exception { CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans); bean.setAutoStartup(true); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("bean", bean); assertFalse(bean.isRunning()); context.refresh(); assertTrue(bean.isRunning()); context.stop(); assertFalse(bean.isRunning()); assertEquals(1, startedBeans.size()); }
@Test public void singleSmartLifecycleShutdown() throws Exception { Assume.group(TestGroup.PERFORMANCE); CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean bean = TestSmartLifecycleBean.forShutdownTests(99, 300, stoppedBeans); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("bean", bean); context.refresh(); assertTrue(bean.isRunning()); context.stop(); assertEquals(1, stoppedBeans.size()); assertFalse(bean.isRunning()); assertEquals(bean, stoppedBeans.get(0)); }
@Test public void singleSmartLifecycleAutoStartupWithLazyInitFactoryBean() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleFactoryBean.class); bd.setLazyInit(true); context.registerBeanDefinition("bean", bd); context.refresh(); DummySmartLifecycleFactoryBean bean = context.getBean("&bean", DummySmartLifecycleFactoryBean.class); assertTrue(bean.isRunning()); context.stop(); assertFalse(bean.isRunning()); }
@Test public void singleSmartLifecycleWithoutAutoStartup() throws Exception { CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans); bean.setAutoStartup(false); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("bean", bean); assertFalse(bean.isRunning()); context.refresh(); assertFalse(bean.isRunning()); assertEquals(0, startedBeans.size()); context.start(); assertTrue(bean.isRunning()); assertEquals(1, startedBeans.size()); context.stop(); }
@Test public void singleSmartLifecycleAutoStartupWithLazyInit() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition bd = new RootBeanDefinition(DummySmartLifecycleBean.class); bd.setLazyInit(true); context.registerBeanDefinition("bean", bd); context.refresh(); DummySmartLifecycleBean bean = context.getBean("bean", DummySmartLifecycleBean.class); assertTrue(bean.isRunning()); context.stop(); assertFalse(bean.isRunning()); }
@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)); }
@Test public void dependencyStartedFirstAndIsSmartLifecycle() throws Exception { CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean beanNegative = TestSmartLifecycleBean.forStartupTests(-99, startedBeans); TestSmartLifecycleBean bean99 = TestSmartLifecycleBean.forStartupTests(99, startedBeans); TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forStartupTests(7, startedBeans); TestLifecycleBean simpleBean = TestLifecycleBean.forStartupTests(startedBeans); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("beanNegative", beanNegative); context.getBeanFactory().registerSingleton("bean7", bean7); context.getBeanFactory().registerSingleton("bean99", bean99); context.getBeanFactory().registerSingleton("simpleBean", simpleBean); context.getBeanFactory().registerDependentBean("bean7", "simpleBean"); context.refresh(); context.stop(); startedBeans.clear(); // clean start so that simpleBean is included context.start(); assertTrue(beanNegative.isRunning()); assertTrue(bean99.isRunning()); assertTrue(bean7.isRunning()); assertTrue(simpleBean.isRunning()); assertEquals(4, startedBeans.size()); assertEquals(-99, getPhase(startedBeans.get(0))); assertEquals(7, getPhase(startedBeans.get(1))); assertEquals(0, getPhase(startedBeans.get(2))); assertEquals(99, getPhase(startedBeans.get(3))); context.stop(); }
@Test public void dependencyStartedFirstButNotSmartLifecycle() throws Exception { CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean beanMin = TestSmartLifecycleBean.forStartupTests(Integer.MIN_VALUE, startedBeans); TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forStartupTests(7, startedBeans); TestLifecycleBean simpleBean = TestLifecycleBean.forStartupTests(startedBeans); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("beanMin", beanMin); context.getBeanFactory().registerSingleton("bean7", bean7); context.getBeanFactory().registerSingleton("simpleBean", simpleBean); context.getBeanFactory().registerDependentBean("simpleBean", "beanMin"); context.refresh(); assertTrue(beanMin.isRunning()); assertTrue(bean7.isRunning()); assertTrue(simpleBean.isRunning()); assertEquals(3, startedBeans.size()); assertEquals(0, getPhase(startedBeans.get(0))); assertEquals(Integer.MIN_VALUE, getPhase(startedBeans.get(1))); assertEquals(7, getPhase(startedBeans.get(2))); context.stop(); }
@Test public void singleSmartLifecycleAutoStartupWithNonAutoStartupDependency() throws Exception { CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans); bean.setAutoStartup(true); TestSmartLifecycleBean dependency = TestSmartLifecycleBean.forStartupTests(1, startedBeans); dependency.setAutoStartup(false); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("bean", bean); context.getBeanFactory().registerSingleton("dependency", dependency); context.getBeanFactory().registerDependentBean("dependency", "bean"); assertFalse(bean.isRunning()); assertFalse(dependency.isRunning()); context.refresh(); assertTrue(bean.isRunning()); assertFalse(dependency.isRunning()); context.stop(); assertFalse(bean.isRunning()); assertFalse(dependency.isRunning()); assertEquals(1, startedBeans.size()); }
@Test public void smartLifecycleGroupShutdown() throws Exception { Assume.group(TestGroup.PERFORMANCE); CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean bean1 = TestSmartLifecycleBean.forShutdownTests(1, 300, stoppedBeans); TestSmartLifecycleBean bean2 = TestSmartLifecycleBean.forShutdownTests(3, 100, stoppedBeans); TestSmartLifecycleBean bean3 = TestSmartLifecycleBean.forShutdownTests(1, 600, stoppedBeans); TestSmartLifecycleBean bean4 = TestSmartLifecycleBean.forShutdownTests(2, 400, stoppedBeans); TestSmartLifecycleBean bean5 = TestSmartLifecycleBean.forShutdownTests(2, 700, stoppedBeans); TestSmartLifecycleBean bean6 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 200, stoppedBeans); TestSmartLifecycleBean bean7 = TestSmartLifecycleBean.forShutdownTests(3, 200, 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(); context.stop(); assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0))); assertEquals(3, getPhase(stoppedBeans.get(1))); assertEquals(3, getPhase(stoppedBeans.get(2))); assertEquals(2, getPhase(stoppedBeans.get(3))); assertEquals(2, getPhase(stoppedBeans.get(4))); assertEquals(1, getPhase(stoppedBeans.get(5))); assertEquals(1, getPhase(stoppedBeans.get(6))); }
@Test public void dependencyStartedFirstEvenIfItsPhaseIsHigher() throws Exception { CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); TestSmartLifecycleBean beanMin = TestSmartLifecycleBean.forStartupTests(Integer.MIN_VALUE, startedBeans); TestSmartLifecycleBean bean2 = TestSmartLifecycleBean.forStartupTests(2, startedBeans); TestSmartLifecycleBean bean99 = TestSmartLifecycleBean.forStartupTests(99, startedBeans); TestSmartLifecycleBean beanMax = TestSmartLifecycleBean.forStartupTests(Integer.MAX_VALUE, startedBeans); StaticApplicationContext context = new StaticApplicationContext(); context.getBeanFactory().registerSingleton("beanMin", beanMin); context.getBeanFactory().registerSingleton("bean2", bean2); context.getBeanFactory().registerSingleton("bean99", bean99); context.getBeanFactory().registerSingleton("beanMax", beanMax); context.getBeanFactory().registerDependentBean("bean99", "bean2"); context.refresh(); assertTrue(beanMin.isRunning()); assertTrue(bean2.isRunning()); assertTrue(bean99.isRunning()); assertTrue(beanMax.isRunning()); assertEquals(4, startedBeans.size()); assertEquals(Integer.MIN_VALUE, getPhase(startedBeans.get(0))); assertEquals(99, getPhase(startedBeans.get(1))); assertEquals(bean99, startedBeans.get(1)); assertEquals(2, getPhase(startedBeans.get(2))); assertEquals(bean2, startedBeans.get(2)); assertEquals(Integer.MAX_VALUE, getPhase(startedBeans.get(3))); context.stop(); }
assertTrue(bean3.isRunning()); assertTrue(beanMax.isRunning()); context.stop(); assertEquals(5, startedBeans.size()); assertEquals(Integer.MIN_VALUE, getPhase(startedBeans.get(0)));
simpleBean.start(); assertTrue(simpleBean.isRunning()); context.stop(); assertFalse(beanMin.isRunning()); assertFalse(bean1.isRunning());
context.stop(); assertFalse(beanMin.isRunning()); assertFalse(beanNegative.isRunning());
@Test public void contextStoppedEvent() { StaticApplicationContext context = new StaticApplicationContext(); context.registerSingleton("lifecycle", LifecycleTestBean.class); context.registerSingleton("listener", LifecycleListener.class); context.refresh(); LifecycleTestBean lifecycleBean = (LifecycleTestBean) context.getBean("lifecycle"); LifecycleListener listener = (LifecycleListener) context.getBean("listener"); assertFalse(lifecycleBean.isRunning()); context.start(); assertTrue(lifecycleBean.isRunning()); assertEquals(0, listener.getStoppedCount()); context.stop(); assertFalse(lifecycleBean.isRunning()); assertEquals(1, listener.getStoppedCount()); assertSame(context, listener.getApplicationContext()); }
assertTrue(bean99.isRunning()); assertTrue(beanMax.isRunning()); context.stop(); assertFalse(beanMin.isRunning()); assertFalse(bean1.isRunning());
assertTrue(bean1.isRunning()); assertTrue(bean4.isRunning()); context.stop(); assertFalse(bean1.isRunning()); assertFalse(bean2.isRunning());
assertEquals(-3, getPhase(startedBeans.get(0))); assertEquals(5, getPhase(startedBeans.get(1))); context.stop(); assertFalse(simpleBean1.isRunning()); assertFalse(simpleBean2.isRunning());
@Test public void contextManagesLifecycle() { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(JmsChannelFactoryBean.class); builder.addConstructorArgValue(true); builder.addPropertyValue("connectionFactory", this.connectionFactory); builder.addPropertyValue("destinationName", "dynamicQueue"); builder.addPropertyValue("pubSubDomain", false); StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("channel", builder.getBeanDefinition()); SubscribableJmsChannel channel = context.getBean("channel", SubscribableJmsChannel.class); assertFalse(channel.isRunning()); context.refresh(); assertTrue(channel.isRunning()); context.stop(); assertFalse(channel.isRunning()); context.close(); }