Home JBpm JBPM tutorial 1
12 | 03 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Login
Sign here for the NewsLetter.



Poll
What book could be in your wish list next XMas ?
 
JBoss admin resources
Banner
JBPM Books
JBoss howto

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More

How do I configure a Queue/Topic to work in a cluster?

JBoss recipe of the day ...
Read More
JBPM tutorial 1
Written by Mark S.   

JBoss jBPM is a flexible, extensible framework for process languages which uses graph oriented programming as a foundation. As the word says it, a graph based language represent some kind of execution which can be shown as a graph (Imagine a simple flow chart which explains the track of a business decision for example).

Business process management can be defined as a procedure in an organization: some of this procedures can be automated, some need human work.

Why we need a BPM in Java ?

the basic of an interaction with other systems is the exchange of information/messages

sendInformationToAreaB();

Response r = waitForResponseFromB();

What is the limitation with this ? the first limitation is that by itself this little code is not persistable, yes of course you can wrap it with some Object/Relational tool...but what about waiting states ? even if you implement it as a Thread you cannot persist a Java Thread.

The second one is that you might need a graphical representation of this......for example your Boss wants to take a look at your process...or even more, what about if business changes ? without a native graphical implementation of the process you have to do double work: re-engineering the Code and the Graph with your favourite tool.

So now let's get our hand dirty ! The first thing to do is to download a fresh copy of JBPM fron jBoss site. For this article we'll use jbpm-jpdl-suite-3.2.3.zip downloadable from www.jboss.org site.

This archive includes embedded application server (JBoss of course) and the graphical designer. Before entering in the details let's make clear one thing: usually BPM engines have a tight coupling with the graphical environment : meaning you have to use the graphical editor to draw or change your process. JBPM is basically a process management Api built on Hibernate (which maps the basic workflow tables) and it can live well without any graphical editor. It does exists a plugin built with Eclipse technology which lets you reverse-engineer your graphical process into an XML file, yet you can still design your process with a simple notepad and a Java compiler.

So keep separated the concept of the Graphical Process Designer (GPD), which will be used by analyst/ top managers to describe the process and the process it self which is only xml and pure Java. Nice separation of tasks between analysts and developers isn't it ?







So we talked about XML: what is the process language of jBPM? there are more then one but just for beginning we'll concentrate on the most common: that is jPDL. JPDL is an XML structured language which has a specific process grammar to describe process definitions, nodes and transitions.

Meanwhile you read this article I guess you have finished downloading jbpm-jpdl-suite-3.2.3.zip. So unzip it and launch the designer.bat (or designer.sh for Unix/Linux users) under the "designer" folder.

Once Eclipse is started, launch the "New..." Menu and select new Process project

  JBPM tutorial

Then choose a convenient name for your Project and wait until you'll see your Project in the Package explored. Resist one more minute before looking at your process: open the JBPM library tag in the Package explorer: you'll see the libraries needed to run your process from a standalone application. Quite a lot, isn't it ? but only two of them are the core jBPM libraries, all the others are just dependancies.

  • jbpm-3.x.x.jar
  • jbpm-identity-3.x.x.jar

So if you plan to run your application on a JBoss application server these 2 are the only libraries you'll need to add to your war/ear file. We'll start with a simple process which describes an order management system.

The process is quite straightforward : once the process is started we meet a Task Node: in short a task node represents an activity that is performed by humans. This task will be in charge of user "buyer".

Once the order is performed we have a Decision Node where the process can lead to different Nodes based on the data at its disposal. In our case we check if the goods ordered are available. If they're not available the process is switched to a State Node named "GetFromStock". A State Node simply tells the process instance to wait and, in contrast to a task node, it doesn't create an activity in anybody's task list.

At last our goods needs to be shipped and this will be another task in charge to our Deliver Office. Once also this task is completed our process will come to a end.

Ok, so what's behind this picture ? jBPM uses jPDL to describe the process in XML format.

<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="simple">
 <swimlane name="buyer">
   <assignment actor-id="customer"></assignment>
 </swimlane>

 <swimlane name="deliverDepartment">
 <assignment actor-id="deliver-man"></assignment>
 </swimlane>

 <start-state name="start">
  <transition name="trPlaceOrder" to="PlaceOrder"></transition>
