org.apache.qpid.pool
Class Job

java.lang.Object
  extended by org.apache.qpid.pool.Job
All Implemented Interfaces:
Runnable, ReadWriteRunnable

public class Job
extends Object
implements ReadWriteRunnable

A Job is a continuation that batches together other continuations, specifically Events, into one continuation. The Events themselves provide methods to process themselves, so processing a job simply consists of sequentially processing all of its aggregated events. The constructor accepts a maximum number of events for the job, and only runs up to that maximum number when processing the job, but the add method does not enforce this maximum. In other words, not all the enqueued events may be processed in each run of the job, several runs may be required to clear the queue.

CRC Card
Responsibilities Collaborations
Aggregate many coninuations together into a single continuation.
Sequentially process aggregated continuations. Event
Provide running and completion status of the aggregate continuation.
Execute a terminal continuation upon job completion. JobCompletionHandler

Todo:
Could make Job implement Runnable, FutureTask, or a custom Continuation interface, to clarify its status as a continuation. Job is a continuation that aggregates other continuations and as such is a usefull re-usable piece of code. There may be other palces than the mina filter chain where continuation batching is used within qpid, so abstracting this out could provide a usefull building block. This also opens the way to different kinds of job with a common interface, e.g. parallel or sequential jobs etc., For better re-usability could make the completion handler optional. Only run it when one is set.

Field Summary
static int MAX_JOB_EVENTS
          Defines the maximum number of events that will be batched into a single job.
 
Constructor Summary
Job(ReferenceCountingExecutorService poolReference, int maxEvents, boolean readJob)
           
 
Method Summary
 boolean activate()
          Marks this job as active if it is inactive.
 void add(Runnable evt)
          Enqueus a continuation for sequential processing by this job.
 void completed()
          Implements a terminal continuation for the Job for this filter.
 void deactivate()
          Marks this job as inactive.
static void fireAsynchEvent(ExecutorService pool, Job job, Runnable event)
          Adds an Event to a Job, triggering the execution of the job if it is not already running.
 boolean isComplete()
          Tests if there are no more enqueued continuations to process.
 boolean isRead()
           
 void notCompleted()
           
(package private)  boolean processAll()
          Sequentially processes, up to the maximum number per job, the aggregated continuations in enqueued in this job.
 void run()
          Processes a batch of aggregated continuations, marks this job as inactive and call the terminal continuation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_JOB_EVENTS

public static final int MAX_JOB_EVENTS
Defines the maximum number of events that will be batched into a single job.

Constructor Detail

Job

public Job(ReferenceCountingExecutorService poolReference,
           int maxEvents,
           boolean readJob)
Method Detail

add

public void add(Runnable evt)
Enqueus a continuation for sequential processing by this job.

Parameters:
evt - The continuation to enqueue.

processAll

boolean processAll()
Sequentially processes, up to the maximum number per job, the aggregated continuations in enqueued in this job.


isComplete

public boolean isComplete()
Tests if there are no more enqueued continuations to process.

Returns:
true if there are no enqueued continuations in this job, false otherwise.

activate

public boolean activate()
Marks this job as active if it is inactive. This method is thread safe.

Returns:
true if this job was inactive and has now been marked as active, false otherwise.

deactivate

public void deactivate()
Marks this job as inactive. This method is thread safe.


run

public void run()
Processes a batch of aggregated continuations, marks this job as inactive and call the terminal continuation.

Specified by:
run in interface Runnable

isRead

public boolean isRead()
Specified by:
isRead in interface ReadWriteRunnable

fireAsynchEvent

public static void fireAsynchEvent(ExecutorService pool,
                                   Job job,
                                   Runnable event)
Adds an Event to a Job, triggering the execution of the job if it is not already running.

Parameters:
job - The job.
event - The event to hand off asynchronously.

completed

public void completed()
Implements a terminal continuation for the Job for this filter. Whenever the Job completes its processing of a batch of events this is called. This method simply re-activates the job, if it has more events to process.

Parameters:
session - The Mina session to work in.
job - The job that completed.

notCompleted

public void notCompleted()


Licensed to the Apache Software Foundation