jBPM 5.0 – creating simple Human Tasks with variables

The fastest way to get jBPM 5.0 running is to take these three simple steps:

1. Download the jBPM 5.0 installer: http://sourceforge.net/projects/jbpm/files/jBPM%205/jbpm-5.0-Final/
2. Run ant install.demo
3. Once everything is downloaded, run ant start.demo

Start off by creating a new BPMN file. To do so, select File->New->File, as the filename input “YourProcessName.bpmn” (thanks to the extension the Process Editor will open).

Inside the editor, create the business logic that you desire.

For this example I have created a process that looks like this:

I used two Script Tasks to set the variables and two User Tasks.
Script tasks are necessary because a Human Task can have only a single input parameter, “Content”.
To pass multiple variables “Content” must be set to a map that contains all the variables.

Begin by opening the properties tab of the project.
If you cannot see the properties view, open it using the menu “Window”, then “Show View” and “Other…”, and under the “General” folder select the Properties View.
Set the projects Id, Name, Package and variables (click on the “…” button in the Value section).
The map field should be a object type with its classname set to java.util.HashMap.
The other fields should be set to strings.
The end result should look like this:

jpbm-process-properties

Select the first Script task and open its properties tab.
Open the Action editor for the Action field (click on the “…” button in the Value section).
Set the dialect to Java.

Create a map and add the necessary variables:

map = new java.util.HashMap();

map.put("name","John Doe");
map.put("phoneNumber","123456789");

kcontext.setVariable("map", map);
kcontext.setVariable("name",name);
kcontext.setVariable("phoneNumber",phoneNumber);

The last three lines of this Action create globally accessible data stored in the Knowledge Session.

Now open the properties view for the First User Task.
Set ActorId to “krisv”.
Open the parameter mapping editor and add a mapping:
Parameter = “Content”, Value = “map”
This will allow us to use variables inside the Human Task Form.
Open the result mapping editor and add a mapping of the returned values:
Parameter = “phoneNumber”, Value = “phoneNumber”
Parameter = “name”, Value = “name”
Set Skippable to “false” and TaskName to “TaskOne”
The task name is important because it connects the task with its form.

Open the properties of the second Script Task and add the necessary action:


map.put("name",name);
map.put("phoneNumber",phoneNumber);

Now open the properties view for the Second User Task.
Set ActorId to “john”.
Open the parameter mapping editor and add a mapping:
Parameter = “Content”, Value = “map”
Set Skippable to “false”, TaskName to “TaskTwo”.

Now its time to create the Human Task Forms.
jBPM 5.0 uses ftl forms which are basically HTML.

Create two files, TaskOne.ftl and TaskTwo.ftl.

Their sources should look accordingly:

TaskOne.ftl:

<html>
<body>
<h2>First Task</h2>
<hr>

Name: ${name}<br>
Phone Number: ${phoneNumber}
<br>
<br>
<form action="complete" method="post"
enctype="multipart/form-data">
Name : <input name="name" type="text">
<br>
<br>
Phone number : <input name="phoneNumber" type="text">
<br>
<input value="Complete" type="submit"></form>

</body>
</html>

TaskTwo.ftl:

<html>
<body>
<h2>Task Two</h2>
Name: ${name} <br>
Phone Number: ${phoneNumber} <br>
<br>

<form action="complete" method="POST" enctype="multipart/form-data">
<input type="submit" value="Complete">
</form>
</body>
</html>

Create a screenshot of the process diagram and save it as a png file with its name set to the process Id (in this case
myproject.MyProcess.png).

Pack these three files (two .ftl files and a .png screenshot) into a .jar file.

There are two ways to deploy a process: by Guvnor or by hand.
The second option means that the .jar file has to be added to the servers classpath (jboss-dir/server/default/lib/),
and the .bpmn file has to be added to the directory where all bpmn files are stored – this directory is set in the ant build.xml file by the
-Djbpm.console.directory argument. The Default value is: ${install.home}/sample/evaluation/src/main/resources/.

When changes are made to the .ftl files, because the .jar is being added to the servers class path, the server has to be restarted.
When using the demo you can simply run ant stop.demo and ant start.demo.

When changes are made to the bpmn file, no restart is necessary. Just copy the new file to the appropriate directory and the
process will change accordingly.

