From afa4807f58badcae0905902a7ccc5bffd2b81dae Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 27 Nov 2009 15:59:12 +0000 Subject: [PATCH] SPR-5507 Factored out the Phased interface. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2532 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../org/springframework/context/Phased.java | 34 +++++++++++ .../context/SmartLifecycle.java | 56 ++++++++++--------- 2 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 org.springframework.context/src/main/java/org/springframework/context/Phased.java diff --git a/org.springframework.context/src/main/java/org/springframework/context/Phased.java b/org.springframework.context/src/main/java/org/springframework/context/Phased.java new file mode 100644 index 00000000000..3d262233b94 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/context/Phased.java @@ -0,0 +1,34 @@ +/* + * 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.context; + +/** + * Interface for objects that may participate in a phased + * process such as lifecycle management. + * + * @author Mark Fisher + * @since 3.0 + * @see SmartLifecycle + */ +public interface Phased { + + /** + * Return the phase value of this object. + */ + int getPhase(); + +} diff --git a/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java b/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java index 5b1f060a9bf..bbee8cdbce8 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java +++ b/org.springframework.context/src/main/java/org/springframework/context/SmartLifecycle.java @@ -17,13 +17,41 @@ package org.springframework.context; /** - * An extension of the Lifecycle interface for those beans that require to be + * An extension of the Lifecycle interface for those objects that require to be * started upon ApplicationContext refresh and/or shutdown in a particular order. + * The {@link #isAutoStartup()} return value indicates whether this object should + * be started at the time of a context refresh. The callback-accepting + * {@link #stop(Runnable)} method is useful for objects that have an asynchronous + * shutdown process. Any implementation of this interface must invoke the + * callback's run() method upon shutdown completion to avoid unnecessary delays + * in the overall ApplicationContext shutdown. + *

+ * This interface extends {@link Phased}, and the {@link #getPhase()} method's + * return value indicates the phase within which this Lifecycle component should + * be started and stopped. The startup process begins with the lowest + * phase value and ends with the highest phase value (Integer.MIN_VALUE + * is the lowest possible, and Integer.MAX_VALUE is the highest possible). The + * shutdown process will apply the reverse order. Any components with the + * same value will be arbitrarily ordered within the same phase. + *

+ * Example: if component B depends on component A having already started, then + * component A should have a lower phase value than component B. During the + * shutdown process, component B would be stopped before component A. + *

+ * Any explicit "depends-on" relationship will take precedence over + * the phase order such that the dependent bean always starts after its + * dependency and always stops before its dependency. + *

+ * Any Lifecycle components within the context that do not also implement + * SmartLifecycle will be treated as if they have a phase value of 0. That + * way a SmartLifecycle implementation may start before those Lifecycle + * components if it has a negative phase value, or it may start after + * those components if it has a positive phase value. * * @author Mark Fisher * @since 3.0 */ -public interface SmartLifecycle extends Lifecycle { +public interface SmartLifecycle extends Lifecycle, Phased { /** * Return whether this Lifecycle component should be started automatically @@ -32,30 +60,6 @@ public interface SmartLifecycle extends Lifecycle { */ boolean isAutoStartup(); - /** - * Return the phase within which this Lifecycle component should be started - * and stopped. The startup process begins with the lowest phase - * value and ends with the highest phase value (Integer.MIN_VALUE is - * the lowest possible, and Integer.MAX_VALUE is the highest possible). The - * shutdown process will apply the reverse order. Any components with the - * same value will be arbitrarily ordered within the same phase. - *

- * Example: if component B depends on component A having already started, then - * component A should have a lower phase value than component B. During the - * shutdown process, component B would be stopped before component A. - *

- * Any Lifecycle components within the context that do not also implement - * SmartLifecycle will be treated as if they have a phase value of 0. That - * way a SmartLifecycle implementation may start before those Lifecycle - * components if it has a negative phase value, or it may start after - * those components if it has a positive phase value. - *

- * Any explicit "depends-on" relationship will take precedence over - * the phase order such that the dependent bean always starts after its - * dependency and always stops before its dependency. - */ - int getPhase(); - /** * Indicates that a Lifecycle component must stop if it is currently running. * The provided callback is used by the LifecycleProcessor to support an