refcodes-jobbus: Asynchronous job execution

refcodes-jobbus: Asynchronous job execution

README

The REFCODES.ORG codes represent a group of artifacts consolidating parts of my work in the past years. Several topics are covered which I consider useful for you, programmers, developers and software engineers.

What is this repository for?

The refcodes-jobbus artifact makes use of the Command pattern and provides a frame to work with Command (job) instances - also in a distributed environment (e.g. REST, SOA, Cloud Computing) and provides do/undo functionality.

The terms command and job are used interchangeably. To understand the benefits which the job-bus offers, we should briefly understand the Command pattern:

See the blog Having fun with the command pattern on how to do the undo with the refcodes-command artifact …

Simply speaking, you push a job into your job-bus. You can either wait for the job to be executed and grab its result when done or you use a handle to grab the result later on. The job is being executed asynchronously, it is up to the job-bus where and when it is being executed: For example the job could be transported to some remote JVM to be executed there or the job could get executed as soon as a worker thread is happy to grab the job from a job-queue.

How do I get set up?

To get up and running, include the following dependency (without the three dots “…”) in your pom.xml:

1 <dependencies>
2   ...
3   <dependency>
4     <groupId>org.refcodes</groupId>
5     <artifactId>refcodes-jobbus</artifactId>
6     <version>1.1.7</version>
7   </dependency>
8   ...
9 </dependencies>

The artifact is hosted directly at Maven Central. Jump straight to the source codes at Bitbucket. Read the artifact’s javadoc at javadoc.io.

How do I get started?

First of all you got to be aware which jobs you got to create for your project (or at least with which ones to start) and on which kind of context your jobs will be applied. Your jobs will actually implement either the Command or the Undoable interface. The terms job, commandand undoablewill be used interchangeable here and there. The context can by of any type, it can be a service, a client or any other JavaObject.

Given you want to apply various arithmetic operations on a list of int values and return the result. Starting with the SumCommandImpl, the code might look as follows:

 1 public class SumCommandImpl extends AbstractUndoable<List<Integer>, Integer, Exception> {
 2 
 3   /**
 4    * {@inheritDoc}
 5    */
 6   @Override
 7   public Integer execute( List<Integer> aContext ) {
 8     int theResult = 0;
 9     for ( Integer eInt : aContext ) {
10       theResult += eInt;
11     }
12     return theResult;
13   }
14 }

All the values contained in the context (being the list) will be summed up and the sum is returned by the execute method. That’s it. There may be additional jobs such as MaxCommandImpl, MinCommandImpl or AverageCommandImpl.

Your jobs may also modify (mutate) the context which would make a job being an Undoable applicable.

a) Create an instance of your JobBus and feed it with your context:

1 ...
2 List<Integer> context = new ArrayList<Integer>();
3 JobBusImpl<List<Integer>> jobBus = new JobBusImpl<List<Integer>>( context );
4 ...

b) Synchronously executing your Undoable (job) is straight forward:

1 ...
2 SumCommandImpl theSumCommand = new SumCommandImpl();
3 int theSumResult = jobBus.getResult( theSumCommand );
4 ...

c) For asynchronous execution, you got the remember a handle with which you can retrieve the result later:

1 ...
2 String theSumHandle = jobBus.execute( theSumCommand );
3 ...
4 if( jobBus.isExecuted( theSumHandle ) ) { 
5   int theSumResult = jobBus.getResult( theSumHandle );
6   ...
7 }
8 ...

You can also wait for execution to finish by calling jobBus.waitForExecution( theSumHandle ).

See the JobBusTest unit test for the source code of this example.

Contribution guidelines

Who do I talk to?

  • Siegfried Steiner (steiner@refcodes.org)

Terms and conditions

The REFCODES.ORG group of artifacts is published under some open source licenses; covered by the refcodes-licensing (org.refcodes group) artifact - evident in each artifact in question as of the pom.xml dependency included in such artifact.

comments powered by Disqus