org.springframework.context.support
Class DefaultLifecycleProcessor

java.lang.Object
  extended by org.springframework.context.support.DefaultLifecycleProcessor
All Implemented Interfaces:
Aware, BeanFactoryAware, Lifecycle, LifecycleProcessor

public class DefaultLifecycleProcessor
extends java.lang.Object
implements LifecycleProcessor, BeanFactoryAware

Default implementation of the LifecycleProcessor strategy.

Since:
3.0
Author:
Mark Fisher, Juergen Hoeller

Nested Class Summary
private  class DefaultLifecycleProcessor.LifecycleGroup
          Helper class for maintaining a group of Lifecycle beans that should be started and stopped together based on their 'phase' value (or the default value of 0).
private  class DefaultLifecycleProcessor.LifecycleGroupMember
          Adapts the Comparable interface onto the lifecycle phase model.
 
Field Summary
private  ConfigurableListableBeanFactory beanFactory
           
private  Log logger
           
private  boolean running
           
private  long timeoutPerShutdownPhase
           
 
Constructor Summary
DefaultLifecycleProcessor()
           
 
Method Summary
private  void doStart(java.util.Map<java.lang.String,? extends Lifecycle> lifecycleBeans, java.lang.String beanName)
          Start the specified bean as part of the given set of Lifecycle beans, making sure that any beans that it depends on are started first.
private  void doStop(java.util.Map<java.lang.String,? extends Lifecycle> lifecycleBeans, java.lang.String beanName, java.util.concurrent.CountDownLatch latch, java.util.Set<java.lang.String> countDownBeanNames)
          Stop the specified bean as part of the given set of Lifecycle beans, making sure that any beans that depends on it are stopped first.
protected  java.util.Map<java.lang.String,Lifecycle> getLifecycleBeans()
          Retrieve all applicable Lifecycle beans: all singletons that have already been created, as well as all SmartLifecycle beans (even if they are marked as lazy-init).
protected  int getPhase(Lifecycle bean)
          Determine the lifecycle phase of the given bean.
 boolean isRunning()
          Check whether this component is currently running.
 void onClose()
          Notification of context close phase, e.g.
 void onRefresh()
          Notification of context refresh, e.g.
 void setBeanFactory(BeanFactory beanFactory)
          Callback that supplies the owning factory to a bean instance.
 void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase)
          Specify the maximum time allotted in milliseconds for the shutdown of any phase (group of SmartLifecycle beans with the same 'phase' value).
 void start()
          Start all registered beans that implement Lifecycle and are not already running.
private  void startBeans(boolean autoStartupOnly)
           
 void stop()
          Stop all registered beans that implement Lifecycle and are currently running.
private  void stopBeans()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private final Log logger

timeoutPerShutdownPhase

private volatile long timeoutPerShutdownPhase

running

private volatile boolean running

beanFactory

private volatile ConfigurableListableBeanFactory beanFactory
Constructor Detail

DefaultLifecycleProcessor

public DefaultLifecycleProcessor()
Method Detail

setTimeoutPerShutdownPhase

public void setTimeoutPerShutdownPhase(long timeoutPerShutdownPhase)
Specify the maximum time allotted in milliseconds for the shutdown of any phase (group of SmartLifecycle beans with the same 'phase' value). The default value is 30 seconds.


setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean.afterPropertiesSet() or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (never null). The bean can immediately call methods on the factory.
See Also:
BeanInitializationException

start

public void start()
Start all registered beans that implement Lifecycle and are not already running. Any bean that implements SmartLifecycle will be started within its 'phase', and all phases will be ordered from lowest to highest value. All beans that do not implement SmartLifecycle will be started in the default phase 0. A bean declared as a dependency of another bean will be started before the dependent bean regardless of the declared phase.

Specified by:
start in interface Lifecycle

stop

public void stop()
Stop all registered beans that implement Lifecycle and are currently running. Any bean that implements SmartLifecycle will be stopped within its 'phase', and all phases will be ordered from highest to lowest value. All beans that do not implement SmartLifecycle will be stopped in the default phase 0. A bean declared as dependent on another bean will be stopped before the dependency bean regardless of the declared phase.

Specified by:
stop in interface Lifecycle
See Also:
SmartLifecycle.stop(Runnable)

onRefresh

public void onRefresh()
Description copied from interface: LifecycleProcessor
Notification of context refresh, e.g. for auto-starting components.

Specified by:
onRefresh in interface LifecycleProcessor

onClose

public void onClose()
Description copied from interface: LifecycleProcessor
Notification of context close phase, e.g. for auto-stopping components.

Specified by:
onClose in interface LifecycleProcessor

isRunning

public boolean isRunning()
Description copied from interface: Lifecycle
Check whether this component is currently running.

In the case of a container, this will return true only if all components that apply are currently running.

Specified by:
isRunning in interface Lifecycle
Returns:
whether the component is currently running

startBeans

private void startBeans(boolean autoStartupOnly)

doStart

private void doStart(java.util.Map<java.lang.String,? extends Lifecycle> lifecycleBeans,
                     java.lang.String beanName)
Start the specified bean as part of the given set of Lifecycle beans, making sure that any beans that it depends on are started first.

Parameters:
lifecycleBeans - Map with bean name as key and Lifecycle instance as value
beanName - the name of the bean to start

stopBeans

private void stopBeans()

doStop

private void doStop(java.util.Map<java.lang.String,? extends Lifecycle> lifecycleBeans,
                    java.lang.String beanName,
                    java.util.concurrent.CountDownLatch latch,
                    java.util.Set<java.lang.String> countDownBeanNames)
Stop the specified bean as part of the given set of Lifecycle beans, making sure that any beans that depends on it are stopped first.

Parameters:
lifecycleBeans - Map with bean name as key and Lifecycle instance as value
beanName - the name of the bean to stop

getLifecycleBeans

protected java.util.Map<java.lang.String,Lifecycle> getLifecycleBeans()
Retrieve all applicable Lifecycle beans: all singletons that have already been created, as well as all SmartLifecycle beans (even if they are marked as lazy-init).

Returns:
the Map of applicable beans, with bean names as keys and bean instances as values

getPhase

protected int getPhase(Lifecycle bean)
Determine the lifecycle phase of the given bean.

The default implementation checks for the Phased interface. Can be overridden to apply other/further policies.

Parameters:
bean - the bean to introspect
Returns:
the phase an an integer value. The suggested default is 0.
See Also:
Phased, SmartLifecycle