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

public Class ScheduledExecutorTask

extends Object
Class Inheritance
Imports
java.util.concurrent.TimeUnit, org.springframework.lang.Nullable, org.springframework.util.Assert

JavaBean that describes a scheduled executor task, consisting of the Runnable and a delay plus period. The period needs to be specified; there is no point in a default for it.

The java.util.concurrent.ScheduledExecutorService does not offer more sophisticated scheduling options such as cron expressions. Consider using ThreadPoolTaskScheduler for such needs.

Note that the java.util.concurrent.ScheduledExecutorService mechanism uses a Runnable instance that is shared between repeated executions, in contrast to Quartz which creates a new Job instance for each execution.

Author
Juergen Hoeller
Since
2.0
See Also
java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit), java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)

Field Summary

Modifier and TypeField and Description
private long
private boolean
private long
private Runnable
private TimeUnit

Constructor Summary

AccessConstructor and Description
public
ScheduledExecutorTask()

Create a new ScheduledExecutorTask, to be populated via bean properties.

public
ScheduledExecutorTask(Runnable
the Runnable to schedule
executorTask
)

Create a new ScheduledExecutorTask, with default one-time execution without delay.

public
ScheduledExecutorTask(Runnable
the Runnable to schedule
executorTask
,
long
the delay before starting the task for the first time (ms)
delay
)

Create a new ScheduledExecutorTask, with default one-time execution with the given delay.

public
ScheduledExecutorTask(Runnable
the Runnable to schedule
executorTask
,
long
the delay before starting the task for the first time (ms)
delay
,
long
the period between repeated task executions (ms)
period
,
boolean
whether to schedule as fixed-rate execution
fixedRate
)

Create a new ScheduledExecutorTask.

Method Summary

Modifier and TypeMethod and Description
public long
getDelay()

Return the delay before starting the job for the first time.

public long
getPeriod()

Return the period between repeated task executions.

public Runnable
getRunnable()

Return the Runnable to schedule as executor task.

public TimeUnit
getTimeUnit()

Return the time unit for the delay and period values.

public boolean
isFixedRate()

Return whether to schedule as fixed-rate execution.

public boolean

Returns:

true if this task is only ever going to execute once
isOneTimeTask
()

Is this task only ever going to execute once?

public void
setDelay(long delay)

Set the delay before starting the task for the first time, in milliseconds.

public void
setFixedRate(boolean fixedRate)

Set whether to schedule as fixed-rate execution, rather than fixed-delay execution.

public void
setPeriod(long period)

Set the period between repeated task executions, in milliseconds.

public void
setRunnable(Runnable executorTask)

Set the Runnable to schedule as executor task.

public void
setTimeUnit(TimeUnit timeUnit)

Specify the time unit for the delay and period values.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

delayback to summary
private long delay
fixedRateback to summary
private boolean fixedRate
periodback to summary
private long period
runnableback to summary
private Runnable runnable
Annotations
@Nullable
timeUnitback to summary
private TimeUnit timeUnit

Constructor Detail

ScheduledExecutorTaskback to summary
public ScheduledExecutorTask()

Create a new ScheduledExecutorTask, to be populated via bean properties.

See Also
setDelay, setPeriod, setFixedRate
ScheduledExecutorTaskback to summary
public ScheduledExecutorTask(Runnable executorTask)

Create a new ScheduledExecutorTask, with default one-time execution without delay.

Parameters
executorTask:Runnable

the Runnable to schedule

ScheduledExecutorTaskback to summary
public ScheduledExecutorTask(Runnable executorTask, long delay)

Create a new ScheduledExecutorTask, with default one-time execution with the given delay.

Parameters
executorTask:Runnable

the Runnable to schedule

delay:long

the delay before starting the task for the first time (ms)

ScheduledExecutorTaskback to summary
public ScheduledExecutorTask(Runnable executorTask, long delay, long period, boolean fixedRate)

Create a new ScheduledExecutorTask.

Parameters
executorTask:Runnable

the Runnable to schedule

delay:long

the delay before starting the task for the first time (ms)

period:long

the period between repeated task executions (ms)

fixedRate:boolean

whether to schedule as fixed-rate execution

Method Detail

getDelayback to summary
public long getDelay()

Return the delay before starting the job for the first time.

getPeriodback to summary
public long getPeriod()

Return the period between repeated task executions.

getRunnableback to summary
public Runnable getRunnable()

Return the Runnable to schedule as executor task.

getTimeUnitback to summary
public TimeUnit getTimeUnit()

Return the time unit for the delay and period values.

isFixedRateback to summary
public boolean isFixedRate()

Return whether to schedule as fixed-rate execution.

isOneTimeTaskback to summary
public boolean isOneTimeTask()

Is this task only ever going to execute once?

Returns:boolean

true if this task is only ever going to execute once

See Also
getPeriod()
setDelayback to summary
public void setDelay(long delay)

Set the delay before starting the task for the first time, in milliseconds. Default is 0, immediately starting the task after successful scheduling.

setFixedRateback to summary
public void setFixedRate(boolean fixedRate)

Set whether to schedule as fixed-rate execution, rather than fixed-delay execution. Default is "false", that is, fixed delay.

See ScheduledExecutorService javadoc for details on those execution modes.

See Also
java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit), java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
setPeriodback to summary
public void setPeriod(long period)

Set the period between repeated task executions, in milliseconds.

Default is -1, leading to one-time execution. In case of a positive value, the task will be executed repeatedly, with the given interval in-between executions.

Note that the semantics of the period value vary between fixed-rate and fixed-delay execution.

Note

A period of 0 (for example as fixed delay) is not supported, simply because java.util.concurrent.ScheduledExecutorService itself does not support it. Hence a value of 0 will be treated as one-time execution; however, that value should never be specified explicitly in the first place!

See Also
setFixedRate, isOneTimeTask(), java.util.concurrent.ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, java.util.concurrent.TimeUnit)
setRunnableback to summary
public void setRunnable(Runnable executorTask)

Set the Runnable to schedule as executor task.

setTimeUnitback to summary
public void setTimeUnit(TimeUnit timeUnit)

Specify the time unit for the delay and period values. Default is milliseconds (TimeUnit.MILLISECONDS).

See Also
java.util.concurrent.TimeUnit#MILLISECONDS, java.util.concurrent.TimeUnit#SECONDS