fr.dyade.aaa.util

Class Daemon

Implemented Interfaces:
Runnable
Known Direct Subclasses:
BridgeUnifiedModule.ConsumerDaemon, BridgeUnifiedModule.ReconnectionDaemon, BridgeUnifiedModule.StartupDaemon, Driver, TcpConnectionListener, TcpReader, TcpServer.Monitor, TcpWriter

public abstract class Daemon
extends java.lang.Object
implements 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 ThreadGroup
group
The group of this thread
protected 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 Thread
thread
The active component of this daemon.

Constructor Summary

Daemon(String name)
Allocates a new Daemon object.
Daemon(String name, Logger logmon)
Allocates a new Daemon object.

Method Summary

protected void
close()
Releases any resources attached to this daemon.
protected void
finish()
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(ThreadGroup group)
Set the thread group to which this daemon's thread belongs.
protected 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.
String
toString()
Returns a string representation of this daemon.

Field Details

canStop

protected boolean canStop


group

protected ThreadGroup group
The group of this thread


logmon

protected Logger logmon


priority

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


running

protected 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


thread

protected Thread thread
The active component of this daemon.

Constructor Details

Daemon

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

Parameters:
name - the name of the new Daemon


Daemon

protected Daemon(String name,
                 Logger logmon)
Allocates a new Daemon object.

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

Method Details

close

protected void close()
Releases any resources attached to this daemon. Be careful, its method should be called more than one time.


finish

protected final void finish()


getName

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

Returns:
this daemon's name.


interrupt

public void interrupt()
Interrupts this daemon.


isCurrentThread

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


isRunning

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

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


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.


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(ThreadGroup group)
Set the thread group to which this daemon's thread belongs. This method must be called before the daemon is started.


shutdown

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


start

public void start()
Causes this daemon to begin execution. A new thread is created to execute the run method.


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.


toString

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

Returns:
A string representation of this daemon.


Copyright B) 2004 Scalagent - All rights reserved