Class ScheduledExecutorFactoryBean

All Implemented Interfaces:
Serializable, ThreadFactory, EventListener, Aware, BeanNameAware, DisposableBean, FactoryBean<ScheduledExecutorService>, InitializingBean, ApplicationContextAware, ApplicationListener<ContextClosedEvent>, Lifecycle, Phased, SmartLifecycle

public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport implements FactoryBean<ScheduledExecutorService>
FactoryBean that sets up a ScheduledExecutorService (by default: a ScheduledThreadPoolExecutor) and exposes it for bean references.

Allows for registration of ScheduledExecutorTasks, automatically starting the ScheduledExecutorService on initialization and canceling 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 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 ScheduledExecutorService uses a Runnable instance that is shared between repeated executions, in contrast to Quartz which instantiates a new Job for each execution.

WARNING: Runnables submitted via a native 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".

Since:
2.0
Author:
Juergen Hoeller
See Also: