Skip navigation links

Oracle® Coherence Java API Reference
Release 3.7.1.0

E22843-01


com.tangosol.util
Class TaskDaemon

java.lang.Object
  extended by com.tangosol.util.Base
      extended by com.tangosol.util.Daemon
          extended by com.tangosol.util.TaskDaemon

All Implemented Interfaces:
Guardable, java.lang.Runnable
Direct Known Subclasses:
SessionHelper.SessionReaperDaemon

public class TaskDaemon
extends Daemon

A Daemon thread handler that asynchronously executes Runnable tasks, either at a scheduled time or "as soon as possible".

Author:
cp 2003.10.09, cp 2006.02.23 (Coherence 3.2) bulletproofing for use in the CQC

Nested Class Summary
 class TaskDaemon.PeriodicTask
          A PeriodicTask is a task that automatically reschedules itself so that it executes on a periodic basis.

 

Nested classes/interfaces inherited from class com.tangosol.util.Daemon
Daemon.DaemonWorker

 

Constructor Summary
TaskDaemon()
          Default constructor.
TaskDaemon(java.lang.String sName)
          Creates a TaskDaemon with the specified name.
TaskDaemon(java.lang.String sName, int nPriority, boolean fStart)
          Creates a TaskDaemon with a specified name and priority.
TaskDaemon(java.lang.String sName, int nPriority, boolean fStart, boolean fFinish, int cMillisTimeout)
          Creates a TaskDaemon with a specified name and priority.

 

Method Summary
 void executePeriodicTask(java.lang.Runnable task, long cMillisInterval)
          Schedule a periodic task to be run "as soon as possible", and to repeat at the specified interval.
 void executeTask(java.lang.Runnable task)
          Schedule a task to be run by the daemon "as soon as possible".
protected  java.lang.String getDescription()
          Format the Daemon attributes into a String for inclusion in the String returned from the Daemon.toString() method.
 long getIdleTimeout()
          Determine the length of time that the daemon will live without any activity before it stops itself.
protected  long getMostRecentTaskTime()
          Determine when the most recent task was run.
protected  LongArray getTasks()
          Obtain the pending tasks.
protected  java.lang.Runnable instantiatePeriodicTask(java.lang.Runnable task, long cMillisInterval)
          Create a task that will automatically be run on a periodic basis.
 boolean isFinishing()
          Determine if the daemon will finish those scheduled tasks that are ripe (presently due to be run) before stopping.
protected  void onException(java.lang.Throwable e, java.lang.Runnable task)
          Process an exception that is thrown during a task execution.
 void run()
          The task processing loop.
protected  void run(java.lang.Runnable task)
          Execute a Runnable task.
 void schedulePeriodicTask(java.lang.Runnable task, long ldtFirst, long cMillisInterval)
          Schedule a periodic task to be run at the specified time, and to repeat at the specified interval.
 void scheduleTask(java.lang.Runnable task, long ldt)
          Schedule a task to be run at the specified time, or as soon after as possible.
 void setFinishing(boolean fFinish)
          Specify whether the daemon will finish scheduled tasks before stopping.
 void setIdleTimeout(long cMillis)
          Configure the daemon's timeout.
 void stop(boolean fFinish)
          Request the daemon to stop, optionally completing tasks that have already been scheduled and are ready to be run.
protected  java.lang.Runnable takeNextRipeTask()
          Wait for the next scheduled task is ripe (due or overdue), then remove it from the pending schedule and return it.
 java.lang.String toString()
          Return a human-readable String representation of the Daemon.
protected  void updateMostRecentTaskTime()
          Set the time that the most recent task was run to the current time.

 

Methods inherited from class com.tangosol.util.Daemon
changeState, configureWorker, finishStarting, finishStopping, getConfiguredName, getConfiguredPriority, getContext, getGuardRegisterAction, getMaxWaitMillis, getState, getThread, getThreadContextClassLoader, getWorker, guardIfNeeded, heartbeat, heartbeat, instantiateWorker, isGuarded, isOnWorkerThread, isRunning, isStopping, recover, setConfiguredName, setConfiguredPriority, setContext, setGuardPolicy, setGuardRegisterAction, setThreadContextClassLoader, shutdown, start, stop, terminate, toStateString

 

Constructor Detail

TaskDaemon

public TaskDaemon()
Default constructor. Creates a TaskDaemon using default settings. The daemon will not be automatically started.

TaskDaemon

public TaskDaemon(java.lang.String sName)
Creates a TaskDaemon with the specified name. The daemon will not be automatically started.
Parameters:
sName - the thread name (may be null)

TaskDaemon

public TaskDaemon(java.lang.String sName,
                  int nPriority,
                  boolean fStart)
Creates a TaskDaemon with a specified name and priority.
Parameters:
sName - the thread name (may be null)
nPriority - the thread priority, between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY inclusive
fStart - pass true to start the thread immediately

TaskDaemon

public TaskDaemon(java.lang.String sName,
                  int nPriority,
                  boolean fStart,
                  boolean fFinish,
                  int cMillisTimeout)
