Class ScheduledTaskRegistrar
java.lang.Object
org.springframework.scheduling.config.ScheduledTaskRegistrar
- All Implemented Interfaces:
DisposableBean
,InitializingBean
,ScheduledTaskHolder
- Direct Known Subclasses:
ContextLifecycleScheduledTaskRegistrar
public class ScheduledTaskRegistrar
extends Object
implements ScheduledTaskHolder, InitializingBean, DisposableBean
Helper bean for registering tasks with a
TaskScheduler
, typically using cron
expressions.
ScheduledTaskRegistrar
has a more prominent user-facing role when used in
conjunction with the @EnableAsync
annotation and its
SchedulingConfigurer
callback interface.
- Since:
- 3.0
- Author:
- Juergen Hoeller, Chris Beams, Tobias Montagna-Hay, Sam Brannen, Arjen Poutsma, Brian Clozel
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
A special cron expression value that indicates a disabled trigger: "-". -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCronTask
(Runnable task, String expression) Add aRunnable
task to be triggered per the given cronexpression
.void
addCronTask
(CronTask task) Add aCronTask
.void
addFixedDelayTask
(Runnable task, long interval) Deprecated.void
addFixedDelayTask
(Runnable task, Duration interval) Add a Runnable task to be triggered with the given fixed delay.void
Add a fixed-delayIntervalTask
.void
addFixedRateTask
(Runnable task, long interval) Deprecated.as of 6.0, in favor ofaddFixedRateTask(Runnable, Duration)
void
addFixedRateTask
(Runnable task, Duration interval) Add aRunnable
task to be triggered at the given fixed-rate interval.void
addFixedRateTask
(IntervalTask task) Add a fixed-rateIntervalTask
.void
addOneTimeTask
(Runnable task, Duration initialDelay) Add a Runnable task to be triggered once after the given initial delay.void
addOneTimeTask
(DelayedTask task) Add a one-timeDelayedTask
.void
addTriggerTask
(Runnable task, Trigger trigger) Add a Runnable task to be triggered per the givenTrigger
.void
addTriggerTask
(TriggerTask task) Add aTriggerTask
.void
CallsscheduleTasks()
at bean construction time.void
destroy()
Invoked by the containingBeanFactory
on destruction of a bean.Get the cron tasks as an unmodifiable list ofCronTask
objects.Get the fixed-delay tasks as an unmodifiable list ofIntervalTask
objects.Get the fixed-rate tasks as an unmodifiable list ofIntervalTask
objects.io.micrometer.observation.ObservationRegistry
Return theObservationRegistry
for this registrar.Return all locally registered tasks that have been scheduled by this registrar.Return theTaskScheduler
instance for this registrar (may benull
).Get the trigger tasks as an unmodifiable list ofTriggerTask
objects.boolean
hasTasks()
Return whether thisScheduledTaskRegistrar
has any tasks registered.scheduleCronTask
(CronTask task) Schedule the specified cron task, either right away if possible or on initialization of the scheduler.Schedule the specified fixed-delay task, either right away if possible or on initialization of the scheduler.Schedule the specified fixed-rate task, either right away if possible or on initialization of the scheduler.Schedule the specified one-time task, either right away if possible or on initialization of the scheduler.protected void
Schedule all registered tasks against the underlying task scheduler.Schedule the specified trigger task, either right away if possible or on initialization of the scheduler.void
setCronTasks
(Map<Runnable, String> cronTasks) Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.void
setCronTasksList
(List<CronTask> cronTasks) Specify triggered tasks as a list ofCronTask
objects.void
setFixedDelayTasks
(Map<Runnable, Long> fixedDelayTasks) Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.void
setFixedDelayTasksList
(List<IntervalTask> fixedDelayTasks) Specify fixed-delay tasks as a list ofIntervalTask
objects.void
setFixedRateTasks
(Map<Runnable, Long> fixedRateTasks) Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.void
setFixedRateTasksList
(List<IntervalTask> fixedRateTasks) Specify fixed-rate tasks as a list ofIntervalTask
objects.void
setObservationRegistry
(io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistry
to record observations for scheduled tasks.void
setScheduler
(Object scheduler) Set theTaskScheduler
to register scheduled tasks with, or aScheduledExecutorService
to be wrapped as aTaskScheduler
.void
setTaskScheduler
(TaskScheduler taskScheduler) Set theTaskScheduler
to register scheduled tasks with.void
setTriggerTasks
(Map<Runnable, Trigger> triggerTasks) Specify triggered tasks as a Map of Runnables (the tasks) and Trigger objects (typically custom implementations of theTrigger
interface).void
setTriggerTasksList
(List<TriggerTask> triggerTasks) Specify triggered tasks as a list ofTriggerTask
objects.
-
Field Details
-
CRON_DISABLED
A special cron expression value that indicates a disabled trigger: "-".This is primarily meant for use with
addCronTask(Runnable, String)
when the value for the suppliedexpression
is retrieved from an external source — for example, from a property in theEnvironment
.- Since:
- 5.2
- See Also:
-
-
Constructor Details
-
ScheduledTaskRegistrar
public ScheduledTaskRegistrar()
-
-
Method Details
-
setTaskScheduler
Set theTaskScheduler
to register scheduled tasks with. -
setScheduler
Set theTaskScheduler
to register scheduled tasks with, or aScheduledExecutorService
to be wrapped as aTaskScheduler
. -
getScheduler
Return theTaskScheduler
instance for this registrar (may benull
). -
setObservationRegistry
public void setObservationRegistry(@Nullable io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistry
to record observations for scheduled tasks.- Since:
- 6.1
-
getObservationRegistry
Return theObservationRegistry
for this registrar.- Since:
- 6.1
-
setTriggerTasks
Specify triggered tasks as a Map of Runnables (the tasks) and Trigger objects (typically custom implementations of theTrigger
interface). -
setTriggerTasksList
Specify triggered tasks as a list ofTriggerTask
objects. Primarily used by<task:*>
namespace parsing.- Since:
- 3.2
- See Also:
-
getTriggerTaskList
Get the trigger tasks as an unmodifiable list ofTriggerTask
objects.- Returns:
- the list of tasks (never
null
) - Since:
- 4.2
-
setCronTasks
Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.- See Also:
-
setCronTasksList
Specify triggered tasks as a list ofCronTask
objects. Primarily used by<task:*>
namespace parsing.- Since:
- 3.2
- See Also:
-
getCronTaskList
Get the cron tasks as an unmodifiable list ofCronTask
objects.- Returns:
- the list of tasks (never
null
) - Since:
- 4.2
-
setFixedRateTasks
Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values. -
setFixedRateTasksList
Specify fixed-rate tasks as a list ofIntervalTask
objects. Primarily used by<task:*>
namespace parsing.- Since:
- 3.2
- See Also:
-
getFixedRateTaskList
Get the fixed-rate tasks as an unmodifiable list ofIntervalTask
objects.- Returns:
- the list of tasks (never
null
) - Since:
- 4.2
-
setFixedDelayTasks
Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values. -
setFixedDelayTasksList
Specify fixed-delay tasks as a list ofIntervalTask
objects. Primarily used by<task:*>
namespace parsing.- Since:
- 3.2
- See Also:
-
getFixedDelayTaskList
Get the fixed-delay tasks as an unmodifiable list ofIntervalTask
objects.- Returns:
- the list of tasks (never
null
) - Since:
- 4.2
-
addTriggerTask
Add a Runnable task to be triggered per the givenTrigger
. -
addTriggerTask
Add aTriggerTask
.- Since:
- 3.2
- See Also:
-
addCronTask
Add aRunnable
task to be triggered per the given cronexpression
.This method will not register the task if the
expression
is equal toCRON_DISABLED
. -
addCronTask
Add aCronTask
.- Since:
- 3.2
-
addFixedRateTask
Deprecated.as of 6.0, in favor ofaddFixedRateTask(Runnable, Duration)
Add aRunnable
task to be triggered at the given fixed-rate interval. -
addFixedRateTask
Add aRunnable
task to be triggered at the given fixed-rate interval.- Since:
- 6.0
- See Also:
-
addFixedRateTask
Add a fixed-rateIntervalTask
.- Since:
- 3.2
- See Also:
-
addFixedDelayTask
Deprecated.as of 6.0, in favor ofaddFixedDelayTask(Runnable, Duration)
Add a Runnable task to be triggered with the given fixed delay. -
addFixedDelayTask
Add a Runnable task to be triggered with the given fixed delay.- Since:
- 6.0
- See Also:
-
addFixedDelayTask
Add a fixed-delayIntervalTask
.- Since:
- 3.2
- See Also:
-
addOneTimeTask
Add a Runnable task to be triggered once after the given initial delay.- Since:
- 6.1
- See Also:
-
addOneTimeTask
Add a one-timeDelayedTask
.- Since:
- 6.1
- See Also:
-
hasTasks
public boolean hasTasks()Return whether thisScheduledTaskRegistrar
has any tasks registered.- Since:
- 3.2
-
afterPropertiesSet
public void afterPropertiesSet()CallsscheduleTasks()
at bean construction time.- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
scheduleTasks
protected void scheduleTasks()Schedule all registered tasks against the underlying task scheduler. -
scheduleTriggerTask
Schedule the specified trigger task, either right away if possible or on initialization of the scheduler.- Returns:
- a handle to the scheduled task, allowing to cancel it
- Since:
- 4.3
-
scheduleCronTask
Schedule the specified cron task, either right away if possible or on initialization of the scheduler.- Returns:
- a handle to the scheduled task, allowing to cancel it
(or
null
if processing a previously registered task) - Since:
- 4.3
-
scheduleFixedRateTask
Schedule the specified fixed-rate task, either right away if possible or on initialization of the scheduler.- Returns:
- a handle to the scheduled task, allowing to cancel it
(or
null
if processing a previously registered task) - Since:
- 5.0.2
-
scheduleFixedDelayTask
Schedule the specified fixed-delay task, either right away if possible or on initialization of the scheduler.- Returns:
- a handle to the scheduled task, allowing to cancel it
(or
null
if processing a previously registered task) - Since:
- 5.0.2
-
scheduleOneTimeTask
Schedule the specified one-time task, either right away if possible or on initialization of the scheduler.- Returns:
- a handle to the scheduled task, allowing to cancel it
(or
null
if processing a previously registered task) - Since:
- 6.1
-
getScheduledTasks
Return all locally registered tasks that have been scheduled by this registrar. -
destroy
public void destroy()Description copied from interface:DisposableBean
Invoked by the containingBeanFactory
on destruction of a bean.- Specified by:
destroy
in interfaceDisposableBean
-
addFixedDelayTask(Runnable, Duration)