Top Description Fields Constructors Methods
org.springframework.scheduling.config

public Class ScheduledTaskRegistrar

extends Object
implements ScheduledTaskHolder, InitializingBean, DisposableBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.scheduling.config.ScheduledTaskHolder
Known Direct Subclasses
org.springframework.scheduling.config.ContextLifecycleScheduledTaskRegistrar
Imports
java.time.Duration, .Instant, java.util.ArrayList, .Collections, .HashMap, .LinkedHashSet, .List, .Map, .Set, java.util.concurrent.Executors, .ScheduledExecutorService, io.micrometer.observation.ObservationRegistry, org.springframework.beans.factory.DisposableBean, .InitializingBean, org.springframework.lang.Nullable, org.springframework.scheduling.TaskScheduler, .Trigger, org.springframework.scheduling.concurrent.ConcurrentTaskScheduler, org.springframework.scheduling.support.CronTrigger, org.springframework.util.Assert, .CollectionUtils

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.

Authors
Juergen Hoeller, Chris Beams, Tobias Montagna-Hay, Sam Brannen, Arjen Poutsma, Brian Clozel
Since
3.0
See Also
org.springframework.scheduling.annotation.EnableAsync, org.springframework.scheduling.annotation.SchedulingConfigurer

Field Summary

Modifier and TypeField and Description
public static final String
CRON_DISABLED

A special cron expression value that indicates a disabled trigger: -.

private List<CronTask>
private List<IntervalTask>
private List<IntervalTask>
private ScheduledExecutorService
private ObservationRegistry
private List<DelayedTask>
private final Set<ScheduledTask>
private TaskScheduler
private List<TriggerTask>
private final Map<Task, ScheduledTask>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addCronTask(Runnable task, String expression)

Add a Runnable task to be triggered per the given cron expression.

public void
public void
addFixedDelayTask(Runnable task, long interval)
Deprecated since 6.0. as of 6.0, in favor of addFixedDelayTask(Runnable, Duration)

Add a Runnable task to be triggered with the given fixed delay.

public void
addFixedDelayTask(Runnable task, Duration interval)

Add a Runnable task to be triggered with the given fixed delay.

public void
public void
addFixedRateTask(Runnable task, long interval)
Deprecated since 6.0. as of 6.0, in favor of addFixedRateTask(Runnable, Duration)

Add a Runnable task to be triggered at the given fixed-rate interval.

public void
addFixedRateTask(Runnable task, Duration interval)

Add a Runnable task to be triggered at the given fixed-rate interval.

public void
public void
addOneTimeTask(Runnable task, Duration initialDelay)

Add a Runnable task to be triggered once after the given initial delay.

public void
addOneTimeTask(DelayedTask task)

Add a one-time DelayedTask.

private void
public void
addTriggerTask(Runnable task, Trigger trigger)

Add a Runnable task to be triggered per the given Trigger.

public void
addTriggerTask(TriggerTask task)

Add a TriggerTask.

public void
public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Invoked by the containing BeanFactory on destruction of a bean.

public List<CronTask>

Returns:

the list of tasks (never null)
getCronTaskList
()

Get the cron tasks as an unmodifiable list of CronTask objects.

public List<IntervalTask>

Returns:

the list of tasks (never null)
getFixedDelayTaskList
()

Get the fixed-delay tasks as an unmodifiable list of IntervalTask objects.

public List<IntervalTask>

Returns:

the list of tasks (never null)
getFixedRateTaskList
()

Get the fixed-rate tasks as an unmodifiable list of IntervalTask objects.

public ObservationRegistry
getObservationRegistry()

Return the ObservationRegistry for this registrar.

public Set<ScheduledTask>
getScheduledTasks()

Implements org.springframework.scheduling.config.ScheduledTaskHolder.getScheduledTasks.

Return all locally registered tasks that have been scheduled by this registrar.

public TaskScheduler
getScheduler()

Return the TaskScheduler instance for this registrar (may be null).

public List<TriggerTask>

Returns:

the list of tasks (never null)
getTriggerTaskList
()

Get the trigger tasks as an unmodifiable list of TriggerTask objects.

public boolean
hasTasks()

Return whether this ScheduledTaskRegistrar has any tasks registered.

public ScheduledTask

Returns:

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)
scheduleCronTask
(CronTask task)

Schedule the specified cron task, either right away if possible or on initialization of the scheduler.

public ScheduledTask

Returns:

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)
scheduleFixedDelayTask
(FixedDelayTask task)

Schedule the specified fixed-delay task, either right away if possible or on initialization of the scheduler.

public ScheduledTask

Returns:

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)
scheduleFixedRateTask
(FixedRateTask task)

Schedule the specified fixed-rate task, either right away if possible or on initialization of the scheduler.

public ScheduledTask

Returns:

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)
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.

public ScheduledTask

