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

public Class ConcurrentTaskScheduler

extends ConcurrentTaskExecutor
implements TaskScheduler
Class Inheritance
All Implemented Interfaces
org.springframework.scheduling.TaskScheduler
Known Direct Subclasses
org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler
Imports
java.time.Clock, .Duration, .Instant, java.util.Date, java.util.concurrent.Executor, .Executors, .RejectedExecutionException, .ScheduledExecutorService, .ScheduledFuture, .TimeUnit, jakarta.enterprise.concurrent.LastExecution, .ManagedScheduledExecutorService, org.springframework.core.task.TaskRejectedException, org.springframework.lang.Nullable, org.springframework.scheduling.TaskScheduler, .Trigger, .TriggerContext, org.springframework.scheduling.support.TaskUtils, org.springframework.util.Assert, .ClassUtils, .ErrorHandler

References Deprecated

ConcurrentTaskExecutor is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Adapter that takes a java.util.concurrent.ScheduledExecutorService and exposes a Spring org.springframework.scheduling.TaskScheduler for it. Extends ConcurrentTaskExecutor in order to implement the org.springframework.scheduling.SchedulingTaskExecutor interface as well.

Autodetects a JSR-236 jakarta.enterprise.concurrent.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 java.util.concurrent.ScheduledThreadPoolExecutor in bean style, exposing it as a Spring org.springframework.scheduling.TaskScheduler directly. This is a convenient alternative to a raw ScheduledThreadPoolExecutor definition with a separate definition of the present adapter class.

Authors
Juergen Hoeller, Mark Fisher, Arjen Poutsma
Since
3.0
See Also
java.util.concurrent.ScheduledExecutorService, java.util.concurrent.ScheduledThreadPoolExecutor, java.util.concurrent.Executors, DefaultManagedTaskScheduler, ThreadPoolTaskScheduler

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler

Delegate that adapts a Spring Trigger to a JSR-236 Trigger.

Field Summary

Modifier and TypeField and Description
private Clock
private boolean
private ErrorHandler
private static Class<?>
private static final TimeUnit
private ScheduledExecutorService

Constructor Summary

AccessConstructor and Description
public
ConcurrentTaskScheduler()
Deprecated since 6.1. in favor of ConcurrentTaskScheduler(ScheduledExecutorService) with an externally provided Executor

Create a new ConcurrentTaskScheduler, using a single thread executor as default.

public
ConcurrentTaskScheduler(ScheduledExecutorService scheduledExecutor)

Create a new ConcurrentTaskScheduler, using the given java.util.concurrent.ScheduledExecutorService as shared delegate.

public
ConcurrentTaskScheduler(Executor concurrentExecutor, ScheduledExecutorService scheduledExecutor)

Create a new ConcurrentTaskScheduler, using the given java.util.concurrent.Executor and java.util.concurrent.ScheduledExecutorService as delegates.

Method Summary

Modifier and TypeMethod and Description
private Runnable
decorateTask(Runnable task, boolean isRepeatingTask)

public Clock
getClock()

Overrides default org.springframework.scheduling.TaskScheduler.getClock.

Return the clock to use for scheduling purposes.

private ScheduledExecutorService
private void
public ScheduledFuture<?>
schedule(Runnable
the Runnable to execute whenever the trigger fires
task
,
Trigger
an implementation of the Trigger interface, e.g. a org.springframework.scheduling.support.CronTrigger object wrapping a cron expression
trigger
)

Implements org.springframework.scheduling.TaskScheduler.schedule.

Schedule the given Runnable, invoking it whenever the trigger indicates a next execution time.

public ScheduledFuture<?>
schedule(Runnable
the Runnable to execute whenever the trigger fires
task
,
Instant
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)
startTime
)

Implements org.springframework.scheduling.TaskScheduler.schedule.

Schedule the given Runnable, invoking it at the specified execution time.

public ScheduledFuture<?>
scheduleAtFixedRate(Runnable
the Runnable to execute whenever the trigger fires
task
,
Instant
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)
startTime
,
Duration
the interval between successive executions of the task
period
)

Implements org.springframework.scheduling.TaskScheduler.scheduleAtFixedRate.

