org.apache.droids.impl
Class MultiThreadedTaskMaster<T extends Task>

java.lang.Object
  extended by org.apache.droids.impl.MultiThreadedTaskMaster<T>
Type Parameters:
T -
All Implemented Interfaces:
TaskMaster<T>

public class MultiThreadedTaskMaster<T extends Task>
extends java.lang.Object
implements TaskMaster<T>

This task master provides a base implementation that support multithreaded task processing powered by a ThreadPoolExecutor.

By default, the ThreadPoolExecutor uses a bounded blocking queue with a size as same as the maxThreads. As long as there is any outstanding task in the Task Queue and the Executor has spare capacity, a new Thread will be created to poll and handle one task from the Task Queue. User may set to use another 'pool' implementation, and the nextTask() method may be overriden by sub-class.

This Task Master doesn't support: pausing, monitoring etc. TaskExceptionHandler is unimplemented.

And there is no failure handling mechanism, e.g. if the JVM of Task Master is crashed, any task polled from the Task Queue will be loss. (this point makes sense only if the Task Queue is persistent)


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.droids.api.TaskMaster
TaskMaster.ExecutionState
 
Field Summary
protected  java.util.concurrent.atomic.AtomicLong completedCount
           
protected  DelayTimer delayTimer
           
protected  Droid<T> droid
           
protected  TaskExceptionHandler exHandler
           
protected  java.util.Date finishedWorking
           
protected  T lastCompletedTask
           
protected  org.apache.commons.logging.Log log
           
protected  int maxThreads
           
protected  WorkMonitor<T> monitor
           
protected  java.lang.String name
           
protected  java.util.concurrent.ThreadPoolExecutor pool
           
protected  TaskQueue<T> queue
           
protected  java.util.Date startedWorking
           
protected  TaskMaster.ExecutionState state
           
 
Constructor Summary
MultiThreadedTaskMaster()
           
 
Method Summary
 void awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
           
 long getCompletedTasks()
           
 DelayTimer getDelayTimer()
           
 TaskMaster.ExecutionState getExecutionState()
           
 java.util.Date getFinishedWorking()
           
 T getLastCompletedTask()
           
 int getMaxThreads()
          Get number of maximum allowed threads
 WorkMonitor<T> getMonitor()
           
 java.lang.String getName()
           
 java.util.Date getStartTime()
           
 void processAllTasks(TaskQueue<T> queue, Droid<T> droid)
          The queue has been initialized
 void setDelayTimer(DelayTimer delayTimer)
           
 void setExceptionHandler(TaskExceptionHandler exHandler)
           
 void setMaxThreads(int count)
          Set the maximum allowed thread count.
 void setMonitor(WorkMonitor<T> monitor)
           
 void setName(java.lang.String name)
           
 void shutdownAndAwaitTermination()
          Shutdown all threads, close the pools and leave.
protected  java.util.concurrent.Future submitNewTaskRunner(java.util.concurrent.ExecutorService executor, TaskQueue<? extends T> queue)
          This method is designed for sub-classing

TODO: refactor and create a specialized thread that - construct with the delayTimer and transparently apply the delay - construct with the Task and transparent set the lastCompleteTask

TODO: consider to provide all variables as argument so that the method overrider needs not to call the parent variables

TODO: caller of this method could use the Future to track the task completion and perform the termination and other checking.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final org.apache.commons.logging.Log log

name

protected java.lang.String name

pool

protected java.util.concurrent.ThreadPoolExecutor pool

maxThreads

protected int maxThreads

queue

protected TaskQueue<T extends Task> queue

droid

protected Droid<T extends Task> droid

delayTimer

protected DelayTimer delayTimer

monitor

protected WorkMonitor<T extends Task> monitor

startedWorking

protected java.util.Date startedWorking

finishedWorking

protected java.util.Date finishedWorking

lastCompletedTask

protected T extends Task lastCompletedTask

state

protected volatile TaskMaster.ExecutionState state

completedCount

protected java.util.concurrent.atomic.AtomicLong completedCount

exHandler

protected TaskExceptionHandler exHandler
Constructor Detail

MultiThreadedTaskMaster

public MultiThreadedTaskMaster()
Method Detail

processAllTasks

public void processAllTasks(TaskQueue<T> queue,
                            Droid<T> droid)
The queue has been initialized

Specified by:
processAllTasks in interface TaskMaster<T extends Task>

submitNewTaskRunner

protected java.util.concurrent.Future submitNewTaskRunner(java.util.concurrent.ExecutorService executor,
                                                          TaskQueue<? extends T> queue)
This method is designed for sub-classing

TODO: refactor and create a specialized thread that - construct with the delayTimer and transparently apply the delay - construct with the Task and transparent set the lastCompleteTask

TODO: consider to provide all variables as argument so that the method overrider needs not to call the parent variables

TODO: caller of this method could use the Future to track the task completion and perform the termination and other checking.

Parameters:
executor -
queue -
Returns:

setMaxThreads

public void setMaxThreads(int count)
Set the maximum allowed thread count. If the new value is less than the current value, excess existing threads will be terminated when they become idle.

Parameters:
count -

getMaxThreads

public int getMaxThreads()
Get number of maximum allowed threads

Returns:
the number of maximum threads that we allow

getExecutionState

public TaskMaster.ExecutionState getExecutionState()
Specified by:
getExecutionState in interface TaskMaster<T extends Task>

shutdownAndAwaitTermination

public void shutdownAndAwaitTermination()
Shutdown all threads, close the pools and leave. If it is not working by asking nice to shutdown just kill all threads.


awaitTermination

public void awaitTermination(long timeout,
                             java.util.concurrent.TimeUnit unit)
                      throws java.lang.InterruptedException
Specified by:
awaitTermination in interface TaskMaster<T extends Task>
Throws:
java.lang.InterruptedException

setExceptionHandler

public final void setExceptionHandler(TaskExceptionHandler exHandler)

getDelayTimer

public DelayTimer getDelayTimer()

setDelayTimer

public void setDelayTimer(DelayTimer delayTimer)

getCompletedTasks

public long getCompletedTasks()
Specified by:
getCompletedTasks in interface TaskMaster<T extends Task>

getFinishedWorking

public java.util.Date getFinishedWorking()
Specified by:
getFinishedWorking in interface TaskMaster<T extends Task>

getLastCompletedTask

public T getLastCompletedTask()
Specified by:
getLastCompletedTask in interface TaskMaster<T extends Task>

getStartTime

public java.util.Date getStartTime()
Specified by:
getStartTime in interface TaskMaster<T extends Task>

getMonitor

public WorkMonitor<T> getMonitor()

setMonitor

public void setMonitor(WorkMonitor<T> monitor)

getName

public java.lang.String getName()

setName

public void setName(java.lang.String name)


Copyright © 2007-2009. All Rights Reserved.