Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

BeanFlow is a lightweight Java library for building workflows using beans to orchestrate events. You can think of BeanFlow as a simple alternative to BPEL where the workflows are all specified and implemented using Java code rather than declarative XML.

Motivation

When building highly concurrent or distributed applications it is very common for there to be many events happening; often asynchronously and in different threads and its very common to need to perform kinds of workflow or orchestration across these events.

...

First we'll give an overview of the two main interfaces in BeanFlow then we'll progress to concepts like timeouts and composing activities.

State class

We have a State<T> interface which is identical to the AtomicReference<T> interface included in java.util.concurrent in Java 5 apart from that it allows you to register listeners to the state. This makes it easy to respond to the changes in state.

One added detail is the that State<T> allows you to specify the Notifier implementation used to perform the notifications (which always occur outside of the semaphore to allow things to be re-entrant).

The default notifier used is the SynchronousNotifier but its easy to switch to an AsynchronousNotifier by just passing it into the constructor of the State<T> object.

...

An activity is just a POJO which implements the Activity interface; it has a simple lifecycle, you can start them and stop them and ask their status.

...

There is a useful base activity called TimeoutActivity which you can derive from to make your own activity which can be started/stopped/failed and which can be timed out.

...

Composition Activities

Description

JoinAll

Performs a join on all the given child activities. So the activity waits until all the child activities have completed, then it completes itself. You can add additional constraints to the activity using derivation. The default is to wait for all the child activities to complete; though you can enable fast-fail mode so that the activity fails as soon as a child activity fails

JoinQuorum

This activity is useful for implementing clustering style activities where you want a quorum of activities to complete. e.g. if you have 5 child activities you want to wait for at least 3 activities to complete succesfully before continuing

AsynchronousActivity

Executes any Runnable or Callable on an Executor and complete the activity when it has completed so that it can be used in joins

ParallelActivity

Allows a collection of parallel Runnable or Callable objects to be performed on an Executor then performing some kind of join on them completing

...

Wiki Markup
{snippet:id=example|lang=java|url=servicemix/smx3/trunk/common/servicemix-beanflow/src/test/java/org/apache/servicemix/beanflow/JoinAllTest.java}

...

Wiki Markup
{snippet:id=join|lang=java|url=servicemix/smx3/trunk/common/servicemix-beanflow/src/test/java/org/apache/servicemix/beanflow/CustomJoinConditionTest.java}

...

So we have a base class called Workflow which adds a new State object for the current step (which allows actvities to listen to the step changing) together with helper methods for moving to different steps, for suspending and so forth.

...

Wiki Markup
{snippet:id=workflow|lang=java|url=servicemix/smx3/trunk/common/servicemix-beanflow/src/test/java/org/apache/servicemix/beanflow/SimpleWorkflow.java}

...

Wiki Markup
{snippet:id=workflow|lang=java|url=servicemix/smx3/trunk/common/servicemix-beanflow/src/test/java/org/apache/servicemix/beanflow/ExampleWorkflow.java}

...

Wiki Markup
{snippet:id=workflow|lang=java|url=servicemix/smx3/trunk/common/servicemix-beanflow/src/test/java/org/apache/servicemix/beanflow/BadWorkflow.java}