Package com.tangosol.util
Class TaskDaemon
- java.lang.Object
-
- com.tangosol.util.Base
-
- com.tangosol.util.Daemon
-
- com.tangosol.util.TaskDaemon
-
- 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
Nested Classes Modifier and Type Class Description 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
-
Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
-
Constructor Summary
Constructors Constructor Description TaskDaemon()
Default constructor.TaskDaemon(String sName)
Creates a TaskDaemon with the specified name.TaskDaemon(String sName, int nPriority, boolean fStart)
Creates a TaskDaemon with a specified name and priority.TaskDaemon(String sName, int nPriority, boolean fStart, boolean fFinish, int cMillisTimeout)
Creates a TaskDaemon with a specified name and priority.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
executePeriodicTask(Runnable task, long cMillisInterval)
Schedule a periodic task to be run "as soon as possible", and to repeat at the specified interval.void
executeTask(Runnable task)
Schedule a task to be run by the daemon "as soon as possible".protected String
getDescription()
Format the Daemon attributes into a String for inclusion in the String returned from theDaemon.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 Runnable
instantiatePeriodicTask(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(Throwable e, Runnable task)
Process an exception that is thrown during a task execution.void
run()
The task processing loop.protected void
run(Runnable task)
Execute a Runnable task.void
schedulePeriodicTask(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(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 Runnable
takeNextRipeTask()
Wait for the next scheduled task is ripe (due or overdue), then remove it from the pending schedule and return it.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, ensureThreadGroup, 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
-
Methods inherited from class com.tangosol.util.Base
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mergeArray, mergeBooleanArray, mergeByteArray, mergeCharArray, mergeDoubleArray, mergeFloatArray, mergeIntArray, mergeLongArray, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
-
-
-
-
Constructor Detail
-
TaskDaemon
public TaskDaemon()
Default constructor. Creates a TaskDaemon using default settings. The daemon will not be automatically started.
-
TaskDaemon
public TaskDaemon(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(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 inclusivefStart
- pass true to start the thread immediately
-
TaskDaemon
public TaskDaemon(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 inclusivefStart
- pass true to start the thread immediatelyfFinish
- pass true to makes sure ripe tasks are run before the daemon shuts downcMillisTimeout
- the number of milliseconds to wait after the previous task finished for a new task to be submitted before automatically shutting down the daemon thread
-
-
Method Detail
-
run
public void run()
The task processing loop.
-
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(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(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 invokeldt
- a datetime value at which to run the task
-
executePeriodicTask
public void executePeriodicTask(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 invokecMillisInterval
- the number of milliseconds to wait after the task is run before running it again
-
schedulePeriodicTask
public void schedulePeriodicTask(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 invokeldtFirst
- a datetime value at which to first run the taskcMillisInterval
- 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 Runnable takeNextRipeTask() throws 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:
InterruptedException
- if this thread is interrupted while waiting for the next task
-
run
protected void run(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 Runnable instantiatePeriodicTask(Runnable task, long cMillisInterval)
Create a task that will automatically be run on a periodic basis.- Parameters:
task
- the actual task to runcMillisInterval
- the period of time, in milliseconds, to wait between runs of the task- Returns:
- a task that will run itself periodically
-
toString
public String toString()
Return a human-readable String representation of the Daemon.
-
getDescription
protected String getDescription()
Format the Daemon attributes into a String for inclusion in the String returned from theDaemon.toString()
method.- Overrides:
getDescription
in classDaemon
- Returns:
- a String listing the attributes of the Daemon
-
-