org.jlf.log
Class QueueProcessor

java.lang.Object
  |
  +--org.jlf.log.QueueProcessor
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
SMTPQueueProcessor

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

The QueueProcessor class is an abstract class designed to offload the processing of a set of items to another thread. Any number of producers from any number of threads can enqueue items to the queue processor. Upon enqueuing the first item, the queue processor will start a thread to process any items added to the queue. To use all of this capability, all you need do is subclass this class and implement the abstract processItem() method.


Field Summary
protected static long NEVER_TIME_OUT_WAIT
          Constant to set the processor so it never times out while waiting for a request.
protected  java.util.Vector processorQueue
          This is a queue for the processor thread to use when processing the items.
protected  java.lang.Thread processorThread
          Holds the thread processing items queued.
protected  java.util.Vector producerQueue
          This is a queue for producer threads to add items that need processing.
protected  long timeOutInMilliseconds
          Timeout where the processor should wake up every so often to process events.
 
Constructor Summary
QueueProcessor()
          Default constructor, starts the thread processing queue items.
 
Method Summary
 void enqueue(java.lang.Object itemToBeProcessed)
          Adds an item to be processed, returning as quickly as possible so the calling thread doesn't have to wait for the item to be processed.
protected abstract  void processItem(java.lang.Object queueItem)
          Override this method to process any items your producers have queued.
protected  void processQueueItems()
          Iterates through the collection of processorQueue items to process them.
 void run()
          Main method to process the queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEVER_TIME_OUT_WAIT

protected static final long NEVER_TIME_OUT_WAIT
Constant to set the processor so it never times out while waiting for a request.

See Also:
Constant Field Values

timeOutInMilliseconds

protected long timeOutInMilliseconds
Timeout where the processor should wake up every so often to process events. The default, -1, makes it so the processor only wakes up when new items are added to the queue.


producerQueue

protected java.util.Vector producerQueue
This is a queue for producer threads to add items that need processing.


processorQueue

protected java.util.Vector processorQueue
This is a queue for the processor thread to use when processing the items. It is different from the producerQueue so that we can immediately free the lock on the producerQueue so producers aren't locked up while we process any items.


processorThread

protected java.lang.Thread processorThread
Holds the thread processing items queued.

Constructor Detail

QueueProcessor

public QueueProcessor()
Default constructor, starts the thread processing queue items. This constructor MUST be called or the processor thread won't start!

Method Detail

enqueue

public void enqueue(java.lang.Object itemToBeProcessed)
Adds an item to be processed, returning as quickly as possible so the calling thread doesn't have to wait for the item to be processed.


run

public void run()
Main method to process the queue. This method should NOT be overridden, implement the processItem() method instead!

Specified by:
run in interface java.lang.Runnable

processQueueItems

protected void processQueueItems()
Iterates through the collection of processorQueue items to process them.


processItem

protected abstract void processItem(java.lang.Object queueItem)
Override this method to process any items your producers have queued.