</start-state>

 <task-node name="PlaceOrder">
  <task name="placeOrderTask" swimlane="buyer">
  </task>
  <transition name="" to="CheckAvailability"></transition>
 </task-node>

 <decision name="CheckAvailability">
  <handler class="com.sample.decision.VerifyOrder"></handler> 
  <transition name="trDeliver" to="Deliver"></transition>
  <transition name="trGetFromStock" to="GetFromStock"></transition>
 </decision>

 <state name="GetFromStock">
  <event type="node-enter">
  <action class="com.sample.action.GetFromStockActionHandler" name="recharge"></action>
  </event>
  <transition name="checkAvailability" to="CheckAvailability"></transition>
 </state>

 <task-node name="Deliver">
  <task name="getFromStock" swimlane="deliverDepartment">
  </task>
  <transition name="trEnd" to="end"></transition>
 </task-node>

 <end-state name="end"></end-state>
</process-definition>
 

You can clearly see how the graphical elements have been translated into XML source code.

jPDL in not too complicated, in short it will associate actions with handlers. Handlers are written as plain Java classes implementing business interfaces.







For example, the decision node CheckAvailability delegates to a DecisionHandler which path should be followed by our process. Here we'll use a very simple decision handler which compares two process variables in order to decide if there are enough good in the stock. In real world process, we'll likely query a Database in order to collect this information.  
package com.sample.decision;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.node.DecisionHandler;

public class VerifyOrder implements DecisionHandler {


 public String decide(ExecutionContext executionContext) throws Exception {

 Integer goodsOrdered = (Integer)executionContext.
                getContextInstance().getVariable("goodsOrdered");
 System.out.println("Goods ordered "+goodsOrdered.intValue());


 Integer goodsInStock = (Integer)executionContext.
                getContextInstance().getVariable("goodsInStock");
 System.out.println("Goods in stock "+goodsInStock.intValue());

 if (goodsOrdered.intValue() > goodsInStock.intValue()) {
  return "trGetFromStock";
 }
 else {
  return "trDeliver";
 }
}

}

Decision Handler are not the only example of Handler provided by jBPM. We have also ActionHandlers which will be used in case we need to get supply for our goods from the Stock. Again here we need to implement the correct interface which will be fired on the event type "node-enter" for the Node "GetFromStock". 

package com.sample.action;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

public class GetFromStockActionHandler implements ActionHandler {

 public void execute(ExecutionContext context) throws Exception {
  System.out.println("Getting goods from factory!");

  context.getContextInstance().
         setVariable("goodsInStock", new Integer(300));

  context.leaveNode();
 }

} 

Here we need to implement the method execute which will be used to fire our business logic, in practice we fill up our stock and when done we leave the node as we've got nothing else to do.

The last thing we left our are swimlanes.

Swimlanes represents process roles and they're used to assign tasks to specific people. Unlike other components they aren't mapped visually in the process definition but they are added as techical detail.







Swimlanes can be one actor or a pooled group of actors: in our example we simply define 2 swimlanes: one for the buyer, who places the order, and one for the deliverDepartment which deliver the goods. Ok, now need a client to test our Process: this can be anything , a Servlet, an EJB or a plain Java Class as our sample. 
package com.sample;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class SimpleProcessTest {
	public static void main(String args[])throws Exception {
		new SimpleProcessTest().execute();
	}
	public void execute() throws Exception {

		// Extract a process definition from the processdefinition.xml file.
		ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("simple/processdefinition.xml");
		HashMap map = new HashMap();

		map.put("goodsOrdered",new Integer(100)); 
		map.put("goodsInStock",new Integer(50));

		// Create an instance of the process definition.
		ProcessInstance instance = new ProcessInstance(processDefinition,map);

		displayStatus(instance);

		instance.signal();

		displayStatus(instance);

		executeTask(instance);

		displayStatus(instance);

		executeTask(instance);
		displayStatus(instance);

	}

	private void displayStatus(ProcessInstance instance) {   
		String nodeName = instance.getRootToken().getNode().getName();   
		System.out.println("You are now in node: "+nodeName);   

	}   
	/* This is a dummy method which prints out the list of TaskInstances  and completes all the single tasks   
	 */  
	private void executeTask(ProcessInstance instance) {   

		Collection c = instance.getTaskMgmtInstance().getTaskInstances();   
		Iterator iter = c.iterator();   
		while (iter.hasNext()) {   

			TaskInstance taskInstance = (TaskInstance)iter.next();   

			if (!taskInstance.hasEnded())   
				taskInstance.end();   
			System.out.println("Found Task "+taskInstance.getName());   
			System.out.println("Task completed.");   
		}   


	}   

}


