public abstract class Daemon extends Base implements Runnable, Guardable
Modifier and Type | Class and Description |
---|---|
class |
Daemon.DaemonWorker
The sub-class of Thread that this Daemon uses as the actual thread
of execution.
|
Base.LoggingWriter, Base.StackFrame
Constructor and Description |
---|
Daemon()
Default constructor.
|
Daemon(String sName)
Creates a Daemon with the specified name.
|
Daemon(String sName,
int nPriority,
boolean fStart)
Creates a Daemon with a specified name and priority.
|
Modifier and Type | Method and Description |
---|---|
protected void |
changeState(int nState,
Daemon.DaemonWorker worker)
Change the state of the daemon.
|
protected void |
configureWorker(Daemon.DaemonWorker worker)
Configure a worker to use as a daemon.
|
protected ThreadGroup |
ensureThreadGroup()
Obtain the existing ThreadGroup or create one if none exists or the
current one is destroyed.
|
protected void |
finishStarting(Daemon.DaemonWorker worker)
Wait for the specified worker to finish starting.
|
protected void |
finishStopping(Daemon.DaemonWorker worker,
long cWait)
Wait for the specified worker to finish stopping.
|
protected String |
getConfiguredName()
Determine the configured name for the daemon.
|
protected int |
getConfiguredPriority()
Determine the configured priority for the daemon.
|
Guardian.GuardContext |
getContext()
Return the guard context monitoring this guardable, or null
if this Guardable is no longer being monitored.
|
protected String |
getDescription()
Format the Daemon attributes into a String for inclusion in the String
returned from the
toString() method. |
protected Runnable |
getGuardRegisterAction()
Return the Guardian registration action.
|
long |
getMaxWaitMillis(long cDefaultMillis)
If this Daemon is guarded, return the maximum wait time that the Daemon
is allowed to wait for, according to its SLA.
|
protected int |
getState()
Obtain the state of the daemon.
|
Thread |
getThread()
Accessor to obtain the Daemon thread object.
|
ClassLoader |
getThreadContextClassLoader()
Determine the configured context ClassLoader for the daemon thread.
|
Daemon.DaemonWorker |
getWorker()
Accessor to obtain the Daemon worker object.
|
protected void |
guardIfNeeded()
If this Daemon has a Guardian and SLA policy specified, ensure that it
is registered with its Guardian.
|
protected void |
heartbeat()
If this Daemon is being guarded, issue a heartbeat to the Guardian.
|
protected void |
heartbeat(long cMillis)
If this Daemon is being guarded, issue a heartbeat to the Guardian.
|
protected Daemon.DaemonWorker |
instantiateWorker()
Instantiate a DaemonWorker that will be used as a daemon.
|
boolean |
isGuarded()
Return true iff this Daemon's execution is guarded.
|
boolean |
isOnWorkerThread()
Indicate if the current execution thread is a child of this daemon.
|
boolean |
isRunning()
Check if the daemon is running (has started and has not stopped).
|
boolean |
isStopping()
Check if the daemon is supposed to stop.
|
void |
recover()
Attempt to recover this Guardable.
|
abstract void |
run()
The daemon's implementation method.
|
protected void |
setConfiguredName(String sName)
Configure the name for the daemon.
|
protected void |
setConfiguredPriority(int nPriority)
Configure the priority for the daemon.
|
void |
setContext(Guardian.GuardContext context)
Set the guard context.
|
protected void |
setGuardPolicy(Guardian guardian,
long cTimeoutMillis,
float flPctRecover)
Set the Guardian and policy to guard this Daemon with.
|
protected void |
setGuardRegisterAction(Runnable action)
Set the Guardian registration action.
|
void |
setThreadContextClassLoader(ClassLoader loader)
Configure the context ClassLoader for the daemon thread.
|
void |
shutdown(long cWait)
Request the daemon to stop and wait up to the specified number of
milliseconds for it to exit.
|
void |
start()
Performs a synchronized start of the thread if the thread is not
already started.
|
void |
stop()
Request the daemon to stop.
|
void |
terminate()
Terminate this Guardable.
|
protected static String |
toStateString(int nState)
Convert a state value to a human-readable String.
|
String |
toString()
Return a human-readable String representation of the Daemon.
|
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, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, 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
public Daemon()
public Daemon(String sName)
sName
- the thread name (may be null)public Daemon(String sName, int nPriority, boolean fStart)
Warning: If the implementation class is a inner non-static class that refers to its outer object ("MyOuterClass.this"), do not use the auto-start option. The reason is that the run() method of the Daemon may be invoked on the new thread before the construction of the Daemon object itself unwinds, which means that the outer object reference will not have been set yet, causing an inexplicable NullPointerException.
sName
- the thread name (may be null)nPriority
- the thread priority, between Thread.MIN_PRIORITY and
Thread.MAX_PRIORITY inclusivefStart
- pass true to auto-start the thread as part of its
constructionpublic abstract void run()
An example implementation is:
while (!isStopping())
{
// do some processing
// ...
synchronized (this)
{
// wait for notification of more work
wait();
}
}
public void setContext(Guardian.GuardContext context)
setContext
in interface Guardable
context
- the context guarding this Guardablepublic void recover()
In a common case where the Guardable has an associated thread, a suitable action would be to interrupt the thread.
If this method does not return before the timeout expires, the Guardable will be considered unrecoverable and will be terminated.
public void terminate()
This is the final action taken by the Guardian before the Guardable is removed from its responsibility list.
public Guardian.GuardContext getContext()
getContext
in interface Guardable
public boolean isGuarded()
Guardian
.public long getMaxWaitMillis(long cDefaultMillis)
cDefaultMillis
- the default wait time to return if this Daemon
is not Guardedprotected void heartbeat()
protected void heartbeat(long cMillis)
cMillis
- the number of milliseconds for which this Daemon
should not be considered timed outprotected void setGuardRegisterAction(Runnable action)
action
- the Guardian registration actionprotected Runnable getGuardRegisterAction()
protected void setGuardPolicy(Guardian guardian, long cTimeoutMillis, float flPctRecover)
guardian
- the Guardian that will be guarding this DaemoncTimeoutMillis
- the timeout in ms for this Daemon, or 0 for the
service guardian timeoutflPctRecover
- the recovery percentage for this Daemonprotected void guardIfNeeded()
public Thread getThread()
The thread returned by this accessor will be null if the Daemon is stopped.
public Daemon.DaemonWorker getWorker()
The worker returned by this accessor will be null if the Daemon is stopped.
public void start()
public boolean isRunning()
public void stop()
isStopping()
.public void shutdown(long cWait)
isStopping()
.cWait
- the maximum number of milliseconds to wait for the
daemon to exit; pass zero to return immediately;
pass -1 to block the calling thread until the worker
finishes stoppingpublic boolean isStopping()
public boolean isOnWorkerThread()
protected int getState()
protected void finishStarting(Daemon.DaemonWorker worker)
worker
- a worker created by this daemonprotected void finishStopping(Daemon.DaemonWorker worker, long cWait)
worker
- a worker created by this daemoncWait
- the maximum number of milliseconds to wait for the
specified worker to finish stopping; pass zero to return
immediately; pass -1 to block the calling thread until
the worker finishes stoppingprotected void changeState(int nState, Daemon.DaemonWorker worker)
nState
- one of the STATE_enumsworker
- the new worker, if starting, otherwise the current
workerprotected void setConfiguredPriority(int nPriority)
nPriority
- the thread priority for the daemonprotected int getConfiguredPriority()
protected void setConfiguredName(String sName)
sName
- the thread name for the daemonprotected String getConfiguredName()
public void setThreadContextClassLoader(ClassLoader loader)
If the daemon thread is not currently running, the specified ClassLoader
will be associated with the daemon thread when it is started (or
restarted). Otherwise, setContextClassLoader
will be called on the daemon thread object
immediately as well as during subsequent restarts.
loader
- the context ClassLoader for the daemon threadpublic ClassLoader getThreadContextClassLoader()
protected Daemon.DaemonWorker instantiateWorker()
protected void configureWorker(Daemon.DaemonWorker worker)
worker
- the DaemonWorker to configureprotected ThreadGroup ensureThreadGroup()
public String toString()
protected String getDescription()
toString()
method.protected static String toStateString(int nState)
nState
- a Daemon state, one of the STATE_* enums