Package org.springframework.context.support

# Class DefaultLifecycleProcessor

java.lang.Object
org.springframework.context.support.DefaultLifecycleProcessor

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanFactoryAware`, `Lifecycle`, `LifecycleProcessor`

public class DefaultLifecycleProcessor extends Object implements LifecycleProcessor, org.springframework.beans.factory.BeanFactoryAware
Spring's default implementation of the `LifecycleProcessor` strategy.

Provides interaction with `Lifecycle` and `SmartLifecycle` beans in groups for specific phases, on startup/shutdown as well as for explicit start/stop interactions on a `ConfigurableApplicationContext`.

As of 6.1, this also includes support for JVM checkpoint/restore (Project CRaC) when the `org.crac:crac` dependency is on the classpath. All running beans will get stopped and restarted according to the CRaC checkpoint/restore callbacks.

As of 6.2, this processor can be configured with custom timeouts for specific shutdown phases, applied to `SmartLifecycle.stop(Runnable)` implementations. As of 6.2.6, there is also support for the concurrent startup of specific phases with individual timeouts, triggering the `Lifecycle.start()` callbacks of all associated beans asynchronously and then waiting for all of them to return, as an alternative to the default sequential startup of beans without a timeout.

Since:
  3.0
Author:
  Mark Fisher, Juergen Hoeller, Sebastien Deleuze
See Also:
  - `SmartLifecycle.getPhase()`
  - `setConcurrentStartupForPhase(int, long)`
  - `setTimeoutForShutdownPhase(int, long)`

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`CHECKPOINT_PROPERTY_NAME`
Property name for a common context checkpoint: "spring.context.checkpoint".
`static final String`
`EXIT_PROPERTY_NAME`
Property name for terminating the JVM when the context reaches a specific phase: "spring.context.exit".
`static final String`
`ON_REFRESH_VALUE`
Recognized value for the context checkpoint and exit properties: "onRefresh".

## Constructor Summary

Constructors
Constructor
Description
`DefaultLifecycleProcessor()`

## Method Summary

Modifier and Type
Method
Description
`protected Map<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, for example, for auto-stopping components.
`void`
`onRefresh()`
Notification of context refresh, for example, for auto-starting components.
`void`
`setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)`
`void`
`setConcurrentStartupForPhase(int phase, long timeout)`
Switch to concurrent startup for a specific phase (group of `SmartLifecycle` beans with the same 'phase' value) with a corresponding timeout.
`void`
`setConcurrentStartupForPhases(Map<Integer,Long> phasesWithTimeouts)`
Switch to concurrent startup for each given phase (group of `SmartLifecycle` beans with the same 'phase' value) with corresponding timeouts.
`void`
`setTimeoutForShutdownPhase(int phase, long timeout)`
Specify the maximum time allotted for the shutdown of a specific phase (group of `SmartLifecycle` beans with the same 'phase' value).
`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`
`setTimeoutsForShutdownPhases(Map<Integer,Long> phasesWithTimeouts)`
Specify the maximum time allotted for the shutdown of each given phase (group of `SmartLifecycle` beans with the same 'phase' value).
`void`
`start()`
Start all registered beans that implement `Lifecycle` and are not already running.
`void`
`stop()`
Stop all registered beans that implement `Lifecycle` and are currently running.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### CHECKPOINT_PROPERTY_NAME

public static final String CHECKPOINT_PROPERTY_NAME
Property name for a common context checkpoint: "spring.context.checkpoint".

Since:
  6.1
See Also:
  - `ON_REFRESH_VALUE`
  - `Core.checkpointRestore()`
  - Constant Field Values

### EXIT_PROPERTY_NAME

public static final String EXIT_PROPERTY_NAME
Property name for terminating the JVM when the context reaches a specific phase: "spring.context.exit".

Since:
  6.1
See Also:
  - `ON_REFRESH_VALUE`
  - Constant Field Values

### ON_REFRESH_VALUE

public static final String ON_REFRESH_VALUE
Recognized value for the context checkpoint and exit properties: "onRefresh".

Since:
  6.1
See Also:
  - `CHECKPOINT_PROPERTY_NAME`
  - `EXIT_PROPERTY_NAME`
  - Constant Field Values