Creates a TaskDaemon with a specified name and priority.
Parameters:
sName - the thread name (may be null)
nPriority - the thread priority, between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY inclusive
fStart - pass true to start the thread immediately
fFinish - pass true to makes sure ripe tasks are run before the daemon shuts down
cMillisTimeout - the number of milliseconds to wait after the previous task finsished for a new task to be submitted before automatically shutting down the daemon thread

Method Detail

run

public void run()
The task processing loop.
Specified by:
run in interface java.lang.Runnable
Specified by:
run in class Daemon
See Also:
Thread.run()

stop

public void stop(boolean fFinish)
Request the daemon to stop, optionally completing tasks that have already been scheduled and are ready to be run.
Parameters:
fFinish - pass true if the daemon should finish any tasks that have already been scheduled before stopping

isFinishing

public boolean isFinishing()
Determine if the daemon will finish those scheduled tasks that are ripe (presently due to be run) before stopping.
Returns:
true if the daemon is configured to finish any ripe scheduled tasks before stopping

setFinishing

public void setFinishing(boolean fFinish)
Specify whether the daemon will finish scheduled tasks before stopping.
Parameters:
fFinish - pass true to force the daemon to finish any scheduled tasks before stopping

getIdleTimeout

public long getIdleTimeout()
Determine the length of time that the daemon will live without any activity before it stops itself.
Returns:
the timeout for the TaskDaemon's thread to live before being shut down

setIdleTimeout

public void setIdleTimeout(long cMillis)
Configure the daemon's timeout. Note that if the daemon shuts itself down, it will automatically restart when something is added to the queue.
Parameters:
cMillis - if greater than zero, the number of milliseconds that the daemon will wait with nothing in the queue before shutting itself down

executeTask

public void executeTask(java.lang.Runnable task)
Schedule a task to be run by the daemon "as soon as possible".
Parameters:
task - a Runnable object to invoke

scheduleTask

public void scheduleTask(java.lang.Runnable task,
                         long ldt)
Schedule a task to be run at the specified time, or as soon after as possible.
Parameters:
task - a Runnable object to invoke
ldt - a datetime value at which to run the task

executePeriodicTask

public void executePeriodicTask(java.lang.Runnable task,
                                long cMillisInterval)
Schedule a periodic task to be run "as soon as possible", and to repeat at the specified interval.
Parameters:
task - a Runnable object to invoke
cMillisInterval - the number of milliseconds to wait after the task is run before running it again

schedulePeriodicTask

public void schedulePeriodicTask(java.lang.Runnable task,
                                 long ldtFirst,
                                 long cMillisInterval)
Schedule a periodic task to be run at the specified time, and to repeat at the specified interval.
Parameters:
task - a Runnable object to invoke
ldtFirst - a datetime value at which to first run the task
cMillisInterval - the number of milliseconds to wait after the task is run before running it again

getTasks

protected LongArray getTasks()
Obtain the pending tasks.
Returns:
a LongArray keyed by SafeTimeMillis with a corresponding value being a List of tasks scheduled at that time

takeNextRipeTask

protected java.lang.Runnable takeNextRipeTask()
                                       throws java.lang.InterruptedException
Wait for the next scheduled task is ripe (due or overdue), then remove it from the pending schedule and return it.
Returns:
a task that is ripe to be run, or null if the TaskDaemon is shutting down and no task should be run
Throws:
java.lang.InterruptedException

run

protected void run(java.lang.Runnable task)
Execute a Runnable task.
Parameters:
task - a Runnable object

getMostRecentTaskTime

protected long getMostRecentTaskTime()
Determine when the most recent task was run.
Returns:
the date/time at which the most recent task was run

updateMostRecentTaskTime

protected void updateMostRecentTaskTime()
Set the time that the most recent task was run to the current time.

instantiatePeriodicTask

protected java.lang.Runnable instantiatePeriodicTask(java.lang.Runnable task,
                                                     long cMillisInterval)
Create a task that will automatically be run on a periodic basis.
Parameters:
task - the actual task to run
cMillisInterval - the period of time, in milliseconds, to wait between runs of the task
Returns:
a task that will run itself periodically

toString

public java.lang.String toString()
Return a human-readable String representation of the Daemon.
Overrides:
toString in class Daemon
Returns:
a String describing the Daemon

getDescription

protected java.lang.String getDescription()
Format the Daemon attributes into a String for inclusion in the String returned from the Daemon.toString() method.
Overrides:
getDescription in class Daemon
Returns:
a String listing the attributes of the Daemon

onException

protected void onException(java.lang.Throwable e,
                           java.lang.Runnable task)
Process an exception that is thrown during a task execution. The default implementation logs the exception and continues.
Parameters:
e - Throwable object (a RuntimeException or an Error)
task - the task that caused the exception

Skip navigation links

Oracle® Coherence Java API Reference
Release 3.7.1.0

E22843-01


Copyright © 2000, 2011, Oracle and/or its affiliates. All rights reserved.