Returns:

a handle to the scheduled task, allowing to cancel it
scheduleTriggerTask
(TriggerTask task)

Schedule the specified trigger task, either right away if possible or on initialization of the scheduler.

public void
setCronTasks(Map<Runnable, String> cronTasks)

Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.

public void
setCronTasksList(List<CronTask> cronTasks)

Specify triggered tasks as a list of CronTask objects.

public void
setFixedDelayTasks(Map<Runnable, Long> fixedDelayTasks)

Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.

public void
setFixedDelayTasksList(List<IntervalTask> fixedDelayTasks)

Specify fixed-delay tasks as a list of IntervalTask objects.

public void
setFixedRateTasks(Map<Runnable, Long> fixedRateTasks)

Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.

public void
setFixedRateTasksList(List<IntervalTask> fixedRateTasks)

Specify fixed-rate tasks as a list of IntervalTask objects.

public void
setObservationRegistry(ObservationRegistry observationRegistry)

Configure an ObservationRegistry to record observations for scheduled tasks.

public void
setScheduler(Object scheduler)

Set the TaskScheduler to register scheduled tasks with, or a java.util.concurrent.ScheduledExecutorService to be wrapped as a TaskScheduler.

public void
setTaskScheduler(TaskScheduler taskScheduler)

Set the TaskScheduler to register scheduled tasks with.

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).

public void
setTriggerTasksList(List<TriggerTask> triggerTasks)

Specify triggered tasks as a list of TriggerTask objects.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CRON_DISABLEDback to summary
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
org.springframework.scheduling.annotation.Scheduled#CRON_DISABLED
cronTasksback to summary
private List<CronTask> cronTasks
Annotations
@Nullable
fixedDelayTasksback to summary
private List<IntervalTask> fixedDelayTasks
Annotations
@Nullable
fixedRateTasksback to summary
private List<IntervalTask> fixedRateTasks
Annotations
@Nullable
localExecutorback to summary
private ScheduledExecutorService localExecutor
Annotations
@Nullable
observationRegistryback to summary
private ObservationRegistry observationRegistry
Annotations
@Nullable
oneTimeTasksback to summary
private List<DelayedTask> oneTimeTasks
Annotations
@Nullable
scheduledTasksback to summary
private final Set<ScheduledTask> scheduledTasks
taskSchedulerback to summary
private TaskScheduler taskScheduler
Annotations
@Nullable
triggerTasksback to summary
private List<TriggerTask> triggerTasks
Annotations
@Nullable
unresolvedTasksback to summary
private final Map<Task, ScheduledTask> unresolvedTasks

Constructor Detail

ScheduledTaskRegistrarback to summary
public ScheduledTaskRegistrar()

Method Detail

addCronTaskback to summary
public void addCronTask(Runnable task, String expression)

Add a Runnable task to be triggered per the given cron expression.

As of Spring Framework 5.2, this method will not register the task if the expression is equal to CRON_DISABLED.

addCronTaskback to summary
public void addCronTask(CronTask task)

Add a CronTask.

Since
3.2
addFixedDelayTaskback to summary
public void addFixedDelayTask(Runnable task, long interval)

Deprecated

since 6.0.

as of 6.0, in favor of addFixedDelayTask(Runnable, Duration)

Add a Runnable task to be triggered with the given fixed delay.

Annotations
@Deprecated
since:6.0
addFixedDelayTaskback to summary
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)
addFixedDelayTaskback to summary
public void addFixedDelayTask(IntervalTask task)

Add a fixed-delay IntervalTask.

Since
3.2
See Also
TaskScheduler#scheduleWithFixedDelay(Runnable, Duration), FixedDelayTask
addFixedRateTaskback to summary
public void addFixedRateTask(Runnable task, long interval)

Deprecated

since 6.0.

as of 6.0, in favor of addFixedRateTask(Runnable, Duration)

Add a Runnable task to be triggered at the given fixed-rate interval.

Annotations
@Deprecated
since:6.0
addFixedRateTaskback to summary
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)
addFixedRateTaskback to summary
public void addFixedRateTask(IntervalTask task)

Add a fixed-rate IntervalTask.

Since
3.2
See Also
TaskScheduler#scheduleAtFixedRate(Runnable, Duration), FixedRateTask
addOneTimeTaskback to summary
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)
addOneTimeTaskback to summary
public void addOneTimeTask(DelayedTask task)

Add a one-time DelayedTask.

Since
6.1
See Also
TaskScheduler#schedule(Runnable, Instant), OneTimeTask
addScheduledTaskback to summary
private void addScheduledTask(ScheduledTask task)
addTriggerTaskback to summary
public void addTriggerTask(Runnable task, Trigger trigger)

Add a Runnable task to be triggered per the given Trigger.

See Also
TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
addTriggerTaskback to summary
public void addTriggerTask(TriggerTask task)

