Skip navigation links

Oracle® Coherence Java API Reference
v3.5.1

E15583-01


com.tangosol.util
Class Daemon

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

All Implemented Interfaces:
Guardable, java.lang.Runnable
Direct Known Subclasses:
AsyncBinaryStore.QueueDaemon, LoadBalancer.SocketHandler, MulticastTest.Listener, ReadWriteBackingMap.ReadThread, ReadWriteBackingMap.WriteThread, RefreshableAddressProvider.RefreshThread, TaskDaemon

public abstract class Daemon
extends Base
implements java.lang.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. If thread pooling is desired, consider using a WorkManager implementation.

Author:
cp 2000.08.02, cp 2006.02.23 (Coherence 3.2) added restartability

Nested Class Summary
 class Daemon.DaemonWorker
          The sub-class of Thread that this Daemon uses as the actual thread of execution.

 

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

 

Method Summary
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  void finishStarting(Daemon.DaemonWorker worker)
          Wait for the specified worker to finish starting.
protected  java.lang.String getConfiguredName()
          Determine the configured name for the daemon.
protected  int getConfiguredPriority()
          Determine the configured priority for the daemon.
protected  java.lang.String getDescription()
          Format the Daemon attributes into a String for inclusion in the String returned from the toString() method.
 Guardian.GuardContext getGuardContext()
          Return the GuardContext under which this Daemon is guarded.
protected  java.lang.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.
 java.lang.Thread getThread()
          Accessor to obtain the Daemon thread object.
 java.lang.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()
          Indiciate 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(java.lang.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(java.lang.Runnable action)
          Set the Guardian registration action.
 void setThreadContextClassLoader(java.lang.ClassLoader loader)
          Configure the context ClassLoader for the daemon thread.
 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 java.lang.String toStateString(int nState)
          Convert a state value to a human-readable String.
 java.lang.String toString()
          Return a human-readable String representation of the Daemon.

 

Constructor Detail

Daemon

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

Daemon

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

Daemon

public Daemon(java.lang.String sName,
              int nPriority,
              boolean fStart)
Creates a Daemon with a specified name and priority. <p/> 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

run

public 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();
           }
       }
 
Specified by:
run in interface java.lang.Runnable

setContext

public 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:
setContext in interface Guardable
Parameters:
context - the context guarding this Guardable

recover

public void recover()
Attempt to recover this Guardable. <p/> In a common case where the Guardable has an associated thread, a suitable action would be to interrupt the thread. <p/> If this method does not return before the timeout expires, the Guardable will be considered unrecoverable and will be terminated.
Specified by:
recover in interface Guardable

terminate

public void terminate()
Terminate this Guardable. <p/> This is the final action taken by the Guardian before the Guardable is removed from its responsibility list.
Specified by:
terminate in interface Guardable

isGuarded

public boolean isGuarded()
Return true iff this Daemon's execution is guarded. See Guardian.
Returns:
true iff this Daemon is guarded

getGuardContext

public Guardian.GuardContext getGuardContext()
Return the GuardContext under which this Daemon is guarded.
Returns:
the GuardContext under which this Daemon is guarded, may be null

getMaxWaitMillis

public 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 maximium wait time allowed, or cDefaultMillis if this Daemon is not guarded

heartbeat

protected void heartbeat()
If this Daemon is being guarded, issue a heartbeat to the Guardian.

heartbeat

protected 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

setGuardRegisterAction

protected void setGuardRegisterAction(java.lang.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

getGuardRegisterAction

protected java.lang.Runnable getGuardRegisterAction()
Return the Guardian registration action.
Returns:
the Guardian registration action

setGuardPolicy

protected 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

guardIfNeeded

protected void guardIfNeeded()
If this Daemon has a Guardian and SLA policy specified, ensure that it is registered with its Guardian.

getThread

public java.lang.Thread getThread()
Accessor to obtain the Daemon thread object. <p/> The thread returned by this accessor will be null if the Daemon is stopped.
Returns:
the thread object

getWorker

public Daemon.DaemonWorker getWorker()
Accessor to obtain the Daemon worker object. <p/> The worker returned by this accessor will be null if the Daemon is stopped.
Returns:
the worker object

start

public 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.

isRunning

public boolean isRunning()
Check if the daemon is running (has started and has not stopped).
Returns:
true if and only if the daemon is running

stop

public void stop()
Request the daemon to stop. This method will only have an effect if the daemon sub-class respects the value returned from isStopping().

isStopping

public 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

isOnWorkerThread

public boolean isOnWorkerThread()
Indiciate if the current execution thread is a child of this daemon.
Returns:
true if the current thread is a child of the Daemon

getState

protected int getState()
Obtain the state of the daemon.
Returns:
one of the STATE_ enums

finishStarting

protected 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

changeState

protected 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

setConfiguredPriority

protected void setConfiguredPriority(int nPriority)
Configure the priority for the daemon.
Parameters:
nPriority - the thread priority for the daemon

getConfiguredPriority

protected int getConfiguredPriority()
Determine the configured priority for the daemon.
Returns:
the configured thread priority for the daemon

setConfiguredName

protected void setConfiguredName(java.lang.String sName)
Configure the name for the daemon.
Parameters:
sName - the thread name for the daemon

getConfiguredName

protected java.lang.String getConfiguredName()
Determine the configured name for the daemon.
Returns:
the configured thread name for the daemon

setThreadContextClassLoader

public void setThreadContextClassLoader(java.lang.ClassLoader loader)
Configure the context ClassLoader for the daemon thread. <p/> 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.
Parameters:
loader - the context ClassLoader for the daemon thread

getThreadContextClassLoader

public java.lang.ClassLoader getThreadContextClassLoader()
Determine the configured context ClassLoader for the daemon thread.
Returns:
the configured context ClassLoader for the daemon thread

instantiateWorker

protected Daemon.DaemonWorker instantiateWorker()
Instantiate a DaemonWorker that will be used as a daemon.
Returns:
a new instance of DaemonWorker or a sub-class thereof

configureWorker

protected void configureWorker(Daemon.DaemonWorker worker)
Configure a worker to use as a daemon.
Parameters:
worker - the DaemonWorker to configure

toString

public java.lang.String toString()
Return a human-readable String representation of the 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 toString() method.
Returns:
a String listing the attributes of the Daemon

toStateString

protected static java.lang.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

Skip navigation links

Oracle® Coherence Java API Reference
v3.5.1

E15583-01


Copyright © 2000, 2009, Oracle. All rights reserved.