Package org.springframework.scheduling.config

# Class ScheduledTaskRegistrar

java.lang.Object
org.springframework.scheduling.config.ScheduledTaskRegistrar

All Implemented Interfaces:
  `org.springframework.beans.factory.DisposableBean`, `org.springframework.beans.factory.InitializingBean`, `ScheduledTaskHolder`

Direct Known Subclasses:
  `ContextLifecycleScheduledTaskRegistrar`

public class ScheduledTaskRegistrar extends Object implements ScheduledTaskHolder, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.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:
  - `EnableAsync`
  - `SchedulingConfigurer`

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`CRON_DISABLED`
A special cron expression value that indicates a disabled trigger: "-".

## Constructor Summary

Constructors
Constructor
Description
`ScheduledTaskRegistrar()`

## Method Summary

Modifier and Type
Method
Description
`void`
`addCronTask(Runnable task, String expression)`
Add a `Runnable` task to be triggered per the given cron `expression`.
`void`
`addCronTask(CronTask task)`
Add a `CronTask`.
`void`
`addFixedDelayTask(Runnable task, long interval)`
Deprecated.
as of 6.0, in favor of `addFixedDelayTask(Runnable, Duration)`
`void`
`addFixedDelayTask(Runnable task, Duration interval)`
Add a Runnable task to be triggered with the given fixed delay.
`void`
`addFixedDelayTask(IntervalTask task)`
Add a fixed-delay `IntervalTask`.
`void`
`addFixedRateTask(Runnable task, long interval)`
Deprecated.
as of 6.0, in favor of `addFixedRateTask(Runnable, Duration)`
`void`
`addFixedRateTask(Runnable task, Duration interval)`
Add a `Runnable` task to be triggered at the given fixed-rate interval.
`void`
`addFixedRateTask(IntervalTask task)`
Add a fixed-rate `IntervalTask`.
`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-time `DelayedTask`.
`void`
`addTriggerTask(Runnable task, Trigger trigger)`
Add a Runnable task to be triggered per the given `Trigger`.
`void`
`addTriggerTask(TriggerTask task)`
Add a `TriggerTask`.
`void`
`afterPropertiesSet()`
Calls `scheduleTasks()` at bean construction time.
`void`
`destroy()`
`List<CronTask>`
`getCronTaskList()`
Get the cron tasks as an unmodifiable list of `CronTask` objects.
`List<IntervalTask>`
`getFixedDelayTaskList()`
Get the fixed-delay tasks as an unmodifiable list of `IntervalTask` objects.
`List<IntervalTask>`
`getFixedRateTaskList()`
Get the fixed-rate tasks as an unmodifiable list of `IntervalTask` objects.
`io.micrometer.observation.ObservationRegistry`
`getObservationRegistry()`
Return the `ObservationRegistry` for this registrar.
`Set<ScheduledTask>`
`getScheduledTasks()`
Return all locally registered tasks that have been scheduled by this registrar.
`TaskScheduler`
`getScheduler()`
Return the `TaskScheduler` instance for this registrar (may be `null`).
`List<TriggerTask>`
`getTriggerTaskList()`
Get the trigger tasks as an unmodifiable list of `TriggerTask` objects.
`boolean`
`hasTasks()`
Return whether this `ScheduledTaskRegistrar` has any tasks registered.
`ScheduledTask`
`scheduleCronTask(CronTask task)`
Schedule the specified cron task, either right away if possible or on initialization of the scheduler.
`ScheduledTask`
`scheduleFixedDelayTask(FixedDelayTask task)`
Schedule the specified fixed-delay task, either right away if possible or on initialization of the scheduler.
`ScheduledTask`
`scheduleFixedRateTask(FixedRateTask task)`
Schedule the specified fixed-rate task, either right away if possible or on initialization of the scheduler.
`ScheduledTask`
`scheduleOneTimeTask(OneTimeTask task)`
Schedule the specified one-time task, either right away if possible or on initialization of the scheduler.
`protected void`
`scheduleTasks()`
Schedule all registered tasks against the underlying task scheduler.
`ScheduledTask`
`scheduleTriggerTask(TriggerTask task)`
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 of `CronTask` 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 of `IntervalTask` 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 of `IntervalTask` objects.
`void`
`setObservationRegistry(io.micrometer.observation.ObservationRegistry observationRegistry)`
Configure an `ObservationRegistry` to record observations for scheduled tasks.
`void`
`setScheduler(Object scheduler)`
Set the `TaskScheduler` to register scheduled tasks with, or a `ScheduledExecutorService` to be wrapped as a `TaskScheduler`.
`void`
`setTaskScheduler(TaskScheduler taskScheduler)`
Set the `TaskScheduler` 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 the `Trigger` interface).
`void`
`setTriggerTasksList(List<TriggerTask> triggerTasks)`
Specify triggered tasks as a list of `TriggerTask` objects.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### CRON_DISABLED

public static final String 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 supplied `expression` is retrieved from an external source — for example, from a property in the `Environment`.

Since:
  5.2
See Also:
  - `Scheduled.CRON_DISABLED`
  - Constant Field Values

## Constructor Details

### ScheduledTaskRegistrar

public ScheduledTaskRegistrar()

## Method Details

### setTaskScheduler

public void setTaskScheduler(TaskScheduler taskScheduler)
Set the `TaskScheduler` to register scheduled tasks with.

### setScheduler

public void setScheduler(@Nullable Object scheduler)
Set the `TaskScheduler` to register scheduled tasks with, or a `ScheduledExecutorService` to be wrapped as a `TaskScheduler`.

### getScheduler

@Nullable public TaskScheduler getScheduler()
Return the `TaskScheduler` instance for this registrar (may be `null`).

### setObservationRegistry

public void setObservationRegistry(@Nullable io.micrometer.observation.ObservationRegistry observationRegistry)
Configure an `ObservationRegistry` to record observations for scheduled tasks.

Since:
  6.1

### getObservationRegistry

@Nullable public io.micrometer.observation.ObservationRegistry getObservationRegistry()
Return the `ObservationRegistry` for this registrar.

Since:
  6.1

### setTriggerTasks

public void setTriggerTasks(Map<Runnable,Trigger> triggerTasks)
Specify triggered tasks as a Map of Runnables (the tasks) and Trigger objects (typically custom implementations of the `Trigger` interface).

### setTriggerTasksList

public void setTriggerTasksList(List<TriggerTask> triggerTasks)
Specify triggered tasks as a list of `TriggerTask` objects. Primarily used by `<task:*>` namespace parsing.

Since:
  3.2
See Also:
  - `ScheduledTasksBeanDefinitionParser`

### getTriggerTaskList

public List<TriggerTask> getTriggerTaskList()
Get the trigger tasks as an unmodifiable list of `TriggerTask` objects.

Returns:
  the list of tasks (never `null`)
Since:
  4.2

### setCronTasks

public void setCronTasks(Map<Runnable,String> cronTasks)
Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.

See Also:
  - `CronTrigger`

### setCronTasksList

public void setCronTasksList(List<CronTask> cronTasks)
Specify triggered tasks as a list of `CronTask` objects. Primarily used by `<task:*>` namespace parsing.

Since:
  3.2
See Also:
  - `ScheduledTasksBeanDefinitionParser`

### getCronTaskList

public List<CronTask> getCronTaskList()
Get the cron tasks as an unmodifiable list of `CronTask` objects.

Returns:
  the list of tasks (never `null`)
Since:
  4.2

### setFixedRateTasks

public void setFixedRateTasks(Map<Runnable,Long> fixedRateTasks)
Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.

See Also:
  - `TaskScheduler.scheduleAtFixedRate(Runnable, Duration)`

### setFixedRateTasksList

public void setFixedRateTasksList(List<IntervalTask> fixedRateTasks)
Specify fixed-rate tasks as a list of `IntervalTask` objects. Primarily used by `<task:*>` namespace parsing.

Since:
  3.2
See Also:
  - `ScheduledTasksBeanDefinitionParser`

### getFixedRateTaskList

public List<IntervalTask> getFixedRateTaskList()
Get the fixed-rate tasks as an unmodifiable list of `IntervalTask` objects.

Returns:
  the list of tasks (never `null`)
Since:
  4.2

### setFixedDelayTasks

public void setFixedDelayTasks(Map<Runnable,Long> fixedDelayTasks)
Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.

See Also:
  - `TaskScheduler.scheduleWithFixedDelay(Runnable, Duration)`

### setFixedDelayTasksList

public void setFixedDelayTasksList(List<IntervalTask> fixedDelayTasks)
Specify fixed-delay tasks as a list of `IntervalTask` objects. Primarily used by `<task:*>` namespace parsing.

Since:
  3.2
See Also:
  - `ScheduledTasksBeanDefinitionParser`

### getFixedDelayTaskList

public List<IntervalTask> getFixedDelayTaskList()
Get the fixed-delay tasks as an unmodifiable list of `IntervalTask` objects.

Returns:
  the list of tasks (never `null`)
Since:
  4.2

### addTriggerTask

public void addTriggerTask(Runnable task, Trigger trigger)
Add a Runnable task to be triggered per the given `Trigger`.

See Also:
  - `TaskScheduler.scheduleAtFixedRate(Runnable, Duration)`

### addTriggerTask

public void addTriggerTask(TriggerTask task)
Add a `TriggerTask`.

Since:
  3.2
See Also:
  - `TaskScheduler.scheduleAtFixedRate(Runnable, Duration)`

### addCronTask

public void addCronTask(Runnable task, String expression)
Add a `Runnable` task to be triggered per the given cron `expression`.

This method will not register the task if the `expression` is equal to `CRON_DISABLED`.

### addCronTask

public void addCronTask(CronTask task)
Add a `CronTask`.

Since:
  3.2

### addFixedRateTask

@Deprecated(since="6.0") public void addFixedRateTask(Runnable task, long interval)
Deprecated.
as of 6.0, in favor of `addFixedRateTask(Runnable, Duration)`
Add a `Runnable` task to be triggered at the given fixed-rate interval.

### addFixedRateTask

public void addFixedRateTask(Runnable task, Duration interval)
Add a `Runnable` task to be triggered at the given fixed-rate interval.

Since:
  6.0
See Also:
  - `TaskScheduler.scheduleAtFixedRate(Runnable, Duration)`

### addFixedRateTask

public void addFixedRateTask(IntervalTask task)
Add a fixed-rate `IntervalTask`.

Since:
  3.2
See Also:
  - `TaskScheduler.scheduleAtFixedRate(Runnable, Duration)`
  - `FixedRateTask`

### addFixedDelayTask

@Deprecated(since="6.0") public void addFixedDelayTask(Runnable task, long interval)
Deprecated.
as of 6.0, in favor of `addFixedDelayTask(Runnable, Duration)`
Add a Runnable task to be triggered with the given fixed delay.

### addFixedDelayTask

public void addFixedDelayTask(Runnable task, Duration interval)
Add a Runnable task to be triggered with the given fixed delay.

Since:
  6.0
See Also:
  - `TaskScheduler.scheduleWithFixedDelay(Runnable, Duration)`

### addFixedDelayTask

public void addFixedDelayTask(IntervalTask task)
Add a fixed-delay `IntervalTask`.

Since:
  3.2
See Also:
  - `TaskScheduler.scheduleWithFixedDelay(Runnable, Duration)`
  - `FixedDelayTask`

### addOneTimeTask

public void addOneTimeTask(Runnable task, Duration initialDelay)
Add a Runnable task to be triggered once after the given initial delay.

Since:
  6.1
See Also:
  - `TaskScheduler.schedule(Runnable, Instant)`

### addOneTimeTask

public void addOneTimeTask(DelayedTask task)
Add a one-time `DelayedTask`.

Since:
  6.1
See Also:
  - `TaskScheduler.schedule(Runnable, Instant)`
  - `OneTimeTask`

### hasTasks

public boolean hasTasks()
Return whether this `ScheduledTaskRegistrar` has any tasks registered.

Since:
  3.2

### afterPropertiesSet

public void afterPropertiesSet()
Calls `scheduleTasks()` at bean construction time.

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### scheduleTasks

protected void scheduleTasks()
Schedule all registered tasks against the underlying task scheduler.

### scheduleTriggerTask

@Nullable public ScheduledTask scheduleTriggerTask(TriggerTask task)
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

@Nullable public ScheduledTask scheduleCronTask(CronTask task)
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

@Nullable public ScheduledTask scheduleFixedRateTask(FixedRateTask task)
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

@Nullable public ScheduledTask scheduleFixedDelayTask(FixedDelayTask task)
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

@Nullable public ScheduledTask scheduleOneTimeTask(OneTimeTask task)
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

public Set<ScheduledTask> getScheduledTasks()
Return all locally registered tasks that have been scheduled by this registrar.

Specified by:
  `getScheduledTasks` in interface `ScheduledTaskHolder`
Since:
  5.0.2
See Also:
  - `addTriggerTask(java.lang.Runnable, org.springframework.scheduling.Trigger)`
  - `addCronTask(java.lang.Runnable, java.lang.String)`
  - `addFixedRateTask(java.lang.Runnable, long)`
  - `addFixedDelayTask(java.lang.Runnable, long)`

### destroy

public void destroy()

Specified by:
  `destroy` in interface `org.springframework.beans.factory.DisposableBean`