Class ThreadPoolTaskScheduler
- All Implemented Interfaces:
Serializable, Executor, ThreadFactory, EventListener, Aware, BeanNameAware, DisposableBean, InitializingBean, ApplicationContextAware, ApplicationListener<ContextClosedEvent>, Lifecycle, Phased, SmartLifecycle, AsyncTaskExecutor, TaskExecutor, SchedulingTaskExecutor, TaskScheduler
TaskScheduler interface, wrapping
a native ScheduledThreadPoolExecutor and providing
all applicable configuration options for it. The default number of scheduler
threads is 1; a higher number can be configured through setPoolSize(int).
This is Spring's traditional scheduler variant, staying as close as possible to
ScheduledExecutorService semantics. Task execution happens
on the scheduler thread(s) rather than on separate execution threads. As a consequence,
a ScheduledFuture handle (for example, from schedule(Runnable, Instant))
represents the actual completion of the provided task (or series of repeated tasks).
- Since:
- 3.0
- Author:
- Juergen Hoeller, Mark Fisher
- See Also:
-
Field Summary
Fields inherited from class ExecutorConfigurationSupport
DEFAULT_PHASE, loggerFields inherited from interface AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ScheduledExecutorServicecreateExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create a newScheduledExecutorServiceinstance.voidExecute the giventask.intReturn the number of currently active threads.getClock()Return the clock to use for scheduling purposes.intReturn the current pool size.Return the underlying ScheduledExecutorService for native access.Return the underlying ScheduledThreadPoolExecutor, if available.protected ExecutorServiceinitializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Create the targetExecutorServiceinstance.booleanDeprecated.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.voidSet the clock to use for scheduling purposes.voidsetContinueExistingPeriodicTasksAfterShutdownPolicy(boolean flag) Set whether to continue existing periodic tasks even when this executor has been shutdown.voidsetErrorHandler(ErrorHandler errorHandler) Set a customErrorHandlerstrategy.voidsetExecuteExistingDelayedTasksAfterShutdownPolicy(boolean flag) Set whether to execute existing delayed tasks even when this executor has been shutdown.voidsetPoolSize(int poolSize) Set the ScheduledExecutorService's pool size.voidsetRemoveOnCancelPolicy(boolean flag) Set the remove-on-cancel mode onScheduledThreadPoolExecutor.voidsetTaskDecorator(TaskDecorator taskDecorator) Specify a customTaskDecoratorto be applied to anyRunnableabout to be executed.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.Methods inherited from class ExecutorConfigurationSupport
afterExecute, afterPropertiesSet, beforeExecute, cancelRemainingTask, destroy, getPhase, initialize, initiateEarlyShutdown, initiateShutdown, isRunning, onApplicationEvent, setAcceptTasksAfterContextClose, setApplicationContext, setAwaitTerminationMillis, setAwaitTerminationSeconds, setBeanName, setPhase, setRejectedExecutionHandler, setThreadFactory, setThreadNamePrefix, setVirtualThreads, setWaitForTasksToCompleteOnShutdown, shutdown, start, stop, stopMethods inherited from class CustomizableThreadFactory
newThreadMethods inherited from class CustomizableThreadCreator
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadPriorityMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ApplicationListener
supportsAsyncExecutionMethods inherited from interface AsyncTaskExecutor
execute, submitCompletable, submitCompletableMethods inherited from interface SchedulingTaskExecutor
prefersShortLivedTasksMethods inherited from interface SmartLifecycle
isAutoStartup, isPauseableMethods inherited from interface 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.
- See Also:
-
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:
-
setTaskDecorator
Specify a customTaskDecoratorto be applied to anyRunnableabout to be executed.Note that such a decorator is not being applied to the user-supplied
Runnable/Callablebut rather to the scheduled execution callback (a wrapper around the user-supplied task).The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.
- Since:
- 6.2
-
setErrorHandler
Set a customErrorHandlerstrategy. -
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:TaskSchedulerReturn the clock to use for scheduling purposes.- Specified by:
getClockin interfaceTaskScheduler- See Also:
-
initializeExecutor
protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) Description copied from class:ExecutorConfigurationSupportCreate the targetExecutorServiceinstance. Called byafterPropertiesSet.- Specified by:
-
getScheduledThreadPoolExecutor()access