Class Daemon
- java.lang.Object
- 
- com.tangosol.util.Base
- 
- com.tangosol.util.Daemon
 
 
- 
- Direct Known Subclasses:
- AsyncBinaryStore.QueueDaemon,- LoadBalancer.SocketHandler,- ReadWriteBackingMap.ReadThread,- ReadWriteBackingMap.WriteThread,- RefreshableAddressProvider.RefreshThread,- TaskDaemon
 
 public abstract class Daemon extends Base implements Runnable, Guardable A abstract Daemon thread handler. A sub-class need only provide an implementation of the run() method. When the Daemon is told to start, it will create a Java thread, which means that the number of Daemon instances within a system must be limited to a reasonable number. If an arbitrarily large number of conceptual daemons are necessary, consider using a TaskDaemon, which allows arbitrary tasks to be queued for execution.- Author:
- cp 2000.08.02, cp 2006.02.23 (Coherence 3.2) added restartability, jh 2010.05.07 (Coherence 3.6) added shutdown
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description classDaemon.DaemonWorkerThe sub-class of Thread that this Daemon uses as the actual thread of execution.- 
Nested classes/interfaces inherited from class com.tangosol.util.BaseBase.LoggingWriter
 
- 
 - 
Method SummaryAll Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidchangeState(int nState, Daemon.DaemonWorker worker)Change the state of the daemon.protected voidconfigureWorker(Daemon.DaemonWorker worker)Configure a worker to use as a daemon.protected ThreadGroupensureThreadGroup()Obtain the existing ThreadGroup or create one if none exists or the current one is destroyed.protected voidfinishStarting(Daemon.DaemonWorker worker)Wait for the specified worker to finish starting.protected voidfinishStopping(Daemon.DaemonWorker worker, long cWait)Wait for the specified worker to finish stopping.protected StringgetConfiguredName()Determine the configured name for the daemon.protected intgetConfiguredPriority()Determine the configured priority for the daemon.Guardian.GuardContextgetContext()Return the guard context monitoring this guardable, or null if this Guardable is no longer being monitored.protected StringgetDescription()Format the Daemon attributes into a String for inclusion in the String returned from thetoString()method.protected RunnablegetGuardRegisterAction()Return the Guardian registration action.longgetMaxWaitMillis(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 intgetState()Obtain the state of the daemon.ThreadgetThread()Accessor to obtain the Daemon thread object.ClassLoadergetThreadContextClassLoader()Determine the configured context ClassLoader for the daemon thread.Daemon.DaemonWorkergetWorker()Accessor to obtain the Daemon worker object.protected voidguardIfNeeded()If this Daemon has a Guardian and SLA policy specified, ensure that it is registered with its Guardian.protected voidheartbeat()If this Daemon is being guarded, issue a heartbeat to the Guardian.protected voidheartbeat(long cMillis)If this Daemon is being guarded, issue a heartbeat to the Guardian.protected Daemon.DaemonWorkerinstantiateWorker()Instantiate a DaemonWorker that will be used as a daemon.booleanisGuarded()Return true iff this Daemon's execution is guarded.booleanisOnWorkerThread()Indicate if the current execution thread is a child of this daemon.booleanisRunning()Check if the daemon is running (has started and has not stopped).booleanisStopping()Check if the daemon is supposed to stop.voidrecover()Attempt to recover this Guardable.abstract voidrun()The daemon's implementation method.protected voidsetConfiguredName(String sName)Configure the name for the daemon.protected voidsetConfiguredPriority(int nPriority)Configure the priority for the daemon.voidsetContext(Guardian.GuardContext context)Set the guard context.protected voidsetGuardPolicy(Guardian guardian, long cTimeoutMillis, float flPctRecover)Set the Guardian and policy to guard this Daemon with.protected voidsetGuardRegisterAction(Runnable action)Set the Guardian registration action.voidsetThreadContextClassLoader(ClassLoader loader)Configure the context ClassLoader for the daemon thread.voidshutdown(long cWait)Request the daemon to stop and wait up to the specified number of milliseconds for it to exit.voidstart()Performs a synchronized start of the thread if the thread is not already started.voidstop()Request the daemon to stop.voidterminate()Terminate this Guardable.protected static StringtoStateString(int nState)Convert a state value to a human-readable String.StringtoString()Return a human-readable String representation of the Daemon.- 
Methods inherited from class com.tangosol.util.Baseazzert, 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- 
Daemonpublic Daemon() Default constructor. Creates a Daemon using default settings. The daemon will not be automatically started.
 - 
Daemonpublic Daemon(String sName) Creates a Daemon with the specified name. The daemon will not be automatically started.- Parameters:
- sName- the thread name (may be null)
 
 - 
Daemonpublic Daemon(String sName, int nPriority, boolean fStart) Creates a Daemon with a specified name and priority.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. - 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 auto-start the thread as part of its construction
 
 
- 
 - 
Method Detail- 
runpublic abstract void run() The daemon's implementation method. Override this method to implement a daemon.An example implementation is: while (!isStopping()) { // do some processing // ... synchronized (this) { // wait for notification of more work wait(); } }
 - 
setContextpublic void setContext(Guardian.GuardContext context) Set the guard context. Passing in null indicates that the Guardable is no longer being monitored by a Guardian.- Specified by:
- setContextin interface- Guardable
- Parameters:
- context- the context guarding this Guardable
 
 - 
recoverpublic void recover() Attempt to recover this Guardable.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. 
 - 
terminatepublic void terminate() Terminate this Guardable.This is the final action taken by the Guardian before the Guardable is removed from its responsibility list. 
 - 
getContextpublic Guardian.GuardContext getContext() Return the guard context monitoring this guardable, or null if this Guardable is no longer being monitored.- Specified by:
- getContextin interface- Guardable
- Returns:
- the context guarding this Guardable
 
 - 
isGuardedpublic boolean isGuarded() Return true iff this Daemon's execution is guarded. SeeGuardian.- Returns:
- true iff this Daemon is guarded
 
 - 
getMaxWaitMillispublic 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.- Parameters:
- cDefaultMillis- the default wait time to return if this Daemon is not Guarded
- Returns:
- the maximum wait time allowed, or cDefaultMillis if this Daemon is not guarded
 
 - 
heartbeatprotected void heartbeat() If this Daemon is being guarded, issue a heartbeat to the Guardian.
 - 
heartbeatprotected void heartbeat(long cMillis) If this Daemon is being guarded, issue a heartbeat to the Guardian.- Parameters:
- cMillis- the number of milliseconds for which this Daemon should not be considered timed out
 
 - 
setGuardRegisterActionprotected void setGuardRegisterAction(Runnable action) Set the Guardian registration action. This action is invoked when a new DaemonWorker thread is started to register the daemon with its Guardian.- Parameters:
- action- the Guardian registration action
 
 - 
getGuardRegisterActionprotected Runnable getGuardRegisterAction() Return the Guardian registration action.- Returns:
- the Guardian registration action
 
 - 
setGuardPolicyprotected void setGuardPolicy(Guardian guardian, long cTimeoutMillis, float flPctRecover) Set the Guardian and policy to guard this Daemon with. The Daemon is registered with the specified Guardian each time the Daemon is started, and is released each time the Daemon is stopped.- Parameters:
- guardian- the Guardian that will be guarding this Daemon
- cTimeoutMillis- the timeout in ms for this Daemon, or 0 for the service guardian timeout
- flPctRecover- the recovery percentage for this Daemon
 
 - 
guardIfNeededprotected void guardIfNeeded() If this Daemon has a Guardian and SLA policy specified, ensure that it is registered with its Guardian.
 - 
getThreadpublic Thread getThread() Accessor to obtain the Daemon thread object.The thread returned by this accessor will be null if the Daemon is stopped. - Returns:
- the thread object
 
 - 
getWorkerpublic Daemon.DaemonWorker getWorker() Accessor to obtain the Daemon worker object.The worker returned by this accessor will be null if the Daemon is stopped. - Returns:
- the worker object
 
 - 
startpublic void start() Performs a synchronized start of the thread if the thread is not already started. This means that when control returns to the caller of this method, that the thread has started. This method has no effect if the daemon thread is already running, even if it is in the process of stopping. Note that a daemon can be re-started once it has stopped.
 - 
isRunningpublic boolean isRunning() Check if the daemon is running (has started and has not stopped).- Returns:
- true if and only if the daemon is running
 
 - 
stoppublic void stop() Request the daemon to stop. This method will only have an effect if the daemon sub-class respects the value returned fromisStopping().
 - 
shutdownpublic void shutdown(long cWait) Request the daemon to stop and wait up to the specified number of milliseconds for it to exit. This method will only have an effect if the daemon sub-class respects the value returned fromisStopping().- Parameters:
- 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 stopping
 
 - 
isStoppingpublic boolean isStopping() Check if the daemon is supposed to stop. This method is primarily used by the daemon thread itself to check if it should stop processing.- Returns:
- true if and only if the worker thread is no longer supposed to be running
 
 - 
isOnWorkerThreadpublic boolean isOnWorkerThread() Indicate if the current execution thread is a child of this daemon.- Returns:
- true if the current thread is a child of the Daemon
 
 - 
getStateprotected int getState() Obtain the state of the daemon.- Returns:
- one of the STATE_enums
 
 - 
finishStartingprotected void finishStarting(Daemon.DaemonWorker worker) Wait for the specified worker to finish starting. This method is called while the daemon is in the STARTING state. The caller must be synchronized on this daemon object.- Parameters:
- worker- a worker created by this daemon
 
 - 
finishStoppingprotected void finishStopping(Daemon.DaemonWorker worker, long cWait) Wait for the specified worker to finish stopping. This method is called while the daemon is in the STOPPING state. The caller must be synchronized on this daemon object.- Parameters:
- worker- a worker created by this daemon
- cWait- 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 stopping
 
 - 
changeStateprotected void changeState(int nState, Daemon.DaemonWorker worker)Change the state of the daemon.- Parameters:
- nState- one of the STATE_enums
- worker- the new worker, if starting, otherwise the current worker
 
 - 
setConfiguredPriorityprotected void setConfiguredPriority(int nPriority) Configure the priority for the daemon.- Parameters:
- nPriority- the thread priority for the daemon
 
 - 
getConfiguredPriorityprotected int getConfiguredPriority() Determine the configured priority for the daemon.- Returns:
- the configured thread priority for the daemon
 
 - 
setConfiguredNameprotected void setConfiguredName(String sName) Configure the name for the daemon.- Parameters:
- sName- the thread name for the daemon
 
 - 
getConfiguredNameprotected String getConfiguredName() Determine the configured name for the daemon.- Returns:
- the configured thread name for the daemon
 
 - 
setThreadContextClassLoaderpublic void setThreadContextClassLoader(ClassLoader loader) Configure the context ClassLoader for the daemon thread.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, setContextClassLoaderwill be called on the daemon thread object immediately as well as during subsequent restarts.- Parameters:
- loader- the context ClassLoader for the daemon thread
 
 - 
getThreadContextClassLoaderpublic ClassLoader getThreadContextClassLoader() Determine the configured context ClassLoader for the daemon thread.- Returns:
- the configured context ClassLoader for the daemon thread
 
 - 
instantiateWorkerprotected Daemon.DaemonWorker instantiateWorker() Instantiate a DaemonWorker that will be used as a daemon.- Returns:
- a new instance of DaemonWorker or a sub-class thereof
 
 - 
configureWorkerprotected void configureWorker(Daemon.DaemonWorker worker) Configure a worker to use as a daemon.- Parameters:
- worker- the DaemonWorker to configure
 
 - 
ensureThreadGroupprotected ThreadGroup ensureThreadGroup() Obtain the existing ThreadGroup or create one if none exists or the current one is destroyed.- Returns:
- the ThreadGroup that the worker will be part of
 
 - 
toStringpublic String toString() Return a human-readable String representation of the Daemon.
 - 
getDescriptionprotected String getDescription() Format the Daemon attributes into a String for inclusion in the String returned from thetoString()method.- Returns:
- a String listing the attributes of the Daemon
 
 - 
toStateStringprotected static String toStateString(int nState) Convert a state value to a human-readable String.- Parameters:
- nState- a Daemon state, one of the STATE_* enums
- Returns:
- a human-readable name for the state
 
 
- 
 
-