Package fr.dyade.aaa.common
Class Daemon
- java.lang.Object
-
- fr.dyade.aaa.common.Daemon
-
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
AdminProxy.AdminMonitor,AmqpAcquisition.ConnectionUpdater,AMQPConnectionListener,AMQPConnectionListener.NetServerOut,DistributionDaemon,HttpNetwork.NetServerIn,HttpNetwork.NetServerOut,IOControl.Reader,JMSAcquisition.ConnectionUpdater,JMSModule.ReconnectionDaemon,JMSModule.StartupDaemon,LiveServerConnection.ReconnectionDaemon,MultiSessionConsumer.MessageDispatcher,MultiSessionConsumer.MessageDispatcher,NGNetwork.Dispatcher,NGNetwork.NetServer,PoolNetwork.Dispatcher,PoolNetwork.Sender,PoolNetwork.WakeOnConnection,PoolNetwork.WatchDog,Proxy.NetServerIn,ReliableTcpConnection.Reader,ReliableTcpConnection.Reader,RequestMultiplexer.DemultiplexerDaemon,RequestMultiplexer.DemultiplexerDaemon,RestAcquisitionAsync.XDaemon,Session.SessionDaemon,Session.SessionDaemon,SimpleNetwork.NetServerIn,SimpleNetwork.NetServerOut,TcpConnectionListener,TcpReader,TcpServer.Monitor,TcpWriter,UDPNetwork.MessageBuilder,UDPNetwork.NetServerIn,UDPNetwork.NetServerOut,UDPNetwork.WatchDog
public abstract class Daemon extends Object implements Runnable
The Daemon class represents a basic active component in a server. It provides useful code to start and safely stop inner Thread.Main loop of daemon:
try { while (running) { canStop = true; // Get a notification, then execute the right reaction. try { // Get a request ... } catch (InterruptedException exc) { continue; } canStop = false; // executes the request ... } } finally { finish(); }
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancanStopBoolean variable used to stop the daemon properly.private booleandaemonThedaemon's nature.protected ThreadGroupgroupThe group of this thread.protected Loggerlogmonprivate StringnameThedaemon's name.protected intpriorityTheprioritythat is assigned to the daemon.protected booleanrunningBoolean variable used to stop the daemon properly.protected ThreadthreadThe active component of this daemon.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidclose()Releases any resources attached to this daemon.protected voidfinish()StringgetName()Returns thisdaemon's name.voidinterrupt()Interrupts this daemon.booleanisCurrentThread()Tests if the daemon's thread is the current one.booleanisRunning()Tests if this daemon is alive.voidsetDaemon(boolean daemon)Marks the daemon's thread as either a daemon thread a user thread.voidsetName(String name)Set the name of the daemon's thread.voidsetPriority(int newPriority)Changes the priority of this daemon.voidsetThreadGroup(ThreadGroup group)Set the thread group to which this daemon's thread belongs.protected abstract voidshutdown()Interrupts a thread that waits for long periods.voidstart()Causes this daemon to begin execution.voidstop()Forces the daemon to stop executing.StringtoString()Returns a string representation of this daemon.
-
-
-
Field Detail
-
logmon
protected Logger logmon
-
running
protected volatile boolean running
Boolean variable used to stop the daemon properly. The daemon tests this variable between each request, and stops if it is false.
-
canStop
protected volatile boolean canStop
Boolean variable used to stop the daemon properly. If this variable is true then the daemon is waiting for a long time and it can be interrupted, else it handles a request and it will exit after (it tests thevariable between each reaction)running
-
thread
protected Thread thread
The active component of this daemon.
-
name
private String name
Thedaemon's name.
-
daemon
private boolean daemon
Thedaemon's nature.
-
priority
protected int priority
Theprioritythat is assigned to the daemon.
-
group
protected ThreadGroup group
The group of this thread. Note: The ThreadGroup is just used as information.
-
-
Constructor Detail
-
Daemon
protected Daemon(String name)
Deprecated.Allocates a new Daemon object. Be careful, the use of this constructor is not recommended as it does not dissociate the corresponding logger.- Parameters:
name- the name of the new Daemon
-
-
Method Detail
-
isRunning
public boolean isRunning()
Tests if this daemon is alive.- Returns:
- true if this daemon is alive; false otherwise.
-
getName
public final String getName()
Returns thisdaemon's name.- Returns:
- this
daemon's name.
-
toString
public String toString()
Returns a string representation of this daemon.
-
setDaemon
public void setDaemon(boolean daemon)
Marks the daemon's thread as either a daemon thread a user thread. This method must be called before the daemon is started.- Parameters:
daemon- if true marks the daemon's thread as either a daemon thread.- Throws:
IllegalThreadStateException- if this daemon was already active.
-
setPriority
public void setPriority(int newPriority)
Changes the priority of this daemon. If the daemon is running calls the setPriority method on corresponding thread, else stores value for next start.- Parameters:
newPriority- the new priority.
-
setThreadGroup
public void setThreadGroup(ThreadGroup group)
Set the thread group to which this daemon's thread belongs. This method must be called before the daemon is started.- Parameters:
group- the thread group.- Throws:
IllegalThreadStateException- if this daemon was already active.
-
start
public void start()
Causes this daemon to begin execution. A new thread is created to execute the run method.- Throws:
IllegalThreadStateException- If the daemon was already started.
-
close
protected abstract void close()
Releases any resources attached to this daemon. Be careful, its method should be called more than one time.
-
shutdown
protected abstract void shutdown()
Interrupts a thread that waits for long periods. In some cases, we must use application specific tricks. For example, if a thread is waiting on a known socket, we have to close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general.
-
interrupt
public void interrupt()
Interrupts this daemon.
-
finish
protected final void finish()
-
stop
public void stop()
Forces the daemon to stop executing. This method notifies thread that it should stop running, if the thread is waiting it is first interrupted then the shutdown method is called to close all resources.
-
isCurrentThread
public boolean isCurrentThread()
Tests if the daemon's thread is the current one.- Returns:
- true if the daemon's thread is the current one, false otherwise.
-
setName
public void setName(String name)
Set the name of the daemon's thread.- Parameters:
name- the thread name.
-
-