## Constructor Details

### DefaultLifecycleProcessor

public DefaultLifecycleProcessor()

## Method Details

### setConcurrentStartupForPhases

public void setConcurrentStartupForPhases(Map<Integer,Long> phasesWithTimeouts)
Switch to concurrent startup for each given phase (group of `SmartLifecycle` beans with the same 'phase' value) with corresponding timeouts.

Note: By default, the startup for every phase will be sequential without a timeout. Calling this setter with timeouts for the given phases switches to a mode where the beans in these phases will be started concurrently, cancelling the startup if the corresponding timeout is not met for any of these phases.

For an actual concurrent startup, a bootstrap `Executor` needs to be set for the application context, typically through a "bootstrapExecutor" bean.

Parameters:
  `phasesWithTimeouts` - a map of phase values (matching `SmartLifecycle.getPhase()`) and corresponding timeout values (in milliseconds)
Since:
  6.2.6
See Also:
  - `SmartLifecycle.getPhase()`
  - `ConfigurableBeanFactory.getBootstrapExecutor()`

### setConcurrentStartupForPhase

public void setConcurrentStartupForPhase(int phase, long timeout)
Switch to concurrent startup for a specific phase (group of `SmartLifecycle` beans with the same 'phase' value) with a corresponding timeout.

Note: By default, the startup for every phase will be sequential without a timeout. Calling this setter with a timeout for the given phase switches to a mode where the beans in this phase will be started concurrently, cancelling the startup if the corresponding timeout is not met for this phase.

For an actual concurrent startup, a bootstrap `Executor` needs to be set for the application context, typically through a "bootstrapExecutor" bean.

Parameters:
  `phase` - the phase value (matching `SmartLifecycle.getPhase()`)
  `timeout` - the corresponding timeout value (in milliseconds)
Since:
  6.2.6
See Also:
  - `SmartLifecycle.getPhase()`
  - `ConfigurableBeanFactory.getBootstrapExecutor()`

### setTimeoutsForShutdownPhases

public void setTimeoutsForShutdownPhases(Map<Integer,Long> phasesWithTimeouts)
Specify the maximum time allotted for the shutdown of each given phase (group of `SmartLifecycle` beans with the same 'phase' value).

In case of no specific timeout configured, the default timeout per shutdown phase will apply: 10000 milliseconds (10 seconds) as of 6.2.

Parameters:
  `phasesWithTimeouts` - a map of phase values (matching `SmartLifecycle.getPhase()`) and corresponding timeout values (in milliseconds)
Since:
  6.2
See Also:
  - `SmartLifecycle.getPhase()`
  - `setTimeoutPerShutdownPhase(long)`

### setTimeoutForShutdownPhase

public void setTimeoutForShutdownPhase(int phase, long timeout)
Specify the maximum time allotted for the shutdown of a specific phase (group of `SmartLifecycle` beans with the same 'phase' value).

In case of no specific timeout configured, the default timeout per shutdown phase will apply: 10000 milliseconds (10 seconds) as of 6.2.

Parameters:
  `phase` - the phase value (matching `SmartLifecycle.getPhase()`)
  `timeout` - the corresponding timeout value (in milliseconds)
Since:
  6.2
See Also:
  - `SmartLifecycle.getPhase()`
  - `setTimeoutPerShutdownPhase(long)`

### 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 10000 milliseconds (10 seconds) as of 6.2.

See Also:
  - `SmartLifecycle.getPhase()`

### setBeanFactory

public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)

Specified by:
  `setBeanFactory` in interface `org.springframework.beans.factory.BeanFactoryAware`

### 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`
See Also:
  - `SmartLifecycle.isAutoStartup()`

### 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)`
  - `DisposableBean.destroy()`

### onRefresh

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

Specified by:
  `onRefresh` in interface `LifecycleProcessor`

### onClose

public void onClose()
Description copied from interface: `LifecycleProcessor`
Notification of context close phase, for example, 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

### getLifecycleBeans

protected Map<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, using a default of 0 otherwise. Can be overridden to apply other/further policies.

Parameters:
  `bean` - the bean to introspect
Returns:
  the phase (an integer value)
See Also:
  - `Phased.getPhase()`
  - `SmartLifecycle`