When the deployment is completed, the task can be started.
There are two ways to accomplish this:
From the jBPM console or by an application.
Login into the jBPM console (localhost:8080/jbpm-console), go to Processes -> Process Overview, select the appropriate process and click “Start”.
Once the process has started you can login as krisv/krisv and view the human task form by selecting “view” for the appropriate task in the Personal Tasks tab.
Once you complete the form, login as john/john. The new variables you have entered should be visible in this users form.

The easiest way to start a task from an application is to create a jBPM project in Eclipse, select adding the Hello World example and slightly modify the ProcessTest.java code.
The final code should look like this:


package myproject;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.process.workitem.wsht.WSHumanTaskHandler;

/**
* This is a sample file to launch a process.
*/
public class ProcessTest {

public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new WSHumanTaskHandler());
// start a new process instance
ksession.startProcess("myproject.MyProcess");
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}

private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("MyProcess.bpmn"), ResourceType.BPMN2);
return kbuilder.newKnowledgeBase();
}


}

Once this file is ran as a Java Application, the process starts and can be accessed via the jBPM console for the user krisv.

While learning the basics of jBPM I found very helpful information here:
1. Getting started with jBPM 5 screencast: http://www.youtube.com/watch?v=-8pDrcGejxk
2. jBPM 5 Example – Human Task Forms with Variables: http://community.jboss.org/people/bpmn2user/blog/2011/02/21/jbpm5-example-for-forms-with-variables

4 Responses to “jBPM 5.0 – creating simple Human Tasks with variables”


  1. 1 Nitin 20/05/2011 at 07:20

    I go through this example , and it’s too good understand the concept of human task using jbpm

  2. 2 Nitin 20/05/2011 at 07:20

    I go through this example , and it’s too good understand the concept of human task using jbpm.

  3. 3 Vishal Sharma 30/05/2011 at 11:33

    i have created the example but i got an error that could not find work item handler for the Human Task.

    org.drools.WorkItemHandlerNotFoundException: Could not find work item handler for Human Task
    at org.drools.process.instance.impl.DefaultWorkItemManager.internalExecuteWorkItem(DefaultWorkItemManager.java:74)
    at org.jbpm.workflow.instance.node.WorkItemNodeInstance.internalTrigger(WorkItemNodeInstance.java:101)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.trigger(NodeInstanceImpl.java:122)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.triggerConnection(NodeInstanceImpl.java:185)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.triggerCompleted(NodeInstanceImpl.java:150)
    at org.jbpm.workflow.instance.node.ActionNodeInstance.triggerCompleted(ActionNodeInstance.java:55)
    at org.jbpm.workflow.instance.node.ActionNodeInstance.internalTrigger(ActionNodeInstance.java:51)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.trigger(NodeInstanceImpl.java:122)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.triggerConnection(NodeInstanceImpl.java:185)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.triggerCompleted(NodeInstanceImpl.java:150)
    at org.jbpm.workflow.instance.node.StartNodeInstance.triggerCompleted(StartNodeInstance.java:49)
    at org.jbpm.workflow.instance.node.StartNodeInstance.internalTrigger(StartNodeInstance.java:41)
    at org.jbpm.workflow.instance.impl.NodeInstanceImpl.trigger(NodeInstanceImpl.java:122)
    at org.jbpm.ruleflow.instance.RuleFlowProcessInstance.internalStart(RuleFlowProcessInstance.java:35)
    at org.jbpm.process.instance.impl.ProcessInstanceImpl.start(ProcessInstanceImpl.java:188)
    at org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl.start(WorkflowProcessInstanceImpl.java:302)
    at org.jbpm.process.instance.ProcessRuntimeImpl.startProcess(ProcessRuntimeImpl.java:124)
    at org.jbpm.process.instance.ProcessRuntimeImpl.startProcess(ProcessRuntimeImpl.java:105)
    at org.drools.common.AbstractWorkingMemory.startProcess(AbstractWorkingMemory.java:1094)
    at org.drools.impl.StatefulKnowledgeSessionImpl.startProcess(StatefulKnowledgeSessionImpl.java:297)
    at com.sample.ProcessTest.main(ProcessTest.java:26)

  4. 4 Vedanth 07/11/2011 at 10:07

    thank u Kuba Krzemien for the explanation of creating human tasks and deploying it.. it was really use full to us..
    Please let us know if there are any other examples of creating few complex tasks (invoking webservices) in jbpm


Leave a comment




Language / Język