org.apache.muse.util
Class Timer

java.lang.Object
  extended by org.apache.muse.util.Timer

public class Timer
extends Object

Timer is a wrapper class for the JDK java.util.Timer that makes it easy to reschedule timers using the same java.util.TimerTask. It also provides some extra convenience methods related to scheduling and execution. All real work is delegated to java.util.Timer.

Author:
Dan Jemiolo (danj)

Constructor Summary
Timer()
           
 
Method Summary
 void cancel()
          Cancels the timer.
 void executeNow()
          Executes the timer's task whether the timer is scheduled or not.
 Date getScheduledTime()
           
 TimerTask getTimerTask()
           
 boolean isPastScheduledTime()
           
 void reschedule(TimerTask task, Date whenToFire)
          Cancels the timer (if it is currently scheduled) and reschedules it for the given time.
 void schedule(TimerTask task, Date whenToFire)
          Sets the timer to fire at the given time, which will results in the timer's TimerTask being executed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Timer

public Timer()
Method Detail

cancel

public final void cancel()
Cancels the timer. If the timer was not scheduled, this is a no-op. If it was scheduled, its task will not be executed and subsequent calls to getScheduledTime() will return null.

To reschedule a timer after it has been cancelled, you must use the reschedule(TimerTask, Date) method - you cannot just call schedule(TimerTask, Date).


executeNow

public final void executeNow()
Executes the timer's task whether the timer is scheduled or not. The implementation first tries to cancel the timer, then executes the task directly (via TimerTask.run()).


getScheduledTime

public final Date getScheduledTime()
Returns:
The time when this timer is set to fire, or null if it is not scheduled.

getTimerTask

public final TimerTask getTimerTask()
Returns:
The task that will be executed if and when the timer is fired.

isPastScheduledTime

public final boolean isPastScheduledTime()
Returns:
True, if the timer is scheduled and the current time is later than the schedule time.

reschedule

public final void reschedule(TimerTask task,
                             Date whenToFire)
Cancels the timer (if it is currently scheduled) and reschedules it for the given time.

Parameters:
task - The code to run when the timer is up.
whenToFire - The new fire time for the timer.

schedule

public final void schedule(TimerTask task,
                           Date whenToFire)
Sets the timer to fire at the given time, which will results in the timer's TimerTask being executed. If the given time is in the past, the timer will execute immediately (or as soon as its thread is given control).

Parameters:
task - The code to run when the timer is up.
whenToFire - The time when the timer should fire.


Copyright © 2005-2011 Apache Web Services - Muse. All Rights Reserved.