net.sf.tuplespace.utils
Class UnitOfWorkRunner

java.lang.Object
  extended bynet.sf.tuplespace.utils.UnitOfWorkRunner
All Implemented Interfaces:
Runnable, UnitOfWork

public class UnitOfWorkRunner
extends Object
implements Runnable, UnitOfWork

A class that repeateadly executes a unit of work, always using the same TupleSpace.

This class allows subclasses to always be waiting for a Tuple., and do not have to concern themselves with writing the plumbing:

while (true) {
     Tuple t = tupleSpace.readAndRemove(template);
     // Work with t
 }

Instead, subclasses simply have to implement the execute(net.sf.tuplespace.TupleSpace) method, and are guaranteed to always be waiting for a tuple.


Constructor Summary
UnitOfWorkRunner()
          Instantiate a new unit of work, with default values.
UnitOfWorkRunner(UnitOfWork unitOfWork, TupleSpace tupleSpace)
          Instantiate a new unit of work.
 
Method Summary
 void execute(TupleSpace tupleSpace)
          Delegate to #unitOfWork.
 boolean isDone()
          Delegate to #unitOfWork.
 void run()
          Repeatedly call execute(), until either an exception is thrown, or isDone() is true.
 void setAbortOnException(boolean abortOnException)
          Determine what to do when exceptions occur.
 void setTupleSpace(TupleSpace tupleSpace)
          Sets the TupleSpace to use.
 void setUnitOfWork(UnitOfWork unitOfWork)
          Sets the unit of work to execute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnitOfWorkRunner

public UnitOfWorkRunner()
Instantiate a new unit of work, with default values.

It is imperative that you call setTupleSpace(net.sf.tuplespace.TupleSpace) otherwise you will get an IllegalArgumentException when calling run(), indicating that you haven't not provided a tuple space.


UnitOfWorkRunner

public UnitOfWorkRunner(UnitOfWork unitOfWork,
                        TupleSpace tupleSpace)
Instantiate a new unit of work.

It is illegal to provide a null TupleSpace.

Parameters:
unitOfWork - The UnitOfWork to run.
tupleSpace - The TupleSpace to use.
Method Detail

setTupleSpace

public void setTupleSpace(TupleSpace tupleSpace)
Sets the TupleSpace to use.

It is illegal to provide a null TupleSpace.

Parameters:
tupleSpace - The TupleSpace to use.

setUnitOfWork

public void setUnitOfWork(UnitOfWork unitOfWork)
Sets the unit of work to execute.

Parameters:
unitOfWork - The unit of work to repeateadly call.

setAbortOnException

public void setAbortOnException(boolean abortOnException)
Determine what to do when exceptions occur.

If this is true, run() will rethrow the caught exception as a RuntimeException. If this is false instead (the default), the exception will be logged and run() will loop and execute another unit of work.

Parameters:
abortOnException - true to make run() return when an exception occurs, false otherwise.

run

public final void run()
Repeatedly call execute(), until either an exception is thrown, or isDone() is true.

Each time through the loop, #tupleSpace will be asserted to non-null. If it is, an IllegalArgumentException will be thrown.

Also, after every call to execute(net.sf.tuplespace.TupleSpace), this method will call isDone() to determine if it should continue or not.

Specified by:
run in interface Runnable

execute

public final void execute(TupleSpace tupleSpace)
                   throws Exception
Delegate to #unitOfWork.

Specified by:
execute in interface UnitOfWork
Parameters:
tupleSpace - The space to provide to the delegee.
Throws:
Exception - Any exception that could be thrown from UnitOfWork.execute(net.sf.tuplespace.TupleSpace).

isDone

public final boolean isDone()
Delegate to #unitOfWork.

Specified by:
isDone in interface UnitOfWork
Returns:
true or false.