Add a TriggerTask.

Since
3.2
See Also
TaskScheduler#scheduleAtFixedRate(Runnable, Duration)
afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Calls scheduleTasks() at bean construction time.

Annotations
@Override
destroyback to summary
public void destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Doc from org.springframework.beans.factory.DisposableBean.destroy.

Invoked by the containing BeanFactory on destruction of a bean.

Annotations
@Override
getCronTaskListback to summary
public List<CronTask> getCronTaskList()

Get the cron tasks as an unmodifiable list of CronTask objects.

Returns:List<CronTask>

the list of tasks (never null)

Since
4.2
getFixedDelayTaskListback to summary
public List<IntervalTask> getFixedDelayTaskList()

Get the fixed-delay tasks as an unmodifiable list of IntervalTask objects.

Returns:List<IntervalTask>

the list of tasks (never null)

Since
4.2
getFixedRateTaskListback to summary
public List<IntervalTask> getFixedRateTaskList()

Get the fixed-rate tasks as an unmodifiable list of IntervalTask objects.

Returns:List<IntervalTask>

the list of tasks (never null)

Since
4.2
getObservationRegistryback to summary
public ObservationRegistry getObservationRegistry()

Return the ObservationRegistry for this registrar.

Annotations
@Nullable
Since
6.1
getScheduledTasksback to summary
public Set<ScheduledTask> getScheduledTasks()

Implements org.springframework.scheduling.config.ScheduledTaskHolder.getScheduledTasks.

Return all locally registered tasks that have been scheduled by this registrar.

Annotations
@Override
Since
5.0.2
See Also
addTriggerTask, addCronTask, addFixedRateTask, addFixedDelayTask
getSchedulerback to summary
public TaskScheduler getScheduler()

Return the TaskScheduler instance for this registrar (may be null).

Annotations
@Nullable
getTriggerTaskListback to summary
public List<TriggerTask> getTriggerTaskList()

Get the trigger tasks as an unmodifiable list of TriggerTask objects.

Returns:List<TriggerTask>

the list of tasks (never null)

Since
4.2
hasTasksback to summary
public boolean hasTasks()

Return whether this ScheduledTaskRegistrar has any tasks registered.

Since
3.2
scheduleCronTaskback to summary
public ScheduledTask scheduleCronTask(CronTask task)

Schedule the specified cron task, either right away if possible or on initialization of the scheduler.

Returns:ScheduledTask

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)

Annotations
@Nullable
Since
4.3
scheduleFixedDelayTaskback to summary
public ScheduledTask scheduleFixedDelayTask(FixedDelayTask task)

Schedule the specified fixed-delay task, either right away if possible or on initialization of the scheduler.

Returns:ScheduledTask

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)

Annotations
@Nullable
Since
5.0.2
scheduleFixedRateTaskback to summary
public ScheduledTask scheduleFixedRateTask(FixedRateTask task)

Schedule the specified fixed-rate task, either right away if possible or on initialization of the scheduler.

Returns:ScheduledTask

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)

Annotations
@Nullable
Since
5.0.2
scheduleOneTimeTaskback to summary
public ScheduledTask scheduleOneTimeTask(OneTimeTask task)

Schedule the specified one-time task, either right away if possible or on initialization of the scheduler.

Returns:ScheduledTask

a handle to the scheduled task, allowing to cancel it (or null if processing a previously registered task)

Annotations
@Nullable
Since
6.1
scheduleTasksback to summary
protected void scheduleTasks()

Schedule all registered tasks against the underlying task scheduler.

scheduleTriggerTaskback to summary
public ScheduledTask scheduleTriggerTask(TriggerTask task)

Schedule the specified trigger task, either right away if possible or on initialization of the scheduler.

Returns:ScheduledTask

a handle to the scheduled task, allowing to cancel it

Annotations
@Nullable
Since
4.3
setCronTasksback to summary
public void setCronTasks(Map<Runnable, String> cronTasks)

Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.

See Also
CronTrigger
setCronTasksListback to summary
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
setFixedDelayTasksback to summary
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)
setFixedDelayTasksListback to summary
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
setFixedRateTasksback to summary
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)
setFixedRateTasksListback to summary
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
setObservationRegistryback to summary
public void setObservationRegistry(ObservationRegistry observationRegistry)

Configure an ObservationRegistry to record observations for scheduled tasks.

Since
6.1
setSchedulerback to summary
public void setScheduler(Object scheduler)

Set the TaskScheduler to register scheduled tasks with, or a java.util.concurrent.ScheduledExecutorService to be wrapped as a TaskScheduler.

setTaskSchedulerback to summary
public void setTaskScheduler(TaskScheduler taskScheduler)

Set the TaskScheduler to register scheduled tasks with.

setTriggerTasksback to summary
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).

setTriggerTasksListback to summary
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