Class ThreadPoolTaskScheduler

All Implemented Interfaces:
Serializable, Executor, ThreadFactory, EventListener, Aware, BeanNameAware, DisposableBean, InitializingBean, ApplicationContextAware, ApplicationListener<ContextClosedEvent>, Lifecycle, Phased, SmartLifecycle, AsyncTaskExecutor, TaskExecutor, SchedulingTaskExecutor, TaskScheduler

public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements AsyncTaskExecutor, SchedulingTaskExecutor, TaskScheduler
A standard implementation of Spring's 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:
  • 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 on ScheduledThreadPoolExecutor.

      Default is false. If set to true, 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 to true, 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 to false, 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

      public void setTaskDecorator(TaskDecorator taskDecorator)
      Specify a custom TaskDecorator to be applied to any Runnable about to be executed.

      Note that such a decorator is not being applied to the user-supplied Runnable/Callable but 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

      public void setErrorHandler(ErrorHandler errorHandler)
      Set a custom ErrorHandler strategy.
    • setClock

      public void setClock(Clock clock)
      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

      public Clock getClock()
      Description copied from interface: TaskScheduler
      Return the clock to use for scheduling purposes.
      Specified by:
      getClock in interface TaskScheduler
      See Also:
    • initializeExecutor

      protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)
      Description copied from class: ExecutorConfigurationSupport
      Create the target ExecutorService instance. Called by afterPropertiesSet.
      Specified by: