接口 SmartLifecycle
- 所有已知子接口:
MessageListenerContainer
- 所有已知实现类:
AbstractBrokerMessageHandler,AbstractJmsListeningContainer,AbstractMessageListenerContainer,AbstractPollingMessageListenerContainer,AnnotatedEndpointConnectionManager,ConnectionManagerSupport,DefaultMessageListenerContainer,EndpointConnectionManager,GenericMessageEndpointManager,JmsListenerEndpointRegistry,JmsMessageEndpointManager,SchedulerFactoryBean,SimpAnnotationMethodMessageHandler,SimpleBrokerMessageHandler,SimpleMessageListenerContainer,StompBrokerRelayMessageHandler,SubProtocolWebSocketHandler,UserDestinationMessageHandler,WebSocketAnnotationMethodMessageHandler,WebSocketConnectionManager,WebSocketHandlerMapping,WebSocketStompClient
public interface SmartLifecycle extends Lifecycle, Phased
An extension of theLifecycleinterface for those objects that require to be started uponApplicationContextrefresh and/or shutdown in a particular order.The
isAutoStartup()return value indicates whether this object should be started at the time of a context refresh. The callback-acceptingstop(Runnable)method is useful for objects that have an asynchronous shutdown process. Any implementation of this interface must invoke the callback'srun()method upon shutdown completion to avoid unnecessary delays in the overallApplicationContextshutdown.This interface extends
Phased, and thegetPhase()method's return value indicates the phase within which thisLifecyclecomponent should be started and stopped. The startup process begins with the lowest phase value and ends with the highest phase value (Integer.MIN_VALUEis the lowest possible, andInteger.MAX_VALUEis 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
Lifecyclecomponents within the context that do not also implementSmartLifecyclewill be treated as if they have a phase value of0. This allows aSmartLifecyclecomponent to start before thoseLifecyclecomponents if theSmartLifecyclecomponent has a negative phase value, or theSmartLifecyclecomponent may start after thoseLifecyclecomponents if theSmartLifecyclecomponent has a positive phase value.Note that, due to the auto-startup support in
SmartLifecycle, aSmartLifecyclebean instance will usually get initialized on startup of the application context in any case. As a consequence, the bean definition lazy-init flag has very limited actual effect onSmartLifecyclebeans.- 从以下版本开始:
- 3.0
- 作者:
- Mark Fisher, Juergen Hoeller, Sam Brannen
- 另请参阅:
LifecycleProcessor,ConfigurableApplicationContext
字段概要
字段 修饰符和类型 字段 说明 static intDEFAULT_PHASEThe default phase forSmartLifecycle:Integer.MAX_VALUE.
方法概要
所有方法 实例方法 默认方法 修饰符和类型 方法 说明 default intgetPhase()Return the phase that this lifecycle object is supposed to run in.default booleanisAutoStartup()Returnstrueif thisLifecyclecomponent should get started automatically by the container at the time that the containingApplicationContextgets refreshed.default voidstop(Runnable callback)Indicates that a Lifecycle component must stop if it is currently running.
字段详细资料
DEFAULT_PHASE
static final int DEFAULT_PHASE
The default phase forSmartLifecycle:Integer.MAX_VALUE.This is different from the common phase
0associated with regularLifecycleimplementations, putting the typically auto-startedSmartLifecyclebeans into a later startup phase and an earlier shutdown phase.- 从以下版本开始:
- 5.1
- 另请参阅:
getPhase(),DefaultLifecycleProcessor.getPhase(Lifecycle), 常量字段值
方法详细资料
isAutoStartup
default boolean isAutoStartup()
Returnstrueif thisLifecyclecomponent should get started automatically by the container at the time that the containingApplicationContextgets refreshed.A value of
falseindicates that the component is intended to be started through an explicitLifecycle.start()call instead, analogous to a plainLifecycleimplementation.The default implementation returns
true.
stop
default void stop(Runnable callback)
Indicates that a Lifecycle component must stop if it is currently running.The provided callback is used by the
LifecycleProcessorto support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after theSmartLifecyclecomponent does indeed stop.The
LifecycleProcessorwill call only this variant of thestopmethod; i.e.Lifecycle.stop()will not be called forSmartLifecycleimplementations unless explicitly delegated to within the implementation of this method.The default implementation delegates to
Lifecycle.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).- 另请参阅:
Lifecycle.stop(),getPhase()
getPhase
default int getPhase()
Return the phase that this lifecycle object is supposed to run in.The default implementation returns
DEFAULT_PHASEin order to letstop()callbacks execute after regularLifecycleimplementations.