Saturday, August 2, 2008

creating jBPM workflow programmatically

Hello readers of my blog. The purpose of this blog is to provide the best and creative ways of using the workflow engines. I am working on jBPM for the last 2 to 3 years. All examples and work available is creating the workflow by using jBPM graph designer plug-in.

We have some issues that we want to create the workflow on the fly based upon some specific criteria. When I started to look for the examples which could solve my problems, I found there is no such example present on the web so far. Now I wish to publish my work on jBPM workflow. I hope it will give the readers and developers better look and they can get the deep knowledge of jBPM APIs.

Now I will write step by step how to create workflow by using jBPM API.

Step 1: Create ProcessDefinition

ProcessDefinition processDefinition = new ProcessDefinition("process");

Step2: Create StartState
StartState startState = new StartState("start-state");

Step3: Add StartState to processDefinition

processDefinition .addNode(startState);

the flexiblity in this way of creating the workflow is we can easily add all nodes at any time to the processDefinition. When we have to create the transitions which will define the flow of the processes.

Step4: Creating Nodes (you can create the Nodes based upon some rules i.e TaskNode etc)

Node node = new Node("nodeName");

Step 5: Now Create the Events, Actions and Delegation. Delegation is more important as it defines the actionHandler associated with this Action which defines the Events.

Event event = new Event(Event.EVENTTYPE_NODE_ENTER);

Delegation delegation =
new Delegation("package.ActionHander");

Action action = new Action(delegation);

event.addAction(action);

node.addEvent(event);


Add this created Node to the processDefinition as described in Step3

Now we can create as many nodes and associate them with processDefinition. When we got any logic that we want to create parallel process or want to combine parallel processing to single processing we need Fork and Join.

In jBPM Fork and Join can be created by using the following code

Fork fork = new Fork("forkName");

and

Join join = new Join("joinName");

The most important in jBPM workflow is the transition. Workflow creater should know the importance of the transition and know how to control and properly use Transitions. Fork can have single arriving transition and multiple leaving transition while Join can have multiple arriving transition and only one leaving transition.

Step 6: Creating Transition

Transition transiton = new Transition();

transiton.setName(transitionName);

transiton.setFrom(previousNode);

transiton.setTo(nextNode);

Step 7: Associating the transition to the originating node

originatingNode.addLeavingTransition(transiton);

Step 8:
Creating the EndState. This state will instruct the jBPM engine that this workflow will end at this point.

EndState endState = new EndState("end-state");

Adding the endState to the processDefinition as described in Step3.

Step8:
Deploying the processDefinition to the jBPM database

JbpmConfiguration configuration = JbpmConfiguration.getInstance();

JbpmContext context = configuration.createJbpmContext();

context.getGraphSession().saveProcessDefinition("processDefinition ");

context.close();

Step 9: The most important thing is to do properly setpup jbpm-config.xml file

add transaction attribute to the service tag of jbpm-context. otherwise processDefinition cannot be deployed properly.

name option is "tx" and factory value is "org.jbpm.tx.TxServiceFactory"


Step 10: Enjoy jBPM and explore the beauty of jBPM programmatically.

Hopefully I am able to explain the world about workflow with jBPM.

Cheers

Anees-ur-Rehman
Chief Solution Architect

Infopro Solutions Sdn Bhd
ph: 0060-17-6960742

1 comment:

opeyemi ahmed said...

hi, i have a procesd definition file (.xml format), how do i get the list of tasks in that process definition? Thanks