Top Description Methods
org.springframework.scheduling

public Interface TaskScheduler

Known Direct Implementers
org.springframework.scheduling.concurrent.ConcurrentTaskScheduler, org.springframework.scheduling.concurrent.SimpleAsyncTaskScheduler, org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler, org.springframework.scheduling.config.TaskSchedulerRouter, org.springframework.scheduling.support.NoOpTaskScheduler
Imports
java.time.Clock, .Duration, .Instant, java.util.Date, java.util.concurrent.ScheduledFuture, org.springframework.lang.Nullable

Task scheduler interface that abstracts the scheduling of Runnables based on different kinds of triggers.

This interface is separate from SchedulingTaskExecutor since it usually represents a different kind of backend, i.e. a thread pool with different characteristics and capabilities. Implementations may implement both interfaces if they can handle both kinds of execution characteristics.

The 'default' implementation is org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler, wrapping a native java.util.concurrent.ScheduledExecutorService and adding extended trigger capabilities.

This interface is roughly equivalent to a JSR-236 ManagedScheduledExecutorService as supported in Jakarta EE environments but aligned with Spring's TaskExecutor model.

Author
Juergen Hoeller
Since
3.0
See Also
org.springframework.core.task.TaskExecutor, java.util.concurrent.ScheduledExecutorService, org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler

Method Summary

Modifier and TypeMethod and Description
public default Clock
getClock()

Return the clock to use for scheduling purposes.

public ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task, or null if the given Trigger object never fires (i.e. returns null from Trigger#nextExecution)
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
)

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

public ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
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
)

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

public default ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
schedule
(Runnable
the Runnable to execute whenever the trigger fires
task
,
Date
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
)

Deprecated since 6.0. as of 6.0, in favor of schedule(Runnable, Instant)
Schedule the given Runnable, invoking it at the specified execution time.
public ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
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
)

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

public default ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
scheduleAtFixedRate
(Runnable
the Runnable to execute whenever the trigger fires
task
,
Date
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
,
long
the interval between successive executions of the task (in milliseconds)
period
)

Deprecated since 6.0. as of 6.0, in favor of scheduleAtFixedRate(Runnable, Instant, Duration)
Schedule the given Runnable, invoking it at the specified execution time and subsequently with the given period.
public ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
scheduleAtFixedRate
(Runnable
the Runnable to execute whenever the trigger fires
task
,
Duration
the interval between successive executions of the task
period
)

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

public default ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
scheduleAtFixedRate
(Runnable
the Runnable to execute whenever the trigger fires
task
,
long
the interval between successive executions of the task (in milliseconds)
period
)

Deprecated since 6.0. as of 6.0, in favor of scheduleAtFixedRate(Runnable, Duration)
Schedule the given Runnable, starting as soon as possible and invoking it with the given period.
public ScheduledFuture<?>

Returns:

a ScheduledFuture representing pending completion of the task
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
)

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

Returns:

a ScheduledFuture representing pending completion of the task
scheduleWithFixedDelay
(Runnable
the Runnable to execute whenever the trigger fires
task
,
Date
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
,
long
the delay between the completion of one execution and the start of the next (in milliseconds)
delay
)

Deprecated since 6.0. as of 6.0, in favor of scheduleWithFixedDelay(Runnable, Instant, Duration)
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<?>

Returns:

a ScheduledFuture representing pending completion of the task
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
)

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

Returns:

a ScheduledFuture representing pending completion of the task
scheduleWithFixedDelay
(Runnable
the Runnable to execute whenever the trigger fires
task
,
long
the delay between the completion of one execution and the start of the next (in milliseconds)
delay
)

Deprecated since 6.0. as of 6.0, in favor of scheduleWithFixedDelay(Runnable, Duration)
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.

Method Detail

getClockback to summary
public default Clock getClock()

Return the clock to use for scheduling purposes.

Since
5.3
See Also
Clock#systemDefaultZone()
scheduleback to summary
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger)

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
@Nullable
Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
See Also
org.springframework.scheduling.support.CronTrigger
scheduleback to summary
public ScheduledFuture<?> schedule(Runnable task, Instant startTime)

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

Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
Since
5.0
scheduleback to summary
public default ScheduledFuture<?> schedule(Runnable task, Date startTime)

Deprecated

since 6.0.

as of 6.0, in favor of schedule(Runnable, Instant)

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

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
@Deprecated
since:6.0
Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
scheduleAtFixedRateback to summary
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period)

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

Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
Since
5.0
scheduleAtFixedRateback to summary
public default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period)

Deprecated

since 6.0.

as of 6.0, in favor of scheduleAtFixedRate(Runnable, Instant, Duration)

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

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

the interval between successive executions of the task (in milliseconds)

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Deprecated
since:6.0
Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
scheduleAtFixedRateback to summary
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Duration period)

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

Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
Since
5.0
scheduleAtFixedRateback to summary
public default ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period)

Deprecated

since 6.0.

as of 6.0, in favor of scheduleAtFixedRate(Runnable, Duration)

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

the interval between successive executions of the task (in milliseconds)

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Deprecated
since:6.0
Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
scheduleWithFixedDelayback to summary
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay)

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

Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
Since
5.0
scheduleWithFixedDelayback to summary
public default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay)

Deprecated

since 6.0.

as of 6.0, in favor of scheduleWithFixedDelay(Runnable, Instant, Duration)

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

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

the delay between the completion of one execution and the start of the next (in milliseconds)

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Deprecated
since:6.0
Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
scheduleWithFixedDelayback to summary
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Duration delay)

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

Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)
Since
5.0
scheduleWithFixedDelayback to summary
public default ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay)

Deprecated

since 6.0.

as of 6.0, in favor of scheduleWithFixedDelay(Runnable, Duration)

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

the delay between the completion of one execution and the start of the next (in milliseconds)

Returns:ScheduledFuture<?>

a ScheduledFuture representing pending completion of the task

Annotations
@Deprecated
since:6.0
Exceptions
TaskRejectedException:
if the given task was not accepted for internal reasons (e.g. a pool overload handling policy or a pool shutdown in progress)