As you can see, we need to create an instance of ProcessDefinition to map our Process as a Java class. Once we've our ProcessDefinition we create a ProcessInstance which represents is our Customer Order, so for every Definition we've got many ProcessInstances.

Our client simply sends signals to our process telling to advance to the next node. When we've entered a Task Node we read the task list and we end our Task. In this case we use another class to map our Tasks, the TaskInstance class which holds all the information concerning our task.

Running our application will produce the following output:

We're in state start
We're in state PlaceOrder
Found task placeOrderTask in charge to customer
Goods ordered 100
Goods in stock 50
Getting goods from factory!
Goods ordered 100
Goods in stock 300
We're in state Deliver
Found task getFromStock in charge to deliver-man
We're in state end


Conclusion

JBoss jBPM provides a sophisticated platform for designing and developing workflows and business process management systems. Its framework of APIs, domain-specific language, and graphical modeling tool enables developers and business analysts to communicate and operate using a common platform. In this article, I discussed the high-level concepts of JBoss jBPM that will give you a place to start your journey towards a workflow-oriented future with BPM and Java.

Additional resources:
Hibernate tutorial
Download here the code from this example


JBoss.org Search
Custom Search
Comments
Search
student   |2008-10-22 16:18:07
very good tutorial indeed. thanks
Anonymous   |2008-10-29 08:55:05
very good tutorial but where are the assignment handlers and how do they work
?

Thanks in advanced
Mark   |2008-10-29 10:42:23
Hello,
in this example there is not Assignment Handler: you can choose to
assign a task either with an expression or with an Assignment Handler.
You can
use Assignment Handler when you need to do evaluate more complex rules.
In some
days I'll add a tutorial about Assignment Handler......stay tuned
SE-Newbie   |2008-11-12 22:30:50
I'm not sure if this question belongs here but I gotta try.
Is there a way to
run JBMP without the web-console?
I have bunch of webforms, richfaces, and
wanted to integrate them in a system controlled by JBPM machinery.

cheers
admin   |2008-11-14 12:36:16
yes sure! web-console is only a built-in front-end for your process.
wrap the
class SimpleProcessTest into a Servlet for example. Remember JBPM is nothing
else then Java classes wrapped on a Database with hibernate.
Priit  - Thanks     |2008-11-14 12:02:09
Thanks a lot. This tutorial helped me on track with jbpm. You could include
packages in your source so importing it would be easier.

I used only
jbpm-jpdl.jar with its mandatory dependencies to commons-logging and dom4j so my
setup was simple.

Thanks again jbpm documentation lacks this kind of simple
getting started example.
alihammad  - a few suggestion   |2009-01-15 14:08:19
This is one amazingly simple and useful tutorial. I must accept that I was stuck
in learning jBPM but you have made thing easy for me. You clarified a lot of
things.

Thanks for sharing

I have only one question.

I have created an
Eclipse project and did everything right ... Now can you please tell me how have
you run the application ?

Please guide me how to run the application so that I
can also see the similar output on the console.

I would suggest that you should
upload an Eclipse project, that will be a great help to a newbie like me
.

Secondly, it would be of great help if upload some snap shots of how to run
the application.

Thank you very much again for sharing.
rohitrai  - Creating a new instance of a process from a java p   |2009-04-08 11:49:42
Hi!
I am very new to jBPM and exploring how to use jBPM in my web
application. As a proof of concept for my project i want to create a simple java
program which will create a new instance of a process and access the workflow
states. Most of the samples i have seen show integration with Seam. is it
possible only with Seam or can i integrate jBPM with my application that has
been built entirely on JSP and Servlets.

Any pointers ????

Thanks
admin   |2009-04-08 11:55:54
Hello,
definitely yes. Seam simplify your JBPM programming but it's not at all
required. Make your JBPM libraries + hibernate xml file available to your web
application. That's all.
This question is quite popular, maybe I'll write a
small tutorial about it soon.
regards
Francesco
rohitrai  - That would be great - specially for people who hav   |2009-04-08 12:37:28
Hi!
Thanks for the reply. Such a tutorial would be great for those of us
starting out. I am struggling with this problem for the last 4 days ....


