Top Description Fields Constructors Methods
org.springframework.scheduling.concurrent

public Class ScheduledExecutorFactoryBean

extends ExecutorConfigurationSupport
implements FactoryBean<ScheduledExecutorService>
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.FactoryBean
Annotations
@SuppressWarnings:serial
Imports
java.util.concurrent.ExecutorService, .Executors, .RejectedExecutionHandler, .ScheduledExecutorService, .ScheduledThreadPoolExecutor, .ThreadFactory, org.springframework.beans.factory.FactoryBean, org.springframework.lang.Nullable, org.springframework.scheduling.support.DelegatingErrorHandlingRunnable, .TaskUtils, org.springframework.util.Assert, .ObjectUtils

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 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 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

Runnables 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, setThreadFactory, ScheduledExecutorTask, java.util.concurrent.ScheduledExecutorService, java.util.concurrent.ScheduledThreadPoolExecutor

Field Summary

Modifier and TypeField and Description
private boolean
private ScheduledExecutorService
private boolean
private int
private boolean
private ScheduledExecutorTask[]
Inherited from org.springframework.scheduling.concurrent.ExecutorConfigurationSupport:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ScheduledExecutorService

Returns:

a new ScheduledExecutorService instance
createExecutor
(int
the specified pool size
poolSize
,
ThreadFactory
the ThreadFactory to use
threadFactory
,
RejectedExecutionHandler
the RejectedExecutionHandler to use
rejectedExecutionHandler
)

Create a new ScheduledExecutorService instance.

public ScheduledExecutorService
getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.
public Class<? extends ScheduledExecutorService>
getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.
protected Runnable

Returns:

the actual Runnable to schedule (may be a decorator)
getRunnableToSchedule
(ScheduledExecutorTask
the ScheduledExecutorTask to schedule
task
)

Determine the actual Runnable to schedule for the given task.

protected ExecutorService
initializeExecutor(ThreadFactory
the ThreadFactory to use
threadFactory
,
RejectedExecutionHandler
the RejectedExecutionHandler to use
rejectedExecutionHandler
)

Implements abstract org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.initializeExecutor.

Create the target java.util.concurrent.ExecutorService instance.
public boolean
isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton?
protected void
registerTasks(ScheduledExecutorTask[]
the specified ScheduledExecutorTasks (never empty)
tasks
,
ScheduledExecutorService
the ScheduledExecutorService to register the tasks on.
executor
)

Register the specified ScheduledExecutorTasks on the given ScheduledExecutorService.

public void
setContinueScheduledExecutionAfterException(boolean continueScheduledExecutionAfterException)

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

public void
setExposeUnconfigurableExecutor(boolean exposeUnconfigurableExecutor)

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

public void
setPoolSize(int poolSize)

Set the ScheduledExecutorService's pool size.

public void
setRemoveOnCancelPolicy(boolean removeOnCancelPolicy)

Set the remove-on-cancel mode on ScheduledThreadPoolExecutor.

public void
setScheduledExecutorTasks(ScheduledExecutorTask... scheduledExecutorTasks)

Register a list of ScheduledExecutorTask objects with the ScheduledExecutorService that this FactoryBean creates.

Inherited from org.springframework.scheduling.concurrent.ExecutorConfigurationSupport:
afterExecuteafterPropertiesSetbeforeExecutecancelRemainingTaskdestroygetPhaseinitializeinitiateEarlyShutdowninitiateShutdownisRunningonApplicationEventsetAcceptTasksAfterContextClosesetApplicationContextsetAwaitTerminationMillissetAwaitTerminationSecondssetBeanNamesetPhasesetRejectedExecutionHandlersetThreadFactorysetThreadNamePrefixsetWaitForTasksToCompleteOnShutdownshutdownstartstopstop

Field Detail

continueScheduledExecutionAfterExceptionback to summary
private boolean continueScheduledExecutionAfterException
exposedExecutorback to summary
private ScheduledExecutorService exposedExecutor
Annotations
@Nullable
exposeUnconfigurableExecutorback to summary
private boolean exposeUnconfigurableExecutor
poolSizeback to summary
private int poolSize
removeOnCancelPolicyback to summary
private boolean removeOnCancelPolicy
scheduledExecutorTasksback to summary
private ScheduledExecutorTask[] scheduledExecutorTasks
Annotations
@Nullable

Constructor Detail

