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
FieldsModifier and TypeFieldDescriptionstatic final StringA special cron expression value that indicates a disabled trigger: "-". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCronTask(Runnable task, String expression) Add aRunnabletask to be triggered per the given cronexpression.voidaddCronTask(CronTask task) Add aCronTask.voidaddFixedDelayTask(Runnable task, long interval) Deprecated.voidaddFixedDelayTask(Runnable task, Duration interval) Add a Runnable task to be triggered with the given fixed delay.voidAdd a fixed-delayIntervalTask.voidaddFixedRateTask(Runnable task, long interval) Deprecated.as of 6.0, in favor ofaddFixedRateTask(Runnable, Duration)voidaddFixedRateTask(Runnable task, Duration interval) Add aRunnabletask to be triggered at the given fixed-rate interval.voidaddFixedRateTask(IntervalTask task) Add a fixed-rateIntervalTask.voidaddOneTimeTask(Runnable task, Duration initialDelay) Add a Runnable task to be triggered once after the given initial delay.voidaddOneTimeTask(DelayedTask task) Add a one-timeDelayedTask.voidaddTriggerTask(Runnable task, Trigger trigger) Add a Runnable task to be triggered per the givenTrigger.voidaddTriggerTask(TriggerTask task) Add aTriggerTask.voidCallsscheduleTasks()at bean construction time.voiddestroy()Invoked by the containingBeanFactoryon destruction of a bean.Get the cron tasks as an unmodifiable list ofCronTaskobjects.Get the fixed-delay tasks as an unmodifiable list ofIntervalTaskobjects.Get the fixed-rate tasks as an unmodifiable list ofIntervalTaskobjects.@Nullable io.micrometer.observation.ObservationRegistryReturn theObservationRegistryfor this registrar.Return all locally registered tasks that have been scheduled by this registrar.Return theTaskSchedulerinstance for this registrar (may benull).Get the trigger tasks as an unmodifiable list ofTriggerTaskobjects.booleanhasTasks()Return whether thisScheduledTaskRegistrarhas 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 voidSchedule all registered tasks against the underlying task scheduler.Schedule the specified trigger task, either right away if possible or on initialization of the scheduler.voidsetCronTasks(Map<Runnable, String> cronTasks) Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.voidsetCronTasksList(List<CronTask> cronTasks) Specify triggered tasks as a list ofCronTaskobjects.voidsetFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks) Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.voidsetFixedDelayTasksList(List<IntervalTask> fixedDelayTasks) Specify fixed-delay tasks as a list ofIntervalTaskobjects.voidsetFixedRateTasks(Map<Runnable, Long> fixedRateTasks) Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.voidsetFixedRateTasksList(List<IntervalTask> fixedRateTasks) Specify fixed-rate tasks as a list ofIntervalTaskobjects.voidsetObservationRegistry(@Nullable io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistryto record observations for scheduled tasks.voidsetScheduler(@Nullable Object scheduler) Set theTaskSchedulerto register scheduled tasks with, or aScheduledExecutorServiceto be wrapped as aTaskScheduler.voidsetTaskScheduler(TaskScheduler taskScheduler) Set theTaskSchedulerto register scheduled tasks with.voidsetTriggerTasks(Map<Runnable, Trigger> triggerTasks) Specify triggered tasks as a Map of Runnables (the tasks) and Trigger objects (typically custom implementations of theTriggerinterface).voidsetTriggerTasksList(List<TriggerTask> triggerTasks) Specify triggered tasks as a list ofTriggerTaskobjects.
-
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 suppliedexpressionis 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 theTaskSchedulerto register scheduled tasks with. -
setScheduler
Set theTaskSchedulerto register scheduled tasks with, or aScheduledExecutorServiceto be wrapped as aTaskScheduler. -
getScheduler
Return theTaskSchedulerinstance for this registrar (may benull). -
setObservationRegistry
public void setObservationRegistry(@Nullable io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistryto record observations for scheduled tasks.- Since:
- 6.1
-
getObservationRegistry
Return theObservationRegistryfor this registrar.- Since:
- 6.1
-
setTriggerTasks
-
setTriggerTasksList
Specify triggered tasks as a list ofTriggerTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
-
getTriggerTaskList
Get the trigger tasks as an unmodifiable list ofTriggerTaskobjects.- Returns:
- the list of tasks (never
null) - Since:
- 4.2
-
setCronTasks
-
setCronTasksList
-
getCronTaskList
-
setFixedRateTasks
-
setFixedRateTasksList
Specify fixed-rate tasks as a list ofIntervalTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
-
getFixedRateTaskList
Get the fixed-rate tasks as an unmodifiable list ofIntervalTaskobjects.- Returns:
- the list of tasks (never
null) - Since:
- 4.2
-
setFixedDelayTasks
-
setFixedDelayTasksList
Specify fixed-delay tasks as a list ofIntervalTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
-
getFixedDelayTaskList
Get the fixed-delay tasks as an unmodifiable list ofIntervalTaskobjects.- Returns:
- the list of tasks (never
null) - Since:
- 4.2
-
addTriggerTask
-
addTriggerTask
-
addCronTask
Add aRunnabletask to be triggered per the given cronexpression.This method will not register the task if the
expressionis equal toCRON_DISABLED. -
addCronTask
-
addFixedRateTask
Deprecated.as of 6.0, in favor ofaddFixedRateTask(Runnable, Duration)Add aRunnabletask to be triggered at the given fixed-rate interval. -
addFixedRateTask
-
addFixedRateTask
-
addFixedDelayTask
Deprecated.as of 6.0, in favor ofaddFixedDelayTask(Runnable, Duration)Add a Runnable task to be triggered with the given fixed delay. -
addFixedDelayTask
-
addFixedDelayTask
Add a fixed-delayIntervalTask.- Since:
- 3.2
- See Also:
-
addOneTimeTask
-
addOneTimeTask
-
hasTasks
public boolean hasTasks()Return whether thisScheduledTaskRegistrarhas any tasks registered.- Since:
- 3.2
-
afterPropertiesSet
public void afterPropertiesSet()CallsscheduleTasks()at bean construction time.- Specified by:
afterPropertiesSetin 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
nullif 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
nullif 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
nullif 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
nullif processing a previously registered task) - Since:
- 6.1
-
getScheduledTasks
Return all locally registered tasks that have been scheduled by this registrar.- Specified by:
getScheduledTasksin interfaceScheduledTaskHolder- Since:
- 5.0.2
- See Also:
-
destroy
public void destroy()Description copied from interface:DisposableBeanInvoked by the containingBeanFactoryon destruction of a bean.- Specified by:
destroyin interfaceDisposableBean
-
addFixedDelayTask(Runnable, Duration)