|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -23,43 +23,56 @@ package org.springframework.context; |
|
|
|
* be started at the time of a context refresh. The callback-accepting |
|
|
|
* be started at the time of a context refresh. The callback-accepting |
|
|
|
* {@link #stop(Runnable)} method is useful for objects that have an asynchronous |
|
|
|
* {@link #stop(Runnable)} method is useful for objects that have an asynchronous |
|
|
|
* shutdown process. Any implementation of this interface <i>must</i> invoke the |
|
|
|
* shutdown process. Any implementation of this interface <i>must</i> invoke the |
|
|
|
* callback's run() method upon shutdown completion to avoid unnecessary delays |
|
|
|
* callback's {@code run()} method upon shutdown completion to avoid unnecessary |
|
|
|
* in the overall ApplicationContext shutdown. |
|
|
|
* delays in the overall ApplicationContext shutdown. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>This interface extends {@link Phased}, and the {@link #getPhase()} method's |
|
|
|
* <p>This interface extends {@link Phased}, and the {@link #getPhase()} method's |
|
|
|
* return value indicates the phase within which this Lifecycle component should |
|
|
|
* return value indicates the phase within which this Lifecycle component should |
|
|
|
* be started and stopped. The startup process begins with the <i>lowest</i> |
|
|
|
* be started and stopped. The startup process begins with the <i>lowest</i> phase |
|
|
|
* phase value and ends with the <i>highest</i> phase value (Integer.MIN_VALUE |
|
|
|
* value and ends with the <i>highest</i> phase value ({@code Integer.MIN_VALUE} |
|
|
|
* is the lowest possible, and Integer.MAX_VALUE is the highest possible). The |
|
|
|
* is the lowest possible, and {@code Integer.MAX_VALUE} is the highest possible). |
|
|
|
* shutdown process will apply the reverse order. Any components with the |
|
|
|
* The shutdown process will apply the reverse order. Any components with the |
|
|
|
* same value will be arbitrarily ordered within the same phase. |
|
|
|
* same value will be arbitrarily ordered within the same phase. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Example: if component B depends on component A having already started, then |
|
|
|
* <p>Example: if component B depends on component A having already started, |
|
|
|
* component A should have a lower phase value than component B. During the |
|
|
|
* then component A should have a lower phase value than component B. During |
|
|
|
* shutdown process, component B would be stopped before component A. |
|
|
|
* the shutdown process, component B would be stopped before component A. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Any explicit "depends-on" relationship will take precedence over |
|
|
|
* <p>Any explicit "depends-on" relationship will take precedence over the phase |
|
|
|
* the phase order such that the dependent bean always starts after its |
|
|
|
* order such that the dependent bean always starts after its dependency and |
|
|
|
* dependency and always stops before its dependency. |
|
|
|
* always stops before its dependency. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Any Lifecycle components within the context that do not also implement |
|
|
|
* <p>Any {@code Lifecycle} components within the context that do not also |
|
|
|
* SmartLifecycle will be treated as if they have a phase value of 0. That |
|
|
|
* implement {@code SmartLifecycle} will be treated as if they have a phase |
|
|
|
* way a SmartLifecycle implementation may start before those Lifecycle |
|
|
|
* value of 0. That way a {@code SmartLifecycle} implementation may start |
|
|
|
* components if it has a negative phase value, or it may start after |
|
|
|
* before those {@code Lifecycle} components if it has a negative phase value, |
|
|
|
* those components if it has a positive phase value. |
|
|
|
* or it may start after those components if it has a positive phase value. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Note that, due to the auto-startup support in SmartLifecycle, |
|
|
|
* <p>Note that, due to the auto-startup support in {@code SmartLifecycle}, a |
|
|
|
* a SmartLifecycle bean instance will get initialized on startup of the |
|
|
|
* {@code SmartLifecycle} bean instance will usually get initialized on startup |
|
|
|
* application context in any case. As a consequence, the bean definition |
|
|
|
* of the application context in any case. As a consequence, the bean definition |
|
|
|
* lazy-init flag has very limited actual effect on SmartLifecycle beans. |
|
|
|
* lazy-init flag has very limited actual effect on {@code SmartLifecycle} beans. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Mark Fisher |
|
|
|
* @author Mark Fisher |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 3.0 |
|
|
|
* @since 3.0 |
|
|
|
* @see LifecycleProcessor |
|
|
|
* @see LifecycleProcessor |
|
|
|
* @see ConfigurableApplicationContext |
|
|
|
* @see ConfigurableApplicationContext |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public interface SmartLifecycle extends Lifecycle, Phased { |
|
|
|
public interface SmartLifecycle extends Lifecycle, Phased { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The default phase for {@code SmartLifecycle}: {@code Integer.MAX_VALUE}. |
|
|
|
|
|
|
|
* <p>This is different from the common phase 0 associated with regular |
|
|
|
|
|
|
|
* {@link Lifecycle} implementations, putting the typically auto-started |
|
|
|
|
|
|
|
* {@code SmartLifecycle} beans into a separate later shutdown phase. |
|
|
|
|
|
|
|
* @since 5.1 |
|
|
|
|
|
|
|
* @see #getPhase() |
|
|
|
|
|
|
|
* @see org.springframework.context.support.DefaultLifecycleProcessor#getPhase(Lifecycle) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int DEFAULT_PHASE = Integer.MAX_VALUE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns {@code true} if this {@code Lifecycle} component should get |
|
|
|
* Returns {@code true} if this {@code Lifecycle} component should get |
|
|
|
* started automatically by the container at the time that the containing |
|
|
|
* started automatically by the container at the time that the containing |
|
|
|
@ -67,12 +80,15 @@ public interface SmartLifecycle extends Lifecycle, Phased { |
|
|
|
* <p>A value of {@code false} indicates that the component is intended to |
|
|
|
* <p>A value of {@code false} indicates that the component is intended to |
|
|
|
* be started through an explicit {@link #start()} call instead, analogous |
|
|
|
* be started through an explicit {@link #start()} call instead, analogous |
|
|
|
* to a plain {@link Lifecycle} implementation. |
|
|
|
* to a plain {@link Lifecycle} implementation. |
|
|
|
|
|
|
|
* <p>The default implementation returns {@code true}. |
|
|
|
* @see #start() |
|
|
|
* @see #start() |
|
|
|
* @see #getPhase() |
|
|
|
* @see #getPhase() |
|
|
|
* @see LifecycleProcessor#onRefresh() |
|
|
|
* @see LifecycleProcessor#onRefresh() |
|
|
|
* @see ConfigurableApplicationContext#refresh() |
|
|
|
* @see ConfigurableApplicationContext#refresh() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
boolean isAutoStartup(); |
|
|
|
default boolean isAutoStartup() { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Indicates that a Lifecycle component must stop if it is currently running. |
|
|
|
* Indicates that a Lifecycle component must stop if it is currently running. |
|
|
|
@ -84,9 +100,30 @@ public interface SmartLifecycle extends Lifecycle, Phased { |
|
|
|
* {@code stop} method; i.e. {@link Lifecycle#stop()} will not be called for |
|
|
|
* {@code stop} method; i.e. {@link Lifecycle#stop()} will not be called for |
|
|
|
* {@code SmartLifecycle} implementations unless explicitly delegated to within |
|
|
|
* {@code SmartLifecycle} implementations unless explicitly delegated to within |
|
|
|
* the implementation of this method. |
|
|
|
* the implementation of this method. |
|
|
|
|
|
|
|
* <p>The default implementation delegates to {@link #stop()} and immediately |
|
|
|
|
|
|
|
* triggers the given callback in the calling thread. Note that there is no |
|
|
|
|
|
|
|
* synchronization between the two, so custom implementations may at least |
|
|
|
|
|
|
|
* want to put the same steps within their common lifecycle monitor (if any). |
|
|
|
* @see #stop() |
|
|
|
* @see #stop() |
|
|
|
* @see #getPhase() |
|
|
|
* @see #getPhase() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void stop(Runnable callback); |
|
|
|
default void stop(Runnable callback) { |
|
|
|
|
|
|
|
stop(); |
|
|
|
|
|
|
|
callback.run(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Return the phase that this lifecycle object is supposed to run in. |
|
|
|
|
|
|
|
* <p>The default implementation returns {@link #DEFAULT_PHASE} in order to |
|
|
|
|
|
|
|
* let stop callbacks execute after regular {@code Lifecycle} implementations. |
|
|
|
|
|
|
|
* @see #isAutoStartup() |
|
|
|
|
|
|
|
* @see #start() |
|
|
|
|
|
|
|
* @see #stop(Runnable) |
|
|
|
|
|
|
|
* @see org.springframework.context.support.DefaultLifecycleProcessor#getPhase(Lifecycle) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
default int getPhase() { |
|
|
|
|
|
|
|
return DEFAULT_PHASE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|