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 SummaryFieldsModifier and TypeFieldDescriptionstatic final StringA special cron expression value that indicates a disabled trigger: "-".
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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_DISABLEDA 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- 
ScheduledTaskRegistrarpublic ScheduledTaskRegistrar()
 
- 
- 
Method Details- 
setTaskSchedulerSet theTaskSchedulerto register scheduled tasks with.
- 
setSchedulerSet theTaskSchedulerto register scheduled tasks with, or aScheduledExecutorServiceto be wrapped as aTaskScheduler.
- 
getSchedulerReturn theTaskSchedulerinstance for this registrar (may benull).
- 
setObservationRegistrypublic void setObservationRegistry(@Nullable io.micrometer.observation.ObservationRegistry observationRegistry) Configure anObservationRegistryto record observations for scheduled tasks.- Since:
- 6.1
 
- 
getObservationRegistryReturn theObservationRegistryfor this registrar.- Since:
- 6.1
 
- 
setTriggerTasksSpecify triggered tasks as a Map of Runnables (the tasks) and Trigger objects (typically custom implementations of theTriggerinterface).
- 
setTriggerTasksListSpecify triggered tasks as a list ofTriggerTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
 
- 
getTriggerTaskListGet the trigger tasks as an unmodifiable list ofTriggerTaskobjects.- Returns:
- the list of tasks (never null)
- Since:
- 4.2
 
- 
setCronTasksSpecify triggered tasks as a Map of Runnables (the tasks) and cron expressions.- See Also:
 
- 
setCronTasksListSpecify triggered tasks as a list ofCronTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
 
- 
getCronTaskListGet the cron tasks as an unmodifiable list ofCronTaskobjects.- Returns:
- the list of tasks (never null)
- Since:
- 4.2
 
- 
setFixedRateTasksSpecify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.
- 
setFixedRateTasksListSpecify fixed-rate tasks as a list ofIntervalTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
 
- 
getFixedRateTaskListGet the fixed-rate tasks as an unmodifiable list ofIntervalTaskobjects.- Returns:
- the list of tasks (never null)
- Since:
- 4.2
 
- 
setFixedDelayTasksSpecify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.
- 
setFixedDelayTasksListSpecify fixed-delay tasks as a list ofIntervalTaskobjects. Primarily used by<task:*>namespace parsing.- Since:
- 3.2
- See Also:
 
- 
getFixedDelayTaskListGet the fixed-delay tasks as an unmodifiable list ofIntervalTaskobjects.- Returns:
- the list of tasks (never null)
- Since:
- 4.2
 
- 
addTriggerTaskAdd a Runnable task to be triggered per the givenTrigger.
- 
addTriggerTaskAdd aTriggerTask.- Since:
- 3.2
- See Also:
 
- 
addCronTaskAdd aRunnabletask to be triggered per the given cronexpression.This method will not register the task if the expressionis equal toCRON_DISABLED.
- 
addCronTaskAdd aCronTask.- Since:
- 3.2
 
- 
addFixedRateTaskDeprecated.as of 6.0, in favor ofaddFixedRateTask(Runnable, Duration)Add aRunnabletask to be triggered at the given fixed-rate interval.
- 
addFixedRateTaskAdd aRunnabletask to be triggered at the given fixed-rate interval.- Since:
- 6.0
- See Also:
 
- 
addFixedRateTaskAdd a fixed-rateIntervalTask.- Since:
- 3.2
- See Also:
 
- 
addFixedDelayTaskDeprecated.as of 6.0, in favor ofaddFixedDelayTask(Runnable, Duration)Add a Runnable task to be triggered with the given fixed delay.
- 
addFixedDelayTaskAdd a Runnable task to be triggered with the given fixed delay.- Since:
- 6.0
- See Also:
 
- 
addFixedDelayTaskAdd a fixed-delayIntervalTask.- Since:
- 3.2
- See Also:
 
- 
addOneTimeTaskAdd a Runnable task to be triggered once after the given initial delay.- Since:
- 6.1
- See Also:
 
- 
addOneTimeTaskAdd a one-timeDelayedTask.- Since:
- 6.1
- See Also:
 
- 
hasTaskspublic boolean hasTasks()Return whether thisScheduledTaskRegistrarhas any tasks registered.- Since:
- 3.2
 
- 
afterPropertiesSetpublic void afterPropertiesSet()CallsscheduleTasks()at bean construction time.- Specified by:
- afterPropertiesSetin interface- InitializingBean
 
- 
scheduleTasksprotected void scheduleTasks()Schedule all registered tasks against the underlying task scheduler.
- 
scheduleTriggerTaskSchedule 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
 
- 
scheduleCronTaskSchedule 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
 
- 
scheduleFixedRateTaskSchedule 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
 
- 
scheduleFixedDelayTaskSchedule 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
 
- 
scheduleOneTimeTaskSchedule 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
 
- 
getScheduledTasksReturn all locally registered tasks that have been scheduled by this registrar.
- 
destroypublic void destroy()Description copied from interface:DisposableBeanInvoked by the containingBeanFactoryon destruction of a bean.- Specified by:
- destroyin interface- DisposableBean
 
 
- 
addFixedDelayTask(Runnable, Duration)