Class ThreadPoolTaskScheduler
- All Implemented Interfaces:
Serializable
,Executor
,ThreadFactory
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,AsyncListenableTaskExecutor
,AsyncTaskExecutor
,TaskExecutor
,SchedulingTaskExecutor
,TaskScheduler
TaskScheduler
interface, wrapping
a native ScheduledThreadPoolExecutor
and providing
all applicable configuration options for it.- Since:
- 3.0
- Author:
- Juergen Hoeller, Mark Fisher
- See Also:
-
setPoolSize(int)
setRemoveOnCancelPolicy(boolean)
setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean)
setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean)
ExecutorConfigurationSupport.setThreadFactory(java.util.concurrent.ThreadFactory)
setErrorHandler(org.springframework.util.ErrorHandler)
- Serialized Form
-
Field Summary
Fields inherited from class org.springframework.scheduling.concurrent.ExecutorConfigurationSupport
logger
Fields inherited from interface org.springframework.core.task.AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
cancelRemainingTask
(Runnable task) Cancel the given remaining task which never commenced execution, as returned fromExecutorService.shutdownNow()
.protected ScheduledExecutorService
createExecutor
(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create a newScheduledExecutorService
instance.void
Execute the giventask
.void
Deprecated.int
Return the number of currently active threads.getClock()
Return the clock to use for scheduling purposes.int
Return the current pool size.Return the underlying ScheduledExecutorService for native access.Return the underlying ScheduledThreadPoolExecutor, if available.protected ExecutorService
initializeExecutor
(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create the targetExecutorService
instance.boolean
Deprecated.Schedule the givenRunnable
, invoking it at the specified execution time.Schedule the givenRunnable
, invoking it whenever the trigger indicates a next execution time.scheduleAtFixedRate
(Runnable task, Duration period) Schedule the givenRunnable
, starting as soon as possible and invoking it with the given period.scheduleAtFixedRate
(Runnable task, Instant startTime, Duration period) Schedule the givenRunnable
, invoking it at the specified execution time and subsequently with the given period.scheduleWithFixedDelay
(Runnable task, Duration delay) Schedule the givenRunnable
, starting as soon as possible and invoking it with the given delay between the completion of one execution and the start of the next.scheduleWithFixedDelay
(Runnable task, Instant startTime, Duration delay) Schedule the givenRunnable
, invoking it at the specified execution time and subsequently with the given delay between the completion of one execution and the start of the next.void
Set the clock to use for scheduling purposes.void
setContinueExistingPeriodicTasksAfterShutdownPolicy
(boolean flag) Set whether to continue existing periodic tasks even when this executor has been shutdown.void
setErrorHandler
(ErrorHandler errorHandler) Set a customErrorHandler
strategy.void
setExecuteExistingDelayedTasksAfterShutdownPolicy
(boolean flag) Set whether to execute existing delayed tasks even when this executor has been shutdown.void
setPoolSize
(int poolSize) Set the ScheduledExecutorService's pool size.void
setRemoveOnCancelPolicy
(boolean flag) Set the remove-on-cancel mode onScheduledThreadPoolExecutor
.Future<?>
Submit a Runnable task for execution, receiving a Future representing that task.<T> Future<T>
Submit a Callable task for execution, receiving a Future representing that task.submitListenable
(Runnable task) Submit aRunnable
task for execution, receiving aListenableFuture
representing that task.<T> ListenableFuture<T>
submitListenable
(Callable<T> task) Submit aCallable
task for execution, receiving aListenableFuture
representing that task.Methods inherited from class org.springframework.scheduling.concurrent.ExecutorConfigurationSupport
afterPropertiesSet, destroy, initialize, setAwaitTerminationMillis, setAwaitTerminationSeconds, setBeanName, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setWaitForTasksToCompleteOnShutdown, shutdown
Methods inherited from class org.springframework.scheduling.concurrent.CustomizableThreadFactory
newThread
Methods inherited from class org.springframework.util.CustomizableThreadCreator
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriority
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.task.AsyncTaskExecutor
submitCompletable, submitCompletable
Methods inherited from interface org.springframework.scheduling.SchedulingTaskExecutor
prefersShortLivedTasks
Methods inherited from interface org.springframework.scheduling.TaskScheduler
schedule, scheduleAtFixedRate, scheduleAtFixedRate, scheduleWithFixedDelay, scheduleWithFixedDelay
-
Constructor Details
-
ThreadPoolTaskScheduler
public ThreadPoolTaskScheduler()
-
-
Method Details
-
setPoolSize
public void setPoolSize(int poolSize) Set the ScheduledExecutorService's pool size. Default is 1.This setting can be modified at runtime, for example through JMX.
-
setRemoveOnCancelPolicy
public void setRemoveOnCancelPolicy(boolean flag) Set the remove-on-cancel mode onScheduledThreadPoolExecutor
.Default is
false
. If set totrue
, the target executor will be switched into remove-on-cancel mode (if possible).This setting can be modified at runtime, for example through JMX.
-
setContinueExistingPeriodicTasksAfterShutdownPolicy
public void setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean flag) Set whether to continue existing periodic tasks even when this executor has been shutdown.Default is
false
. If set totrue
, the target executor will be switched into continuing periodic tasks (if possible).This setting can be modified at runtime, for example through JMX.
- Since:
- 5.3.9
- See Also:
-
setExecuteExistingDelayedTasksAfterShutdownPolicy
public void setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean flag) Set whether to execute existing delayed tasks even when this executor has been shutdown.Default is
true
. If set tofalse
, the target executor will be switched into dropping remaining tasks (if possible).This setting can be modified at runtime, for example through JMX.
- Since:
- 5.3.9
- See Also:
-
setErrorHandler
Set a customErrorHandler
strategy. -
setClock
Set the clock to use for scheduling purposes.The default clock is the system clock for the default time zone.
- Since:
- 5.3
- See Also:
-
getClock
Description copied from interface:TaskScheduler
Return the clock to use for scheduling purposes.- Specified by:
getClock
in interfaceTaskScheduler
- See Also:
-
initializeExecutor
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Description copied from class:ExecutorConfigurationSupport
Create the targetExecutorService
instance. Called byafterPropertiesSet
.- Specified by:
initializeExecutor
in classExecutorConfigurationSupport
- Parameters:
threadFactory
- the ThreadFactory to userejectedExecutionHandler
- the RejectedExecutionHandler to use- Returns:
- a new ExecutorService instance
- See Also:
-
createExecutor
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create a newScheduledExecutorService
instance.The default implementation creates a
ScheduledThreadPoolExecutor
. Can be overridden in subclasses to provide customScheduledExecutorService
instances.- Parameters:
poolSize
- the specified pool sizethreadFactory
- the ThreadFactory to userejectedExecutionHandler
- the RejectedExecutionHandler to use- Returns:
- a new ScheduledExecutorService instance
- See Also:
-
getScheduledExecutor
Return the underlying ScheduledExecutorService for native access.- Returns:
- the underlying ScheduledExecutorService (never
null
) - Throws:
IllegalStateException
- if the ThreadPoolTaskScheduler hasn't been initialized yet
-
getScheduledThreadPoolExecutor
Return the underlying ScheduledThreadPoolExecutor, if available.- Returns:
- the underlying ScheduledExecutorService (never
null
) - Throws:
IllegalStateException
- if the ThreadPoolTaskScheduler hasn't been initialized yet or if the underlying ScheduledExecutorService isn't a ScheduledThreadPoolExecutor- See Also:
-
getPoolSize
public int getPoolSize()Return the current pool size.Requires an underlying
ScheduledThreadPoolExecutor
. -
getActiveCount
public int getActiveCount()Return the number of currently active threads.Requires an underlying
ScheduledThreadPoolExecutor
. -
isRemoveOnCancelPolicy
Deprecated.as of 5.3.9, in favor of directgetScheduledThreadPoolExecutor()
accessReturn the current setting for the remove-on-cancel mode.Requires an underlying
ScheduledThreadPoolExecutor
. -
execute
Description copied from interface:TaskExecutor
Execute the giventask
.The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.
- Specified by:
execute
in interfaceExecutor
- Specified by:
execute
in interfaceTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)
-
execute
Deprecated.Description copied from interface:AsyncTaskExecutor
Execute the giventask
.- Specified by:
execute
in interfaceAsyncTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)startTimeout
- the time duration (milliseconds) within which the task is supposed to start. This is intended as a hint to the executor, allowing for preferred handling of immediate tasks. Typical values areAsyncTaskExecutor.TIMEOUT_IMMEDIATE
orAsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used byTaskExecutor.execute(Runnable)
).- See Also:
-
submit
Description copied from interface:AsyncTaskExecutor
Submit a Runnable task for execution, receiving a Future representing that task. The Future will return anull
result upon completion.- Specified by:
submit
in interfaceAsyncTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- Returns:
- a Future representing pending completion of the task
-
submit
Description copied from interface:AsyncTaskExecutor
Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.- Specified by:
submit
in interfaceAsyncTaskExecutor
- Parameters:
task
- theCallable
to execute (nevernull
)- Returns:
- a Future representing pending completion of the task
-
submitListenable
Description copied from interface:AsyncListenableTaskExecutor
Submit aRunnable
task for execution, receiving aListenableFuture
representing that task. The Future will return anull
result upon completion.- Specified by:
submitListenable
in interfaceAsyncListenableTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- Returns:
- a
ListenableFuture
representing pending completion of the task
-
submitListenable
Description copied from interface:AsyncListenableTaskExecutor
Submit aCallable
task for execution, receiving aListenableFuture
representing that task. The Future will return the Callable's result upon completion.- Specified by:
submitListenable
in interfaceAsyncListenableTaskExecutor
- Parameters:
task
- theCallable
to execute (nevernull
)- Returns:
- a
ListenableFuture
representing pending completion of the task
-
cancelRemainingTask
Description copied from class:ExecutorConfigurationSupport
Cancel the given remaining task which never commenced execution, as returned fromExecutorService.shutdownNow()
.- Overrides:
cancelRemainingTask
in classExecutorConfigurationSupport
- Parameters:
task
- the task to cancel (typically aRunnableFuture
)- See Also:
-
schedule
Description copied from interface:TaskScheduler
Schedule the givenRunnable
, invoking it whenever the trigger indicates a next execution time.Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.- Specified by:
schedule
in interfaceTaskScheduler
- Parameters:
task
- the Runnable to execute whenever the trigger firestrigger
- an implementation of theTrigger
interface, e.g. aCronTrigger
object wrapping a cron expression- Returns:
- a
ScheduledFuture
representing pending completion of the task, ornull
if the given Trigger object never fires (i.e. returnsnull
fromTrigger.nextExecution(org.springframework.scheduling.TriggerContext)
) - See Also:
-
schedule
Description copied from interface:TaskScheduler
Schedule the givenRunnable
, invoking it at the specified execution time.Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.- Specified by:
schedule
in interfaceTaskScheduler
- Parameters:
task
- the Runnable to execute whenever the trigger firesstartTime
- the desired execution time for the task (if this is in the past, the task will be executed immediately, i.e. as soon as possible)- Returns:
- a
ScheduledFuture
representing pending completion of the task
-
scheduleAtFixedRate
Description copied from interface:TaskScheduler
Schedule the givenRunnable
, invoking it at the specified execution time and subsequently with the given period.Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.- Specified by:
scheduleAtFixedRate
in interfaceTaskScheduler
- Parameters:
task
- the Runnable to execute whenever the trigger firesstartTime
- the desired first execution time for the task (if this is in the past, the task will be executed immediately, i.e. as soon as possible)period
- the interval between successive executions of the task- Returns:
- a
ScheduledFuture
representing pending completion of the task
-
scheduleAtFixedRate
Description copied from interface:TaskScheduler
Schedule the givenRunnable
, starting as soon as possible and invoking it with the given period.Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.- Specified by:
scheduleAtFixedRate
in interfaceTaskScheduler
- Parameters:
task
- the Runnable to execute whenever the trigger firesperiod
- the interval between successive executions of the task- Returns:
- a
ScheduledFuture
representing pending completion of the task
-
scheduleWithFixedDelay
Description copied from interface:TaskScheduler
Schedule the givenRunnable
, invoking it at the specified execution time and subsequently with the given delay between the completion of one execution and the start of the next.Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.- Specified by:
scheduleWithFixedDelay
in interfaceTaskScheduler
- Parameters:
task
- the Runnable to execute whenever the trigger firesstartTime
- the desired first execution time for the task (if this is in the past, the task will be executed immediately, i.e. as soon as possible)delay
- the delay between the completion of one execution and the start of the next- Returns:
- a
ScheduledFuture
representing pending completion of the task
-
scheduleWithFixedDelay
Description copied from interface:TaskScheduler
Schedule the givenRunnable
, starting as soon as possible and invoking it with the given delay between the completion of one execution and the start of the next.Execution will end once the scheduler shuts down or the returned
ScheduledFuture
gets cancelled.- Specified by:
scheduleWithFixedDelay
in interfaceTaskScheduler
- Parameters:
task
- the Runnable to execute whenever the trigger firesdelay
- the delay between the completion of one execution and the start of the next- Returns:
- a
ScheduledFuture
representing pending completion of the task
-
getScheduledThreadPoolExecutor()
access