From c6f361341101bda493262b97bd36bd6a3968fcb6 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 20 Nov 2009 18:09:14 +0000 Subject: [PATCH] SPR-6354 DefaultLifecycleProcessor no longer waits for the shutdown of SmartLifecycle beans that are not actually running. --- .../quartz/QuartzSchedulerLifecycleTests.java | 60 +++++++++++++++++++ .../quartz/quartzSchedulerLifecycleTests.xml | 13 ++++ .../support/DefaultLifecycleProcessor.java | 4 ++ 3 files changed, 77 insertions(+) create mode 100644 org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java create mode 100644 org.springframework.context.support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml diff --git a/org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java b/org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java new file mode 100644 index 00000000000..6c3b0b9ca28 --- /dev/null +++ b/org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/QuartzSchedulerLifecycleTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.quartz; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.util.StopWatch; + +/** + * @author Mark Fisher + * @since 3.0 + */ +public class QuartzSchedulerLifecycleTests { + + @Test // SPR-6354 + public void destroyLazyInitSchedulerWithDefaultShutdownOrderDoesNotHang() { + AbstractApplicationContext context = new ClassPathXmlApplicationContext("quartzSchedulerLifecycleTests.xml", this.getClass()); + assertNotNull(context.getBean("lazyInitSchedulerWithDefaultShutdownOrder")); + StopWatch sw = new StopWatch(); + sw.start("lazyScheduler"); + context.destroy(); + sw.stop(); + System.out.println(sw.getTotalTimeMillis()); + assertTrue("Quartz Scheduler with lazy-init is hanging on destruction: " + + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500); + } + + @Test // SPR-6354 + public void destroyLazyInitSchedulerWithCustomShutdownOrderDoesNotHang() { + AbstractApplicationContext context = new ClassPathXmlApplicationContext("quartzSchedulerLifecycleTests.xml", this.getClass()); + assertNotNull(context.getBean("lazyInitSchedulerWithCustomShutdownOrder")); + StopWatch sw = new StopWatch(); + sw.start("lazyScheduler"); + context.destroy(); + sw.stop(); + System.out.println(sw.getTotalTimeMillis()); + assertTrue("Quartz Scheduler with lazy-init is hanging on destruction: " + + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500); + } + +} diff --git a/org.springframework.context.support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml b/org.springframework.context.support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml new file mode 100644 index 00000000000..91df4ec5393 --- /dev/null +++ b/org.springframework.context.support/src/test/resources/org/springframework/scheduling/quartz/quartzSchedulerLifecycleTests.xml @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java b/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java index 45d2f63f101..626278e45e9 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java @@ -174,6 +174,10 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor bean.stop(); } } + else if (bean instanceof SmartLifecycle) { + // don't wait for beans that aren't running + latch.countDown(); + } lifecycleBeans.remove(beanName); } }