Schedule the given Runnable, invoking it at the specified execution time and subsequently with the given period.

public ScheduledFuture<?>
scheduleAtFixedRate(Runnable
the Runnable to execute whenever the trigger fires
task
,
Duration
the interval between successive executions of the task
period
)

Implements org.springframework.scheduling.TaskScheduler.scheduleAtFixedRate.

Schedule the given Runnable, starting as soon as possible and invoking it with the given period.

public ScheduledFuture<?>
scheduleWithFixedDelay(Runnable
the Runnable to execute whenever the trigger fires
task
,
Instant
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)
startTime
,
Duration
the delay between the completion of one execution and the start of the next
delay
)

Implements org.springframework.scheduling.TaskScheduler.scheduleWithFixedDelay.

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.

public ScheduledFuture<?>
scheduleWithFixedDelay(Runnable
the Runnable to execute whenever the trigger fires
task
,
Duration
the delay between the completion of one execution and the start of the next
delay
)

Implements org.springframework.scheduling.TaskScheduler.scheduleWithFixedDelay.

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.

public void
setClock(Clock clock)

Set the clock to use for scheduling purposes.

public void
setErrorHandler(ErrorHandler errorHandler)

Provide an ErrorHandler strategy.

public void
Inherited from org.springframework.scheduling.concurrent.ConcurrentTaskExecutor:
executeexecutegetConcurrentExecutorsetConcurrentExecutorsetTaskDecoratorsubmitsubmitsubmitListenablesubmitListenable

Field Detail

clockback to summary
private Clock clock
enterpriseConcurrentSchedulerback to summary
private boolean enterpriseConcurrentScheduler
errorHandlerback to summary
private ErrorHandler errorHandler
Annotations
@Nullable
managedScheduledExecutorServiceClassback to summary
private static Class<?> managedScheduledExecutorServiceClass
Annotations
@Nullable
NANOback to summary
private static final TimeUnit NANO
scheduledExecutorback to summary
private ScheduledExecutorService scheduledExecutor
Annotations
@Nullable

Constructor Detail

ConcurrentTaskSchedulerback to summary
public ConcurrentTaskScheduler()

Deprecated

since 6.1.

in favor of ConcurrentTaskScheduler(ScheduledExecutorService) with an externally provided Executor

Create a new ConcurrentTaskScheduler, using a single thread executor as default.

Annotations
@Deprecated
since:6.1
See Also
java.util.concurrent.Executors#newSingleThreadScheduledExecutor()
ConcurrentTaskSchedulerback to summary
public ConcurrentTaskScheduler(ScheduledExecutorService scheduledExecutor)

Create a new ConcurrentTaskScheduler, using the given java.util.concurrent.ScheduledExecutorService as shared delegate.

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

Parameters
scheduledExecutor:ScheduledExecutorService

the java.util.concurrent.ScheduledExecutorService to delegate to for org.springframework.scheduling.SchedulingTaskExecutor as well as TaskScheduler invocations

ConcurrentTaskSchedulerback to summary
public ConcurrentTaskScheduler(Executor concurrentExecutor, ScheduledExecutorService scheduledExecutor)

Create a new ConcurrentTaskScheduler, using the given java.util.concurrent.Executor and java.util.concurrent.ScheduledExecutorService as delegates.

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

Parameters
concurrentExecutor:Executor

the java.util.concurrent.Executor to delegate to for org.springframework.scheduling.SchedulingTaskExecutor invocations

scheduledExecutor:ScheduledExecutorService

the java.util.concurrent.ScheduledExecutorService to delegate to for TaskScheduler invocations

Method Detail

decorateTaskback to summary
private Runnable decorateTask(Runnable task, boolean isRepeatingTask)
getClockback to summary
public Clock getClock()

Overrides default org.springframework.scheduling.TaskScheduler.getClock.

Doc from org.springframework.scheduling.TaskScheduler.getClock.

Return the clock to use for scheduling purposes.

Annotations
@Override
getScheduledExecutorback to summary
private ScheduledExecutorService getScheduledExecutor()
initScheduledExecutorback to summary
private void initScheduledExecutor(ScheduledExecutorService scheduledExecutor)
scheduleback to summary
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger)

