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 aTaskScheduler
, typically using cron expressions.As of Spring 3.1,
ScheduledTaskRegistrar
has a more prominent user-facing role when used in conjunction with the@EnableAsync
annotation and itsSchedulingConfigurer
callback interface.- Since:
- 3.0
- Author:
- Juergen Hoeller, Chris Beams, Tobias Montagna-Hay, Sam Brannen
- See Also:
EnableAsync
,SchedulingConfigurer
Field Summary
Fields Modifier and Type Field Description static String
CRON_DISABLED
A special cron expression value that indicates a disabled trigger: "-".
Constructor Summary
Constructors Constructor Description ScheduledTaskRegistrar()
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
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 delay)
Add a Runnable task to be triggered with the given fixed delay.void
addFixedDelayTask(IntervalTask task)
Add a fixed-delayIntervalTask
.void
addFixedRateTask(Runnable task, long interval)
Add aRunnable
task to be triggered at the given fixed-rate interval.void
addFixedRateTask(IntervalTask task)
Add a fixed-rateIntervalTask
.void
addTriggerTask(Runnable task, Trigger trigger)
Add a Runnable task to be triggered per the givenTrigger
.void
addTriggerTask(TriggerTask task)
Add aTriggerTask
.void
afterPropertiesSet()
CallsscheduleTasks()
at bean construction time.void
destroy()
Invoked by the containingBeanFactory
on destruction of a bean.List<CronTask>
getCronTaskList()
Get the cron tasks as an unmodifiable list ofCronTask
objects.List<IntervalTask>
getFixedDelayTaskList()
Get the fixed-delay tasks as an unmodifiable list ofIntervalTask
objects.List<IntervalTask>
getFixedRateTaskList()
Get the fixed-rate tasks as an unmodifiable list ofIntervalTask
objects.Set<ScheduledTask>
getScheduledTasks()
Return all locally registered tasks that have been scheduled by this registrar.TaskScheduler
getScheduler()
Return theTaskScheduler
instance for this registrar (may benull
).List<TriggerTask>
getTriggerTaskList()
Get the trigger tasks as an unmodifiable list ofTriggerTask
objects.boolean
hasTasks()
Return whether thisScheduledTaskRegistrar
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
scheduleFixedDelayTask(IntervalTask task)
Deprecated.as of 5.0.2, in favor ofscheduleFixedDelayTask(FixedDelayTask)
ScheduledTask
scheduleFixedRateTask(FixedRateTask task)
Schedule the specified fixed-rate task, either right away if possible or on initialization of the scheduler.ScheduledTask
scheduleFixedRateTask(IntervalTask task)
Deprecated.as of 5.0.2, in favor ofscheduleFixedRateTask(FixedRateTask)
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 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
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 Detail
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 suppliedexpression
is retrieved from an external source — for example, from a property in theEnvironment
.- Since:
- 5.2
- See Also:
Scheduled.CRON_DISABLED
, Constant Field Values
Constructor Detail
ScheduledTaskRegistrar
public ScheduledTaskRegistrar()
Method Detail
setTaskScheduler
public void setTaskScheduler(TaskScheduler taskScheduler)
Set theTaskScheduler
to register scheduled tasks with.
setScheduler
public void setScheduler(@Nullable Object scheduler)
Set theTaskScheduler
to register scheduled tasks with, or aScheduledExecutorService
to be wrapped as aTaskScheduler
.
getScheduler
@Nullable public TaskScheduler getScheduler()
Return theTaskScheduler
instance for this registrar (may benull
).
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 theTrigger
interface).
setTriggerTasksList
public void setTriggerTasksList(List<TriggerTask> triggerTasks)
Specify triggered tasks as a list ofTriggerTask
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 ofTriggerTask
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 ofCronTask
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 ofCronTask
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.
setFixedRateTasksList
public void setFixedRateTasksList(List<IntervalTask> fixedRateTasks)
Specify fixed-rate tasks as a list ofIntervalTask
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 ofIntervalTask
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.
setFixedDelayTasksList
public void setFixedDelayTasksList(List<IntervalTask> fixedDelayTasks)
Specify fixed-delay tasks as a list ofIntervalTask
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 ofIntervalTask
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 givenTrigger
.
addTriggerTask
public void addTriggerTask(TriggerTask task)
Add aTriggerTask
.- Since:
- 3.2
- See Also:
TaskScheduler.scheduleAtFixedRate(Runnable, long)
addCronTask
public void addCronTask(Runnable task, String expression)
Add aRunnable
task to be triggered per the given cronexpression
.As of Spring Framework 5.2, this method will not register the task if the
expression
is equal toCRON_DISABLED
.
addCronTask
public void addCronTask(CronTask task)
Add aCronTask
.- Since:
- 3.2
addFixedRateTask
public void addFixedRateTask(Runnable task, long interval)
Add aRunnable
task to be triggered at the given fixed-rate interval.
addFixedRateTask
public void addFixedRateTask(IntervalTask task)
Add a fixed-rateIntervalTask
.- Since:
- 3.2
- See Also:
TaskScheduler.scheduleAtFixedRate(Runnable, long)
addFixedDelayTask
public void addFixedDelayTask(Runnable task, long delay)
Add a Runnable task to be triggered with the given fixed delay.
addFixedDelayTask
public void addFixedDelayTask(IntervalTask task)
Add a fixed-delayIntervalTask
.- Since:
- 3.2
- See Also:
TaskScheduler.scheduleWithFixedDelay(Runnable, long)
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
@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
@Deprecated @Nullable public ScheduledTask scheduleFixedRateTask(IntervalTask task)
Deprecated.as of 5.0.2, in favor ofscheduleFixedRateTask(FixedRateTask)
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:
- 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
@Deprecated @Nullable public ScheduledTask scheduleFixedDelayTask(IntervalTask task)
Deprecated.as of 5.0.2, in favor ofscheduleFixedDelayTask(FixedDelayTask)
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:
- 4.3
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
getScheduledTasks
public Set<ScheduledTask> 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