fr.dyade.aaa.util
Class Daemon

java.lang.Object
  extended byfr.dyade.aaa.util.Daemon
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
BridgeUnifiedModule.ConsumerDaemon, BridgeUnifiedModule.ReconnectionDaemon, BridgeUnifiedModule.StartupDaemon, Driver, TcpConnectionListener, TcpReader, TcpServer.Monitor, TcpWriter

public abstract class Daemon
extends java.lang.Object
implements java.lang.Runnable

The Daemon class represents a basic active component in a server. It provides usefull 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
protected  boolean canStop
          Boolean variable used to stop the daemon properly.
protected  java.lang.ThreadGroup group
          The group of this thread
protected  org.objectweb.util.monolog.api.Logger logmon
           
protected  int priority
          The priority that is assigned to the daemon.
protected  boolean running
          Boolean variable used to stop the daemon properly.
protected  java.lang.Thread thread
          The active component of this daemon.
 
Constructor Summary
protected Daemon(java.lang.String name)
          Allocates a new Daemon object.
protected Daemon(java.lang.String name, org.objectweb.util.monolog.api.Logger logmon)
          Allocates a new Daemon object.
 
Method Summary
protected abstract  void close()
          Releases any resources attached to this daemon.
protected  void finish()
           
 java.lang.String getName()
          Returns this daemon's name.
 void interrupt()
          Interrupts this daemon.
 boolean isCurrentThread()
          Tests if the daemon's thread is the current one.
 boolean isRunning()
          Tests if this daemon is alive.
 void setDaemon(boolean daemon)
          Marks the daemon's thread as either a daemon thread a user thread.
 void setPriority(int newPriority)
          Changes the priority of this daemon.
 void setThreadGroup(java.lang.ThreadGroup group)
          Set the thread group to which this daemon's thread belongs.
protected abstract  void shutdown()
          Interupts a thread that waits for long periods.
 void start()
          Causes this daemon to begin execution.
 void stop()
          Forces the daemon to stop executing.
 java.lang.String toString()
          Returns a string representation of this daemon.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Field Detail

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.

See Also:
start, stop

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 interupted, else it handles a request and it will exit after (it tests the running variable between each reaction)


thread

protected java.lang.Thread thread
The active component of this daemon.


priority

protected int priority
The priority that is assigned to the daemon.


group

protected java.lang.ThreadGroup group
The group of this thread


logmon

protected org.objectweb.util.monolog.api.Logger logmon
Constructor Detail

Daemon

protected Daemon(java.lang.String name)
Allocates a new Daemon object.

Parameters:
name - the name of the new Daemon

Daemon

protected Daemon(java.lang.String name,
                 org.objectweb.util.monolog.api.Logger logmon)
Allocates a new Daemon object.

Parameters:
name - the name of the new Daemon
logmon - inherited logging monitor
Method Detail

isRunning

public boolean isRunning()
Tests if this daemon is alive.

Returns:
true if this daemon is alive; false otherwise.

getName

public final java.lang.String getName()
Returns this daemon's name.

Returns:
this daemon's name.

toString

public java.lang.String toString()
Returns a string representation of this daemon.

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.

Throws:
java.lang.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.

See Also:
Thread.setPriority

setThreadGroup

public void setThreadGroup(java.lang.ThreadGroup group)
Set the thread group to which this daemon's thread belongs. This method must be called before the daemon is started.

Throws:
java.lang.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:
java.lang.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()
Interupts 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 interupted then the shutdown method is called to close all ressources.


isCurrentThread

public boolean isCurrentThread()
Tests if the daemon's thread is the current one.



Copyright ? 2004 Scalagent - All rights reserved