org.sapia.taskman
Class TaskManager

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--org.sapia.taskman.TaskManager
All Implemented Interfaces:
Externalizable, Runnable, Serializable

public class TaskManager
extends Thread
implements Externalizable

An instance of this class executes Task instances. An new task is added to the task manager through a TaskDescriptor. The following excerpt shows how to use a task manager:

  // The following adds a periodic task that is executed every 5 seconds.

 TaskManager mgr = new TaskManager();
 TaskDescriptor periodicTask = new PeriodicTaskDescriptor("periodic", 5000, new MyTask());
 mgr.execTaskFor(periodicTask);
 mgr.start();

  // Tasks can be added after the task manager has started:

 TaskDescriptor transientTask = new TransientTaskDescriptor("transient task", new MyTask());
 mgr.execTaskFor(transientTask);

  // The above adds a "transient task" - a task that will be executed only once.

 
The above code demonstrates how to add asynchronous tasks: tasks are added, and "eventually" executed. Yet, tasks can also be executed synchronously:

 TaskManager mgr = new TaskManager();
 mgr.start();
 mgr.execSyncTask("synchronous", new MyTask());

 // or

 mgr.execSyncTask("synchronous", new MyTask(), myOutput);

 
In the above snippet, the second case shows that it is possible for the caller to pass an instance of TaskOutput as a parameter. This allows a caller to trap task output information and send it where it pleases.

A task manager internally processes its list of tasks to execute and executes them one by one. Once the list has been processed, the task manager goes to sleep (in order not to consume precious cpu cycles if no task needs being executed for a given while) - this sleeping delay can be configured through the setRunInterval() method.

More specifically, the task manager behaves in the following way: if its task list is empty, the task manager waits for a new task to be added before waking up; if the task list is not empty, the task manager waits for the next time a task is ready for execution, or for the addition of a new task.

Author:
Yanick Duchesne 15-Apr-2003
See Also:
Serialized Form

Field Summary
static long RUN_INTERVAL
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TaskManager()
          Creates a new TaskManager.
TaskManager(String name)
          Creates an instance of this class with the given name.
 
Method Summary
 void addTaskDescriptors(List tasks)
          Adds the given list of task descriptors to this instance.
 void execAsyncTask(String name, Task t, TaskOutput out)
          Executes the given task asynchronously.
 void execSyncTask(String name, Task t)
          Executes the given task synchronously.
 void execSyncTask(String name, Task t, TaskOutput out)
          Executes the given task synchronously.
 void execTaskFor(TaskDescriptor desc)
          Asynchronously executes the task corresponding to the given descriptor.
 List getTaskDescriptors()
          Returns this instance's internal list of task descriptors in another list.
protected  TaskOutput newTaskOutput(String taskName)
          This template method is called every time a TaskOutput is created and associated with a Task.
 void readExternal(ObjectInput in)
           
 void run()
           
 void setRunInterval(long millis)
          Sets the interval at which this instance will wake up to process its list of internal tasks.
 void shutdown()
          Shuts down this instance.
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

RUN_INTERVAL

public static final long RUN_INTERVAL
See Also:
Constant Field Values
Constructor Detail

TaskManager

public TaskManager()
Creates a new TaskManager.


TaskManager

public TaskManager(String name)
Creates an instance of this class with the given name.

Parameters:
name - a name.
Method Detail

addTaskDescriptors

public void addTaskDescriptors(List tasks)
Adds the given list of task descriptors to this instance.


getTaskDescriptors

public List getTaskDescriptors()
Returns this instance's internal list of task descriptors in another list.

Returns:
a List of TaskDescriptor

setRunInterval

public void setRunInterval(long millis)
Sets the interval at which this instance will wake up to process its list of internal tasks.

Parameters:
millis - a time interval in milliseconds.

execTaskFor

public void execTaskFor(TaskDescriptor desc)
Asynchronously executes the task corresponding to the given descriptor.

Parameters:
desc - a TaskDescriptor

execSyncTask

public void execSyncTask(String name,
                         Task t)
Executes the given task synchronously.

Parameters:
name - the name of the task - will be passed to the TaskOutput that will be associated with the task.
t - a Task.

execSyncTask

public void execSyncTask(String name,
                         Task t,
                         TaskOutput out)
Executes the given task synchronously.

Parameters:
name - the name of the task.
t - a Task.
out - a TaskOutput.

execAsyncTask

public void execAsyncTask(String name,
                          Task t,
                          TaskOutput out)
Executes the given task asynchronously. The calling thread will return immediately, the task will be executed as soon as possible, logging its output to the given TaskOutput.

The calling thread can thus receive task output asynchronously.

Parameters:
name - the name of the task.
t - a Task.
out - a TaskOutput.

shutdown

public void shutdown()
Shuts down this instance. This method waits for the currently executing task to complete, then exits.


readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
IOException
ClassNotFoundException
See Also:
Externalizable.readExternal(ObjectInput)

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
IOException
See Also:
Externalizable.writeExternal(ObjectOutput)

newTaskOutput

protected TaskOutput newTaskOutput(String taskName)
This template method is called every time a TaskOutput is created and associated with a Task.

Parameters:
taskName - the name of the task for which to create a TaskOutput.

run

public void run()
Specified by:
run in interface Runnable
Overrides:
run in class Thread
See Also:
Thread.run()


Copyright © 2002 Sapia Open Community, Inc. All Rights Reserved.