Class ConcurrentTaskScheduler

java.lang.Object
org.springframework.scheduling.concurrent.ConcurrentTaskExecutor
org.springframework.scheduling.concurrent.ConcurrentTaskScheduler
All Implemented Interfaces:
Executor, AsyncTaskExecutor, TaskExecutor, SchedulingTaskExecutor, TaskScheduler
Direct Known Subclasses:
DefaultManagedTaskScheduler

public class ConcurrentTaskScheduler extends ConcurrentTaskExecutor implements TaskScheduler
Adapter that takes a java.util.concurrent.ScheduledExecutorService and exposes a Spring TaskScheduler for it. Extends ConcurrentTaskExecutor in order to implement the SchedulingTaskExecutor interface as well.

Autodetects a JSR-236 ManagedScheduledExecutorService in order to use it for trigger-based scheduling if possible, instead of Spring's local trigger management which ends up delegating to regular delay-based scheduling against the java.util.concurrent.ScheduledExecutorService API. For JSR-236 style lookup in a Jakarta EE environment, consider using DefaultManagedTaskScheduler.

Note that there is a pre-built ThreadPoolTaskScheduler that allows for defining a ScheduledThreadPoolExecutor in bean style, exposing it as a Spring TaskScheduler directly. This is a convenient alternative to a raw ScheduledThreadPoolExecutor definition with a separate definition of the present adapter class.

Since:
3.0
Author:
Juergen Hoeller, Mark Fisher, Arjen Poutsma
See Also:
  • Constructor Details

  • Method Details

    • setScheduledExecutor

      public void setScheduledExecutor(ScheduledExecutorService scheduledExecutor)
      Specify the ScheduledExecutorService to delegate to.

      Autodetects a JSR-236 ManagedScheduledExecutorService in order to use it for trigger-based scheduling if possible, instead of Spring's local trigger management.

      Note: This will only apply to TaskScheduler invocations. If you want the given executor to apply to SchedulingTaskExecutor invocations as well, pass the same executor reference to ConcurrentTaskExecutor.setConcurrentExecutor(Executor).

      See Also:
    • setErrorHandler

      public void setErrorHandler(ErrorHandler errorHandler)
      Provide an 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:
    • execute

      public void execute(Runnable task)
      Description copied from interface: TaskExecutor
      Execute the given task.

      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 interface Executor
      Specified by:
      execute in interface TaskExecutor
      Overrides:
      execute in class ConcurrentTaskExecutor
      Parameters:
      task - the Runnable to execute (never null)
    • submit

      public Future<?> submit(Runnable task)
      Description copied from interface: AsyncTaskExecutor
      Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.

      As of 6.1, this method comes with a default implementation that delegates to TaskExecutor.execute(Runnable).

      Specified by:
      submit in interface AsyncTaskExecutor
      Overrides:
      submit in class ConcurrentTaskExecutor
      Parameters:
      task - the Runnable to execute (never null)
      Returns:
      a Future representing pending completion of the task
    • submit

      public <T> Future<T> submit(Callable<T> task)
      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.

      As of 6.1, this method comes with a default implementation that delegates to TaskExecutor.execute(Runnable).

      Specified by:
      submit in interface AsyncTaskExecutor
      Overrides:
      submit in class ConcurrentTaskExecutor
      Parameters:
      task - the Callable to execute (never null)
      Returns:
      a Future representing pending completion of the task
    • schedule

      public @Nullable ScheduledFuture<?> schedule(Runnable task, Trigger trigger)
      Description copied from interface: TaskScheduler
      Schedule the given Runnable, 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 interface TaskScheduler
      Parameters:
      task - the Runnable to execute whenever the trigger fires
      trigger - an implementation of the Trigger interface, for example, a CronTrigger object wrapping a cron expression
      Returns:
      a ScheduledFuture representing pending execution of the task, or null if the given Trigger object never fires (i.e. returns null from Trigger.nextExecution(TriggerContext))
      See Also:
    • schedule

      public ScheduledFuture<?> schedule(Runnable task, Instant startTime)
      Description copied from interface: TaskScheduler
      Schedule the given Runnable, 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 interface TaskScheduler
      Parameters:
      task - the Runnable to execute whenever the trigger fires
      startTime - 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 execution of the task
    • scheduleAtFixedRate

      public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period)
      Description copied from interface: TaskScheduler
      Schedule the given Runnable, 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 interface TaskScheduler
      Parameters:
      task - the Runnable to execute whenever the trigger fires
      startTime - 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 execution of the task
    • scheduleAtFixedRate

      public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period)
      Description copied from interface: TaskScheduler
      Schedule the given Runnable, 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 interface TaskScheduler
      Parameters:
      task - the Runnable to execute whenever the trigger fires
      period - the interval between successive executions of the task
      Returns:
      a ScheduledFuture representing pending execution of the task
    • scheduleWithFixedDelay

      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay)
      Description copied from interface: TaskScheduler
      Schedule the given Runnable, 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 interface TaskScheduler
      Parameters:
      task - the Runnable to execute whenever the trigger fires
      startTime - 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 execution of the task
    • scheduleWithFixedDelay

      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)
      Description copied from interface: TaskScheduler
      Schedule the given Runnable, 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 interface TaskScheduler
      Parameters:
      task - the Runnable to execute whenever the trigger fires
      delay - the delay between the completion of one execution and the start of the next
      Returns:
      a ScheduledFuture representing pending execution of the task