spring-framework / org.springframework.scheduling.concurrent / ScheduledExecutorFactoryBean

ScheduledExecutorFactoryBean

open class ScheduledExecutorFactoryBean : ExecutorConfigurationSupport, FactoryBean<ScheduledExecutorService>

org.springframework.beans.factory.FactoryBean that sets up a java.util.concurrent.ScheduledExecutorService (by default: a java.util.concurrent.ScheduledThreadPoolExecutor) and exposes it for bean references.

Allows for registration of ScheduledExecutorTask, automatically starting the ScheduledExecutorService on initialization and cancelling it on destruction of the context. In scenarios that only require static registration of tasks at startup, there is no need to access the ScheduledExecutorService instance itself in application code at all; ScheduledExecutorFactoryBean is then just being used for lifecycle integration.

For an alternative, you may set up a ScheduledThreadPoolExecutor instance directly using constructor injection, or use a factory method definition that points to the java.util.concurrent.Executors class. This is strongly recommended in particular for common @Bean methods in configuration classes, where this FactoryBean variant would force you to return the FactoryBean type instead of ScheduledExecutorService.

Note that java.util.concurrent.ScheduledExecutorService uses a Runnable instance that is shared between repeated executions, in contrast to Quartz which instantiates a new Job for each execution.

WARNING: Runnable submitted via a native java.util.concurrent.ScheduledExecutorService are removed from the execution schedule once they throw an exception. If you would prefer to continue execution after such an exception, switch this FactoryBean's "continueScheduledExecutionAfterException" property to "true".

Author
Juergen Hoeller

Since
2.0

See Also
#setPoolSize#setRemoveOnCancelPolicy#setThreadFactoryScheduledExecutorTaskjava.util.concurrent.ScheduledExecutorServicejava.util.concurrent.ScheduledThreadPoolExecutor

Constructors

<init>

ScheduledExecutorFactoryBean()

org.springframework.beans.factory.FactoryBean that sets up a java.util.concurrent.ScheduledExecutorService (by default: a java.util.concurrent.ScheduledThreadPoolExecutor) and exposes it for bean references.

Allows for registration of ScheduledExecutorTask, automatically starting the ScheduledExecutorService on initialization and cancelling it on destruction of the context. In scenarios that only require static registration of tasks at startup, there is no need to access the ScheduledExecutorService instance itself in application code at all; ScheduledExecutorFactoryBean is then just being used for lifecycle integration.

For an alternative, you may set up a ScheduledThreadPoolExecutor instance directly using constructor injection, or use a factory method definition that points to the java.util.concurrent.Executors class. This is strongly recommended in particular for common @Bean methods in configuration classes, where this FactoryBean variant would force you to return the FactoryBean type instead of ScheduledExecutorService.

Note that java.util.concurrent.ScheduledExecutorService uses a Runnable instance that is shared between repeated executions, in contrast to Quartz which instantiates a new Job for each execution.

WARNING: Runnable submitted via a native java.util.concurrent.ScheduledExecutorService are removed from the execution schedule once they throw an exception. If you would prefer to continue execution after such an exception, switch this FactoryBean's "continueScheduledExecutionAfterException" property to "true".

Functions

getObject

open fun getObject(): ScheduledExecutorService

getObjectType

open fun getObjectType(): Class<out ScheduledExecutorService>

isSingleton

open fun isSingleton(): Boolean

setContinueScheduledExecutionAfterException

open fun setContinueScheduledExecutionAfterException(continueScheduledExecutionAfterException: Boolean): Unit

Specify whether to continue the execution of a scheduled task after it threw an exception.

Default is "false", matching the native behavior of a java.util.concurrent.ScheduledExecutorService. Switch this flag to "true" for exception-proof execution of each task, continuing scheduled execution as in the case of successful execution.

setExposeUnconfigurableExecutor

open fun setExposeUnconfigurableExecutor(exposeUnconfigurableExecutor: Boolean): Unit

Specify whether this FactoryBean should expose an unconfigurable decorator for the created executor.

Default is "false", exposing the raw executor as bean reference. Switch this flag to "true" to strictly prevent clients from modifying the executor's configuration.

setPoolSize

open fun setPoolSize(poolSize: Int): Unit

Set the ScheduledExecutorService's pool size. Default is 1.

setRemoveOnCancelPolicy

open fun setRemoveOnCancelPolicy(removeOnCancelPolicy: Boolean): Unit

Set the remove-on-cancel mode on ScheduledThreadPoolExecutor (JDK 7+).

Default is false. If set to true, the target executor will be switched into remove-on-cancel mode (if possible, with a soft fallback otherwise).

setScheduledExecutorTasks

open fun setScheduledExecutorTasks(vararg scheduledExecutorTasks: ScheduledExecutorTask): Unit

Register a list of ScheduledExecutorTask objects with the ScheduledExecutorService that this FactoryBean creates. Depending on each ScheduledExecutorTask's settings, it will be registered via one of ScheduledExecutorService's schedule methods.