Implements org.springframework.scheduling.TaskScheduler.schedule.

Doc from org.springframework.scheduling.TaskScheduler.schedule.

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.

Parameters
task:Runnable

the Runnable to execute whenever the trigger fires

trigger:Trigger

an implementation of the Trigger interface, e.g. a org.springframework.scheduling.support.CronTrigger object wrapping a cron expression

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task, or null if the given Trigger object never fires (i.e. returns null from Trigger#nextExecution)

Annotations
@Override
@Nullable
scheduleback to summary
public ScheduledFuture<?> schedule(Runnable task, Instant startTime)

Implements org.springframework.scheduling.TaskScheduler.schedule.

Doc from org.springframework.scheduling.TaskScheduler.schedule.

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.

Parameters
task:Runnable

the Runnable to execute whenever the trigger fires

startTime:Instant

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:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Override
scheduleAtFixedRateback to summary
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period)

Implements org.springframework.scheduling.TaskScheduler.scheduleAtFixedRate.

Doc from org.springframework.scheduling.TaskScheduler.scheduleAtFixedRate.

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.

Parameters
task:Runnable

the Runnable to execute whenever the trigger fires

startTime:Instant

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

the interval between successive executions of the task

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Override
scheduleAtFixedRateback to summary
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period)

Implements org.springframework.scheduling.TaskScheduler.scheduleAtFixedRate.

Doc from org.springframework.scheduling.TaskScheduler.scheduleAtFixedRate.

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.

Parameters
task:Runnable

the Runnable to execute whenever the trigger fires

period:Duration

the interval between successive executions of the task

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Override
scheduleWithFixedDelayback to summary
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay)

Implements org.springframework.scheduling.TaskScheduler.scheduleWithFixedDelay.

Doc from org.springframework.scheduling.TaskScheduler.scheduleWithFixedDelay.

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.

Parameters
task:Runnable

the Runnable to execute whenever the trigger fires

startTime:Instant

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

the delay between the completion of one execution and the start of the next

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Override
scheduleWithFixedDelayback to summary
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)

Implements org.springframework.scheduling.TaskScheduler.scheduleWithFixedDelay.

Doc from org.springframework.scheduling.TaskScheduler.scheduleWithFixedDelay.

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.

Parameters
task:Runnable

the Runnable to execute whenever the trigger fires

delay:Duration

the delay between the completion of one execution and the start of the next

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Override
setClockback to summary
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
Clock#systemDefaultZone()
setErrorHandlerback to summary
public void setErrorHandler(ErrorHandler errorHandler)

Provide an ErrorHandler strategy.

setScheduledExecutorback to summary
public void setScheduledExecutor(ScheduledExecutorService scheduledExecutor)

Specify the java.util.concurrent.ScheduledExecutorService to delegate to.

Autodetects a JSR-236 jakarta.enterprise.concurrent.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 org.springframework.scheduling.SchedulingTaskExecutor invocations as well, pass the same executor reference to setConcurrentExecutor.

See Also
setConcurrentExecutor
org.springframework.scheduling.concurrent back to summary

private Class ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.scheduling.concurrent.ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler

Delegate that adapts a Spring Trigger to a JSR-236 Trigger. Separated into an inner class in order to avoid a hard dependency on the JSR-236 API.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ScheduledFuture<?>
schedule(Runnable task, Trigger trigger)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

EnterpriseConcurrentTriggerSchedulerback to summary
private EnterpriseConcurrentTriggerScheduler()

Method Detail

scheduleback to summary
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger)
org.springframework.scheduling.concurrent back to summary

private Class ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler.TriggerAdapter

extends Object
implements Trigger
Class Inheritance
  • java.lang.Object
  • org.springframework.scheduling.concurrent.ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler.TriggerAdapter
All Implemented Interfaces
jakarta.enterprise.concurrent.Trigger

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final Trigger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Date
getNextRunTime(LastExecution
information about the last execution of the task. This value will be null if the task has not yet run.
le
,
Date
the date/time in which the task was scheduled using the ManagedScheduledExecutorService.schedule method.
taskScheduledTime
)

Implements jakarta.enterprise.concurrent.Trigger.getNextRunTime.