Regards
rohitrai  - A sample application that is not working :(   |2009-04-08 11:55:19
I am trying to do a sample program as shown below.
import
org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import
org.jbpm.graph.def.ProcessDefinition;
import
org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;

import
com.sun.org.apache.xpath.internal.functions.FuncBo olean;
public class Test
{

static ProcessDefinition processDefinition =
ProcessDefinition.parseXmlResource("TestSheetP
rocess/processdefinition.xml";;
static JbpmConfiguration
jbpmConfiguration = JbpmConfiguration.getInstance();
static JbpmContext ctx =
jbpmConfiguration.createJbpmContext();




public static void
main(String[] args) {
//ProcessDefinition processDefinition =
ProcessDefinition.parseXmlResource("TestSheetP
rocess/processdefinition.xml";
//ProcessIn stance proc =
processDefinition.createProcessInstance();

//JbpmConfiguration jbpmcfg =
JbpmConfiguratio...
admin   |2009-04-08 11:59:49
I've got the full code attached - so I can see that's a datasource issue.
Verify that you have correclty deployed your datasource in the right namespace-
have a JNDI tree dump from the JMX console to check it.
regards
Francesco
rohitrai  - JNDI tree dump from my system   |2009-04-08 12:56:29
Hi!
I have taken the tree dump that has an entry for jBPM ds as shown
below.I am not sure what i need to check out with this
info.

JbpmDataSource
java:/JbpmDS
javax.namin g.LinkRef

Shit! m feeling
really dumb
admin   |2009-04-08 14:25:08
mmm I can see you have deployed the Datasource in the java: namespace. It
won't be accessible outside of the JVM, maybe you are using a
remote client.
Try deploying your datasource in the global namespace

Code:
  JbpmDS  
rohitrai  - ok .... i am actually writing a sample program   |2009-04-09 06:55:13
Hi!
Yes i think its something like a remote client. I am actually trying to
write an independent sample java program that will create a process from outside
the jbpm-console.

oooo i am horribly confused
Marco   |2009-04-09 16:37:30
Hi there,

First of all thanks for your article.

I checked out your
tutorial, and unfortunately I bump into something I cannot explain.

When I
enter the node Deliver it tries to run the PlaceOrder task again.

Even
stranger, when I try to debug it, it first runs the desired getFromStock task,
and then the PlaceOrder task.

Both times result in an
java.lang.illegalStateException (as they should I guess).

I haven't made any
changes to the tutorial code .. any clues?

Thanks in advance,
Marco
salah  - same problem as Marco   |2009-04-10 12:42:21
Hi everybody,

I've tried to deploy tutorial source, but I've got this return
back to task PlaceOrdeTask after Deliver.... and an illegalStateException
because the program tries to end a task which is already finished.

i've checked
the source no problems in it.

Other thing: task of tasknode Deliver is named
GetFromStock is it normal?


Salah
admin   |2009-04-15 10:24:00
Thank you for pointing out the bug.
The method executeTask needed a check on every Task before calling end() on it.

As a matter of fact the Collection of TaskInstances contain
both ended and not ended tasks and you can call end() only on Task
not ended of course.

Simply update the executeTask method with the
one which I have corrected in the article.
Regards
Francesco
rohitrai  - jpdl - designer as a front end for the users   |2009-04-24 12:13:27
Hi!
We came up with an interestind discussion today. What we have
thought of is - to provide a online web based designer - similar to the
one that we get in eclipse - to the end users of the application
itself so that they may change the process as and when they want it with
some customization.

I checked out some tool :

http://oryx-editor.org

Is there any tool like this available for jpdl?

Thanks,
Rohit
admin   |2009-04-27 10:47:39
Hello,time ago I have found some threads on the jbpm forum (at jboss.org) of
people which started something like that.
I also have suggested to a customer
to follow this approach because there was a strong UI requirement for the
process: the only drawback is that you have to code manually the process,
however that's not a big deal IMHO.
If you have any interesting prototype you
want to share with us, let me know and I'll publish it here.
Regards
prajatna  - The flow is starting again...   |2009-07-30 12:55:29
Hi,
Thanks for the nice tutorial.. .I tried developing a JBPM of a
simple travel request process of my company.All things are working
fine, except ,after control is going to end state, again it is coming
back to the previous node...I have no idea , what is going wrong..

Can you
plz help me in this regards...I ahve attached all the files over
here..

Process-definition.xml























 














 





















 




TravelProcessTest.java (Clas for invoking the process)

Code:
package
com.sample;

import java.util.Collection;
import java.util.Iterator;


import junit.framework.TestCase;

import org.jbpm.JbpmConfiguration;
i
mport org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinitio
n;
import org.jbpm.graph.exe.ProcessInstance;
import...
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."