public class ScheduledExecutorTask
extends java.lang.Object
Runnable
and a delay plus period. The period needs to be specified;
there is no point in a default for it.
The ScheduledExecutorService
does not offer
more sophisticated scheduling options such as cron expressions.
Consider using ThreadPoolTaskScheduler
for such needs.
Note that the 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.
ScheduledExecutorService.scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
,
ScheduledExecutorService.scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
Modifier and Type | Field and Description |
---|---|
private long |
delay |
private boolean |
fixedRate |
private long |
period |
private java.lang.Runnable |
runnable |
private java.util.concurrent.TimeUnit |
timeUnit |
Constructor and Description |
---|
ScheduledExecutorTask()
Create a new ScheduledExecutorTask,
to be populated via bean properties.
|
ScheduledExecutorTask(java.lang.Runnable executorTask)
Create a new ScheduledExecutorTask, with default
one-time execution without delay.
|
ScheduledExecutorTask(java.lang.Runnable executorTask,
long delay)
Create a new ScheduledExecutorTask, with default
one-time execution with the given delay.
|
ScheduledExecutorTask(java.lang.Runnable executorTask,
long delay,
long period,
boolean fixedRate)
Create a new ScheduledExecutorTask.
|
Modifier and Type | Method and Description |
---|---|
long |
getDelay()
Return the delay before starting the job for the first time.
|
long |
getPeriod()
Return the period between repeated task executions.
|
java.lang.Runnable |
getRunnable()
Return the Runnable to schedule as executor task.
|
java.util.concurrent.TimeUnit |
getTimeUnit()
Return the time unit for the delay and period values.
|
boolean |
isFixedRate()
Return whether to schedule as fixed-rate execution.
|
boolean |
isOneTimeTask()
Is this task only ever going to execute once?
|
void |
setDelay(long delay)
Set the delay before starting the task for the first time,
in milliseconds.
|
void |
setFixedRate(boolean fixedRate)
Set whether to schedule as fixed-rate execution, rather than
fixed-delay execution.
|
void |
setPeriod(long period)
Set the period between repeated task executions, in milliseconds.
|
void |
setRunnable(java.lang.Runnable executorTask)
Set the Runnable to schedule as executor task.
|
void |
setTimeUnit(java.util.concurrent.TimeUnit timeUnit)
Specify the time unit for the delay and period values.
|
@Nullable private java.lang.Runnable runnable
private long delay
private long period
private java.util.concurrent.TimeUnit timeUnit
private boolean fixedRate
public ScheduledExecutorTask()
setDelay(long)
,
setPeriod(long)
,
setFixedRate(boolean)
public ScheduledExecutorTask(java.lang.Runnable executorTask)
executorTask
- the Runnable to schedulepublic ScheduledExecutorTask(java.lang.Runnable executorTask, long delay)
executorTask
- the Runnable to scheduledelay
- the delay before starting the task for the first time (ms)public ScheduledExecutorTask(java.lang.Runnable executorTask, long delay, long period, boolean fixedRate)
executorTask
- the Runnable to scheduledelay
- the delay before starting the task for the first time (ms)period
- the period between repeated task executions (ms)fixedRate
- whether to schedule as fixed-rate executionpublic void setRunnable(java.lang.Runnable executorTask)
public java.lang.Runnable getRunnable()
public void setDelay(long delay)
public long getDelay()
public void setPeriod(long period)
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!
setFixedRate(boolean)
,
isOneTimeTask()
,
ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, java.util.concurrent.TimeUnit)
public long getPeriod()
public boolean isOneTimeTask()
true
if this task is only ever going to execute oncegetPeriod()
public void setTimeUnit(@Nullable java.util.concurrent.TimeUnit timeUnit)
TimeUnit.MILLISECONDS
).TimeUnit.MILLISECONDS
,
TimeUnit.SECONDS
public java.util.concurrent.TimeUnit getTimeUnit()
public void setFixedRate(boolean fixedRate)
See ScheduledExecutorService javadoc for details on those execution modes.
ScheduledExecutorService.scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
,
ScheduledExecutorService.scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)
public boolean isFixedRate()