Deprecated
as a consequence of SimpleAsyncTaskExecutor being deprecated. See corresponding docs for further information.
TaskScheduler interface, using
a single scheduler thread and executing every scheduled task in an individual
separate thread. This is an attractive choice with virtual threads on JDK 21,
expecting common usage with setVirtualThreads(true).
NOTE: Scheduling with a fixed delay enforces execution on the single scheduler thread, in order to provide traditional fixed-delay semantics! Prefer the use of fixed rates or cron triggers instead which are a better fit with this thread-per-task scheduler variant.
Supports a graceful shutdown through setTaskTerminationTimeout,
at the expense of task tracking overhead per execution thread at runtime.
Supports limiting concurrent threads through setConcurrencyLimit.
By default, the number of concurrent task executions is unlimited.
This allows for dynamic concurrency of scheduled task executions, in contrast
to ThreadPoolTaskScheduler which requires a fixed pool size.
NOTE: This implementation does not reuse threads! Consider a
thread-pooling TaskScheduler implementation instead, in particular for
scheduling a large number of short-lived tasks. Alternatively, on JDK 21,
consider setting setVirtualThreads to true.
Extends SimpleAsyncTaskExecutor and can serve as a fully capable
replacement for it, e.g. as a single shared instance serving as a
org. as well as a TaskScheduler.
This is generally not the case with other executor/scheduler implementations
which tend to have specific constraints for the scheduler thread pool,
requiring a separate thread pool for general executor purposes in practice.
As an alternative to the built-in thread-per-task capability, this scheduler
can also be configured with a separate target executor for scheduled task
execution through setTargetTaskExecutor: e.g. pointing to a shared
ThreadPoolTaskExecutor bean. This is still rather different from a
ThreadPoolTaskScheduler setup since it always uses a single scheduler
thread while dynamically dispatching to the target thread pool which may have
a dynamic core/max pool size range, participating in a shared concurrency limit.
setVirtualThreads, setTaskTerminationTimeout, setConcurrencyLimit, SimpleAsyncTaskExecutor, ThreadPoolTaskScheduler
| Modifier and Type | Field and Description |
|---|---|
| private ApplicationContext | |
| private Clock | |
| private final ExecutorLifecycleDelegate | |
| private static final TimeUnit | |
| private int | |
| private final ScheduledExecutorService | |
| private Executor |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides org. Implements java. task termination timeout has been set.
|
| private ScheduledExecutorService | |
| protected void | doExecute(Runnable
the Runnable to execute task)Overrides org. |
| public Clock | getClock()
Overrides default org. |
| public int | getPhase()
Overrides default org. |
| public boolean | isRunning()
Implements org. |
| public void | onApplicationEvent(ContextClosedEvent
the event to respond to event)Implements org. |
| public ScheduledFuture | schedule(Runnable
the Runnable to execute whenever the trigger fires task, Trigger an implementation of the trigger)Trigger interface,
e.g. a org. object
wrapping a cron expressionImplements org. 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. 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. 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. Runnable, starting as soon as possible and
invoking it with the given period.
|
| private Runnable | |
| 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. 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. 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 | setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object applicationContext)Implements org. |
| public void | |
| public void | |
| public void | setTargetTaskExecutor(Executor targetTaskExecutor)
Specify a custom target |
| public void | |
| public void | stop()
Implements org. |
| public void | stop(Runnable callback)
Overrides default org. |
| private Runnable |
| applicationContext | back to summary |
|---|---|
| private ApplicationContext applicationContext
| |
| clock | back to summary |
|---|---|
| private Clock clock | |
| lifecycleDelegate | back to summary |
|---|---|
| private final ExecutorLifecycleDelegate lifecycleDelegate | |
| NANO | back to summary |
|---|---|
| private static final TimeUnit NANO | |
| phase | back to summary |
|---|---|
| private int phase | |
| scheduledExecutor | back to summary |
|---|---|
| private final ScheduledExecutorService scheduledExecutor | |
| targetTaskExecutor | back to summary |
|---|---|
| private Executor targetTaskExecutor
| |
| SimpleAsyncTaskScheduler | back to summary |
|---|---|
| public SimpleAsyncTaskScheduler() | |
| close | back to summary |
|---|---|
| public void close() Overrides org. Implements java. Doc from org. This close methods tracks the termination of active threads if a concrete
| |
| createScheduledExecutor | back to summary |
|---|---|
| private ScheduledExecutorService createScheduledExecutor() | |
| doExecute | back to summary |
|---|---|
| protected void doExecute(Runnable task) Overrides org. Doc from org. Template method for the actual execution of a task. The default implementation creates a new Thread and starts it. | |
| getClock | back to summary |
|---|---|
| public Clock getClock() Overrides default org. Doc from org. Return the clock to use for scheduling purposes.
| |
| getPhase | back to summary |
|---|---|
| public int getPhase() Overrides default org. Return the lifecycle phase for pausing and resuming this executor. | |
| isRunning | back to summary |
|---|---|
| public boolean isRunning() Implements org. Doc from org. Check whether this component is currently running. In the case of a container, this will return
| |
| onApplicationEvent | back to summary |
|---|---|
| public void onApplicationEvent(ContextClosedEvent event) Implements org. Doc from org. Handle an application event.
| |
| schedule | back to summary |
|---|---|
| public ScheduledFuture Implements org. Doc from org. Schedule the given Execution will end once the scheduler shuts down or the returned
| |
| schedule | back to summary |
|---|---|
| public ScheduledFuture Implements org. Doc from org. Schedule the given Execution will end once the scheduler shuts down or the returned
| |
| scheduleAtFixedRate | back to summary |
|---|---|
| public ScheduledFuture Implements org. Doc from org. Schedule the given Execution will end once the scheduler shuts down or the returned
| |
| scheduleAtFixedRate | back to summary |
|---|---|
| public ScheduledFuture Implements org. Doc from org. Schedule the given Execution will end once the scheduler shuts down or the returned
| |
| scheduledTask | back to summary |
|---|---|
| private Runnable scheduledTask(Runnable task) | |
| scheduleWithFixedDelay | back to summary |
|---|---|
| public ScheduledFuture Implements org. Doc from org. Schedule the given Execution will end once the scheduler shuts down or the returned
| |
| scheduleWithFixedDelay | back to summary |
|---|---|
| public ScheduledFuture Implements org. Doc from org. Schedule the given Execution will end once the scheduler shuts down or the returned
| |
| setApplicationContext | back to summary |
|---|---|
| public void setApplicationContext(ApplicationContext applicationContext) Implements org. Doc from org. Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object. Invoked after population of normal bean properties but before an init callback such
as
| |
| setClock | back 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.
| |
| setPhase | back to summary |
|---|---|
| public void setPhase(int phase) Specify the lifecycle phase for pausing and resuming this executor.
The default is
| |
| setTargetTaskExecutor | back to summary |
|---|---|
| public void setTargetTaskExecutor(Executor targetTaskExecutor) Specify a custom target If not set, the regular | |
| start | back to summary |
|---|---|
| public void start() Implements org. Doc from org. Start this component. Should not throw an exception if the component is already running. In the case of a container, this will propagate the start signal to all components that apply.
| |
| stop | back to summary |
|---|---|
| public void stop() Implements org. Doc from org. Stop this component, typically in a synchronous fashion, such that the component is
fully stopped upon return of this method. Consider implementing Note that this stop notification is not guaranteed to come before destruction:
On regular shutdown, Should not throw an exception if the component is not running (not started yet). In the case of a container, this will propagate the stop signal to all components that apply.
| |
| stop | back to summary |
|---|---|
| public void stop(Runnable callback) Overrides default org. Doc from org. Indicates that a Lifecycle component must stop if it is currently running. The provided callback is used by the The The default implementation delegates to
| |
| taskOnSchedulerThread | back to summary |
|---|---|
| private Runnable taskOnSchedulerThread(Runnable task) | |