ScheduledExecutorFactoryBeanback to summary
public ScheduledExecutorFactoryBean()

Method Detail

createExecutorback to summary
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)

Create a new ScheduledExecutorService instance.

The default implementation creates a ScheduledThreadPoolExecutor. Can be overridden in subclasses to provide custom ScheduledExecutorService instances.

Parameters
poolSize:int

the specified pool size

threadFactory:ThreadFactory

the ThreadFactory to use

rejectedExecutionHandler:RejectedExecutionHandler

the RejectedExecutionHandler to use

Returns:ScheduledExecutorService

a new ScheduledExecutorService instance

See Also
afterPropertiesSet(), java.util.concurrent.ScheduledThreadPoolExecutor
getObjectback to summary
public ScheduledExecutorService getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Doc from org.springframework.beans.factory.FactoryBean.getObject.

Return an instance (possibly shared or independent) of the object managed by this factory.

As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding FactoryBeanNotInitializedException.

As of Spring 2.0, FactoryBeans are allowed to return null objects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.

Returns:ScheduledExecutorService

an instance of the bean (can be null)

Annotations
@Override
@Nullable
getObjectTypeback to summary
public Class<? extends ScheduledExecutorService> getObjectType()

Implements org.springframework.beans.factory.FactoryBean.getObjectType.

Doc from org.springframework.beans.factory.FactoryBean.getObjectType.

Return the type of object that this FactoryBean creates, or null if not known in advance.

This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

Note

Autowiring will simply ignore FactoryBeans that return null here. Therefore, it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Returns:Class<? extends ScheduledExecutorService>

the type of object that this FactoryBean creates, or null if not known at the time of the call

Annotations
@Override
getRunnableToScheduleback to summary
protected Runnable getRunnableToSchedule(ScheduledExecutorTask task)

Determine the actual Runnable to schedule for the given task.

Wraps the task's Runnable in a org.springframework.scheduling.support.DelegatingErrorHandlingRunnable that will catch and log the Exception. If necessary, it will suppress the Exception according to the "continueScheduledExecutionAfterException" flag.

Parameters
task:ScheduledExecutorTask

the ScheduledExecutorTask to schedule

Returns:Runnable

the actual Runnable to schedule (may be a decorator)

initializeExecutorback to summary
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)

Implements abstract org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.initializeExecutor.

Doc from org.springframework.scheduling.concurrent.ExecutorConfigurationSupport.initializeExecutor.

Create the target java.util.concurrent.ExecutorService instance. Called by afterPropertiesSet.

Parameters
threadFactory:ThreadFactory

the ThreadFactory to use

rejectedExecutionHandler:RejectedExecutionHandler

the RejectedExecutionHandler to use

Returns:ExecutorService

a new ExecutorService instance

Annotations
@Override
isSingletonback to summary
public boolean isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Doc from org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton? That is, will getObject() always return the same object (a reference that can be cached)?

Note

If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

Note

This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean#isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

The default implementation returns true, since a FactoryBean typically manages a singleton instance.

Returns:boolean

whether the exposed object is a singleton

Annotations
@Override
registerTasksback to summary
protected void registerTasks(ScheduledExecutorTask[] tasks, ScheduledExecutorService executor)

Register the specified ScheduledExecutorTasks on the given ScheduledExecutorService.

Parameters
tasks:ScheduledExecutorTask[]

the specified ScheduledExecutorTasks (never empty)

executor:ScheduledExecutorService

the ScheduledExecutorService to register the tasks on.

setContinueScheduledExecutionAfterExceptionback to summary
public void setContinueScheduledExecutionAfterException(boolean continueScheduledExecutionAfterException)

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.

See Also
java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate
setExposeUnconfigurableExecutorback to summary
public void setExposeUnconfigurableExecutor(boolean exposeUnconfigurableExecutor)

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.

See Also
java.util.concurrent.Executors#unconfigurableScheduledExecutorService
setPoolSizeback to summary
public void setPoolSize(int poolSize)

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

setRemoveOnCancelPolicyback to summary
public void setRemoveOnCancelPolicy(boolean removeOnCancelPolicy)

Set the remove-on-cancel mode on ScheduledThreadPoolExecutor.

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).

setScheduledExecutorTasksback to summary
public void setScheduledExecutorTasks(ScheduledExecutorTask... scheduledExecutorTasks)

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.

See Also
java.util.concurrent.ScheduledExecutorService#schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit), java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit), java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)