Retrieve the next time that the task should run after.

public boolean
skipRun(LastExecution
information about the last execution of the task. This value will be null if the task has not yet run.
lastExecutionInfo
,
Date
the date/time that the task was originally scheduled to run.
scheduledRunTime
)

Implements jakarta.enterprise.concurrent.Trigger.skipRun.

Return true if this run instance should be skipped.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

adapteeback to summary
private final Trigger adaptee

Constructor Detail

TriggerAdapterback to summary
public TriggerAdapter(Trigger adaptee)

Method Detail

getNextRunTimeback to summary
public Date getNextRunTime(LastExecution le, Date taskScheduledTime)

Implements jakarta.enterprise.concurrent.Trigger.getNextRunTime.

Doc from jakarta.enterprise.concurrent.Trigger.getNextRunTime.

Retrieve the next time that the task should run after.

Parameters
le:LastExecution

information about the last execution of the task. This value will be null if the task has not yet run.

taskScheduledTime:Date

the date/time in which the task was scheduled using the ManagedScheduledExecutorService.schedule method.

Returns:Date

the date/time in which the next task iteration should execute on or after.

Annotations
@Override
@Nullable
skipRunback to summary
public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime)

Implements jakarta.enterprise.concurrent.Trigger.skipRun.

Doc from jakarta.enterprise.concurrent.Trigger.skipRun.

Return true if this run instance should be skipped.

This is useful if the task shouldn't run because it is late or if the task is paused or suspended.

Once this task is skipped, the state of it's Future's result will throw a SkippedException. Unchecked exceptions will be wrapped in a SkippedException.

Parameters
lastExecutionInfo:LastExecution

information about the last execution of the task. This value will be null if the task has not yet run.

scheduledRunTime:Date

the date/time that the task was originally scheduled to run.

Returns:boolean

true if the task should be skipped and rescheduled.

Annotations
@Override
org.springframework.scheduling.concurrent back to summary

private Class ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler.TriggerAdapter.LastExecutionAdapter

extends Object
implements TriggerContext
Class Inheritance
  • java.lang.Object
  • org.springframework.scheduling.concurrent.ConcurrentTaskScheduler.EnterpriseConcurrentTriggerScheduler.TriggerAdapter.LastExecutionAdapter
All Implemented Interfaces
org.springframework.scheduling.TriggerContext

Field Summary

Modifier and TypeField and Description
private final LastExecution

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Instant
lastActualExecution()

Implements org.springframework.scheduling.TriggerContext.lastActualExecution.

Return the last actual execution time of the task, or null if not scheduled before.

public Instant
lastCompletion()

Implements org.springframework.scheduling.TriggerContext.lastCompletion.

Return the last completion time of the task, or null if not scheduled before.

public Instant
lastScheduledExecution()

Implements org.springframework.scheduling.TriggerContext.lastScheduledExecution.

Return the last scheduled execution time of the task, or null if not scheduled before.

private static Instant
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

leback to summary
private final LastExecution le
Annotations
@Nullable

Constructor Detail

LastExecutionAdapterback to summary
public LastExecutionAdapter(LastExecution le)

Method Detail

lastActualExecutionback to summary
public Instant lastActualExecution()

Implements org.springframework.scheduling.TriggerContext.lastActualExecution.

Doc from org.springframework.scheduling.TriggerContext.lastActualExecution.

Return the last actual execution time of the task, or null if not scheduled before.

Annotations
@Override
lastCompletionback to summary
public Instant lastCompletion()

Implements org.springframework.scheduling.TriggerContext.lastCompletion.

Doc from org.springframework.scheduling.TriggerContext.lastCompletion.

Return the last completion time of the task, or null if not scheduled before.

Annotations
@Override
lastScheduledExecutionback to summary
public Instant lastScheduledExecution()

Implements org.springframework.scheduling.TriggerContext.lastScheduledExecution.

Doc from org.springframework.scheduling.TriggerContext.lastScheduledExecution.

Return the last scheduled execution time of the task, or null if not scheduled before.

Annotations
@Override
toInstantback to summary
private static Instant toInstant(Date date)
Annotations
@Nullable