wt.jmx.core
Class PeriodicTaskSupport

java.lang.Object
  extended by wt.jmx.core.PeriodicTaskSupport

public abstract class PeriodicTaskSupport
extends Object

This abstract class takes care of most of the drudgery of properly setting up a task for repeated execution on a specific, periodic interval. This includes support for start() and stop() lifecycle operations as well as changes to the interval frequency.

Note that though this class defaults to daemon threads unless otherwise specified by the caller. Also note that the default implementation of createExecutor() returns a SharedScheduledExecutor as the backing executor, which implies some limitations as compared to normal ScheduledExecutorService implementations.

Supported API: true

Extendable: true


Constructor Summary
PeriodicTaskSupport()
          Default constructor

Supported API: true
PeriodicTaskSupport(boolean daemon)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(boolean daemon, int taskIntervalSeconds)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(int taskIntervalSeconds)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(String timerThreadNamePrefix)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(String timerThreadNamePrefix, boolean daemon)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(String timerThreadNamePrefix, boolean daemon, int taskIntervalSeconds)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(String timerThreadNamePrefix, boolean daemon, int taskIntervalSeconds, int firstExecutionDelaySeconds)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(String timerThreadNamePrefix, boolean daemon, int taskIntervalSeconds, int firstExecutionDelaySeconds, boolean fixedDelay)
          Constructor; arguments set corresponding properties

Supported API: true
PeriodicTaskSupport(String timerThreadNamePrefix, int taskIntervalSeconds)
          Constructor; arguments set corresponding properties

Supported API: true
 
Method Summary
protected  ScheduledExecutorService createExecutor(String timerThreadNamePrefix, boolean daemon)
          Provides ScheduledExecutorService instance used for execution.
protected abstract  Runnable createTask()
          This method must be overriden to return the Runnable task for periodic execution by this class.
 int getFirstExecutionDelaySeconds()
          Delay of first task execution in seconds.
 int getTaskIntervalSeconds()
          Returns time interval at which task is executed (in seconds).
 String getTimerThreadNamePrefix()
          The prefix name given to all threads associated with the underlying time.
 boolean isDaemon()
          Whether the daemon or non-daemon threads should be used.
 boolean isFixedDelay()
          Whether this instance will run its task at a fixed delay or a fixed rate; by default this class uses a fixed rate.
 boolean isStarted()
          Returns whether this instance is currently running, i.e start() has been called more recently than stop().
 boolean isTaskStarted()
          Whether task itself is currently started.
 void setFirstExecutionDelaySeconds(int firstExecutionDelaySeconds)
          Delay of first task execution in seconds.
 void setTaskIntervalSeconds(int taskIntervalSeconds)
          Sets time interval at which task is executed (in seconds); task execution is disabled if this is non-positive.
protected  void shutdownExecutor(ScheduledExecutorService executor)
          Shutdown the ScheduledExecutorService instance (that produced by createExecutor()) via executor.shutdown().
 void start()
          Start periodic background execution of task.
 void startTask()
          Called by start() and by setTaskIntervalSeconds() when necessary.
 void stop()
          Stop periodic background execution of task.
 void stopTask()
          Called by stop() and by setTaskIntervalSeconds() when necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PeriodicTaskSupport

public PeriodicTaskSupport()
Default constructor

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(int taskIntervalSeconds)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(String timerThreadNamePrefix)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(String timerThreadNamePrefix,
                           int taskIntervalSeconds)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(boolean daemon)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(boolean daemon,
                           int taskIntervalSeconds)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(String timerThreadNamePrefix,
                           boolean daemon)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(String timerThreadNamePrefix,
                           boolean daemon,
                           int taskIntervalSeconds)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(String timerThreadNamePrefix,
                           boolean daemon,
                           int taskIntervalSeconds,
                           int firstExecutionDelaySeconds)
Constructor; arguments set corresponding properties

Supported API: true


PeriodicTaskSupport

public PeriodicTaskSupport(String timerThreadNamePrefix,
                           boolean daemon,
                           int taskIntervalSeconds,
                           int firstExecutionDelaySeconds,
                           boolean fixedDelay)
Constructor; arguments set corresponding properties

Supported API: true

Method Detail

createTask

protected abstract Runnable createTask()
This method must be overriden to return the Runnable task for periodic execution by this class.

Supported API: true


isStarted

public boolean isStarted()
Returns whether this instance is currently running, i.e start() has been called more recently than stop().

Supported API: true


start

public void start()
Start periodic background execution of task. When used from within an SelfAwareMBean this should generally be called within the MBean's start() method.

Supported API: true


stop

public void stop()
Stop periodic background execution of task. When used from within an SelfAwareMBean this should generally be called within the MBean's stop() method.

Supported API: true


getTaskIntervalSeconds

public int getTaskIntervalSeconds()
Returns time interval at which task is executed (in seconds).

Supported API: true


setTaskIntervalSeconds

public void setTaskIntervalSeconds(int taskIntervalSeconds)
Sets time interval at which task is executed (in seconds); task execution is disabled if this is non-positive.

Supported API: true


getFirstExecutionDelaySeconds

public int getFirstExecutionDelaySeconds()
Delay of first task execution in seconds. A negative value implies that the taskIntervalSeconds will be used instead.

Supported API: true


setFirstExecutionDelaySeconds

public void setFirstExecutionDelaySeconds(int firstExecutionDelaySeconds)
Delay of first task execution in seconds. A negative value implies that the taskIntervalSeconds will be used instead.

Note that calling this method has no effect on this instance while it is in a started/running state, but will have an effect the next time the task is restarted (including upon a call to setTaskIntervalSeconds()).

Supported API: true


getTimerThreadNamePrefix

public String getTimerThreadNamePrefix()
The prefix name given to all threads associated with the underlying time. This prefix is used with an underlying SharedScheduledExecutor to share thread pools for all instances with the same prefix name and daemon flag. A value of null here implies that a default prefix will be used in SharedScheduledExecutor.

Supported API: true


isDaemon

public boolean isDaemon()
Whether the daemon or non-daemon threads should be used.

Supported API: true


isFixedDelay

public boolean isFixedDelay()
Whether this instance will run its task at a fixed delay or a fixed rate; by default this class uses a fixed rate.

Supported API: true


isTaskStarted

public boolean isTaskStarted()
Whether task itself is currently started.

Supported API: true


startTask

public void startTask()
Called by start() and by setTaskIntervalSeconds() when necessary. Should only need to be called directly when something else causes a need to temporarily pause and restart the task. This is more efficient than calling stop and then start.

Warning: This routine is a no-op when this object is not started (see start() and isStarted()).

Supported API: true


stopTask

public void stopTask()
Called by stop() and by setTaskIntervalSeconds() when necessary. Should only need to be called directly when something else causes a need to temporarily pause and restart the task. This is more efficient than calling stop and then start.

Supported API: true


createExecutor

protected ScheduledExecutorService createExecutor(String timerThreadNamePrefix,
                                                  boolean daemon)
Provides ScheduledExecutorService instance used for execution. This implementation returns a SharedScheduledExecutor and should almost always suffice as is.

Supported API: true


shutdownExecutor

protected void shutdownExecutor(ScheduledExecutorService executor)
Shutdown the ScheduledExecutorService instance (that produced by createExecutor()) via executor.shutdown(). This implementation returns a SharedScheduledExecutor and should almost always suffice as is.

Supported API: true