to.etc.domui.util.janitor
Class Janitor

java.lang.Object
  extended by to.etc.domui.util.janitor.Janitor
All Implemented Interfaces:
java.lang.Runnable

public class Janitor
extends java.lang.Object
implements java.lang.Runnable

The Janitor class handles all housekeeping chores for NEMA and NEMA-derived applications. The janitor is started as a separate thread (by the ResourceBroker). It maintains a list of things-to-do, where each thing will be scheduled at a specified interval. Each janitor task will be scheduled in a separate thread. The max. number of threads (janitor jobs) can be specified. As soon a job finishes it is rescheduled (if required) at the interval specified. Since rescheduling will only take place after the task has finished there's no risk for running the same task twice. Internals: The janitor has a #of job slots. Each slot can contain a single thread. All Janitor threads derive from JanitorThread, a local class. The janitor's task list contains task entries, containing an interval and a reference to some class and method. The scheduler keeps all things to-do ordered by time-of-next-execution.


Constructor Summary
Janitor(int maxjobs)
           
 
Method Summary
 int addTask(int interval, boolean once, java.lang.String name, JanitorTask jt)
          Add a new task to the janitor's tables.
 int addTaskAt(java.util.Date attime, java.lang.String name, JanitorTask jt)
          Adds a task to the janitor's table.
 boolean cancelJob(int id)
           
static Janitor getJanitor()
          Returns the Janitor, the class handling housekeeping chores.
 long getTime()
          Returns the current time, in milliseconds, as returned from System.currentTimeMillis()
 long getTimeStamp()
          Returns the last scheduler's run timestamp.
protected  void logTask(JanitorThread jt, java.lang.String msg)
           
 boolean mustTerminate()
           
 void run()
          The main Janitor thread loop.
 void start()
          Starts this janitor as a new thread; it waits till the thread starts!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Janitor

public Janitor(int maxjobs)
Method Detail

start

public void start()
Starts this janitor as a new thread; it waits till the thread starts!


getTime

public long getTime()
Returns the current time, in milliseconds, as returned from System.currentTimeMillis()


logTask

protected void logTask(JanitorThread jt,
                       java.lang.String msg)

addTask

public int addTask(int interval,
                   boolean once,
                   java.lang.String name,
                   JanitorTask jt)
            throws java.lang.Exception
Add a new task to the janitor's tables. No duplicate task checking is done! The returned ID can be used to remove the task from the tables.

Parameters:
interval - interval in seconds
once - TRUE if this is a single-shot event
name - The name of the dude for info pps
jt - The task functor to call at time zero.
Throws:
java.lang.Exception

addTaskAt

public int addTaskAt(java.util.Date attime,
                     java.lang.String name,
                     JanitorTask jt)
              throws java.lang.Exception
Adds a task to the janitor's table. The task is to start at a given date/time(!) and runs only once. The date/time passed is only as accurate as the janitor's main loop allows! If the time that the task has to start is expired then the task IS posted and will execute the next time the janitor's main loop sees it.

Throws:
java.lang.Exception

cancelJob

public boolean cancelJob(int id)

mustTerminate

public boolean mustTerminate()

run

public void run()
The main Janitor thread loop. It sleeps for a minute, then it runs the scheduler.

Specified by:
run in interface java.lang.Runnable

getTimeStamp

public long getTimeStamp()
Returns the last scheduler's run timestamp. This is a VERY fast way of obtaining time - without incurring the cost of a system call! This time gets updated once a minute.


getJanitor

public static Janitor getJanitor()
Returns the Janitor, the class handling housekeeping chores.