The Spring Framework

org.springframework.scheduling.timer
Class ScheduledTimerTask

java.lang.Object
  extended by org.springframework.scheduling.timer.ScheduledTimerTask

public class ScheduledTimerTask
extends Object

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

The JDK's Timer facility does not offer sophisticated scheduling options such as cron expressions. Consider using Quartz for such advanced needs.

Note that the Timer mechanism uses a TimerTask instance that is shared between repeated executions, in contrast to Quartz which creates a new Job instance for each execution.

Since:
19.02.2004
Author:
Juergen Hoeller
See Also:
TimerTask, Timer.schedule(TimerTask, long, long), Timer.scheduleAtFixedRate(TimerTask, long, long)

Constructor Summary
ScheduledTimerTask()
          Create a new ScheduledTimerTask, to be populated via bean properties.
ScheduledTimerTask(Runnable timerTask)
          Create a new ScheduledTimerTask, with default one-time execution without delay.
ScheduledTimerTask(Runnable timerTask, long delay)
          Create a new ScheduledTimerTask, with default one-time execution with the given delay.
ScheduledTimerTask(Runnable timerTask, long delay, long period, boolean fixedRate)
          Create a new ScheduledTimerTask.
ScheduledTimerTask(TimerTask timerTask)
          Create a new ScheduledTimerTask, with default one-time execution without delay.
ScheduledTimerTask(TimerTask timerTask, long delay)
          Create a new ScheduledTimerTask, with default one-time execution with the given delay.
ScheduledTimerTask(TimerTask timerTask, long delay, long period, boolean fixedRate)
          Create a new ScheduledTimerTask.
 
Method Summary
 long getDelay()
          Return the delay before starting the job for the first time.
 long getPeriod()
          Return the period between repeated task executions.
 TimerTask getTimerTask()
          Return the TimerTask to schedule.
 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(Runnable timerTask)
          Set the Runnable to schedule as TimerTask.
 void setTimerTask(TimerTask timerTask)
          Set the TimerTask to schedule.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScheduledTimerTask

public ScheduledTimerTask()
Create a new ScheduledTimerTask, to be populated via bean properties.

See Also:
setTimerTask(java.util.TimerTask), setDelay(long), setPeriod(long), setFixedRate(boolean)

ScheduledTimerTask

public ScheduledTimerTask(TimerTask timerTask)
Create a new ScheduledTimerTask, with default one-time execution without delay.

Parameters:
timerTask - the TimerTask to schedule

ScheduledTimerTask

public ScheduledTimerTask(TimerTask timerTask,
                          long delay)
Create a new ScheduledTimerTask, with default one-time execution with the given delay.

Parameters:
timerTask - the TimerTask to schedule
delay - the delay before starting the task for the first time (ms)

ScheduledTimerTask

public ScheduledTimerTask(TimerTask timerTask,
                          long delay,
                          long period,
                          boolean fixedRate)
Create a new ScheduledTimerTask.

Parameters:
timerTask - the TimerTask to schedule
delay - 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 execution

ScheduledTimerTask

public ScheduledTimerTask(Runnable timerTask)
Create a new ScheduledTimerTask, with default one-time execution without delay.

Parameters:
timerTask - the Runnable to schedule as TimerTask

ScheduledTimerTask

public ScheduledTimerTask(Runnable timerTask,
                          long delay)
Create a new ScheduledTimerTask, with default one-time execution with the given delay.

Parameters:
timerTask - the Runnable to schedule as TimerTask
delay - the delay before starting the task for the first time (ms)

ScheduledTimerTask

public ScheduledTimerTask(Runnable timerTask,
                          long delay,
                          long period,
                          boolean fixedRate)
Create a new ScheduledTimerTask.

Parameters:
timerTask - the Runnable to schedule as TimerTask
delay - 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 execution
Method Detail

setRunnable

public void setRunnable(Runnable timerTask)
Set the Runnable to schedule as TimerTask.

See Also:
DelegatingTimerTask

setTimerTask

public void setTimerTask(TimerTask timerTask)
Set the TimerTask to schedule.


getTimerTask

public TimerTask getTimerTask()
Return the TimerTask to schedule.


setDelay

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.


getDelay

public long getDelay()
Return the delay before starting the job for the first time.


setPeriod

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 inbetween 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.Timer 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(boolean), isOneTimeTask(), Timer.schedule(TimerTask, long, long)

getPeriod

public long getPeriod()
Return the period between repeated task executions.


isOneTimeTask

public boolean isOneTimeTask()
Is this task only ever going to execute once?

Returns:
true if this task is only ever going to execute once
See Also:
getPeriod()

setFixedRate

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 Timer javadoc for details on those execution modes.

See Also:
Timer.schedule(TimerTask, long, long), Timer.scheduleAtFixedRate(TimerTask, long, long)

isFixedRate

public boolean isFixedRate()
Return whether to schedule as fixed-rate execution.


The Spring Framework

Copyright © 2002-2007 The Spring Framework.