Human Tasks in jBPM 5

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

jbpm5 jbpm tutorial human task managementIn this tutorial we will continue our journey through jBPM5 exploring the Human Task  which is based on the WS-HumanTask specification

 


Many end-to-end business processes require human interactions with the process. For example, humans may be needed for approvals, exception management, or performing activities required to advance the business process.  jBPM supports a special human task node inside processes for modeling this interaction with human users.

In order to use human tasks inside your processes you need three components:

1) A Human task node
2) A Human Task Server (to be precise a task management component)
3) A Human Task Client


Release comparison: In jBPM 3 Task nodes are just a special kind of wait state node that lives in the same JVM as the process. In jBPM 5, you need a Task Server where task are registered and a Task Client which is used to interact with tasks.

It will be more clear with an example. Start a new jBPM project choosing to add a default example which includes Human Tasks management.
jbpm 5 tutorial human tasks
In this very simple example, we have two Human Task nodes.

jbpm 5 tutorial human tasks

By clicking on its properties we can see that the Tasks are assigned to the user John an Mary
jbpm 5 tutorial human tasks jbpm5
Looking at the code source, we have the minimal requirement to start the process.

package com.sample;

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.runtime.StatefulKnowledgeSession;
import org.jbpm.test.JBPMHelper;

public class ProcessMain {

    public static final void main(String[] args) throws Exception {
        startUp();
        // load up the knowledge base
        KnowledgeBase kbase = readKnowledgeBase();
        StatefulKnowledgeSession ksession = JBPMHelper.newStatefulKnowledgeSession(kbase);
        // start a new process instance
        ksession.startProcess("com.sample.bpmn.hello");
        System.out.println("Process started ...");
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2);
        return kbuilder.newKnowledgeBase();
    }
    
    private static void startUp() {
        JBPMHelper.startH2Server();
        JBPMHelper.setupDataSource();
        // please comment this line if you already have the task service running,
        // for example when running the jbpm-installer
        JBPMHelper.startTaskService();
    }
    
}

As you can see, this example project uses the jBPMHelper class. The jBPMHelper class can be used to getting started quickly with jBPM5. Let’s see in detail what these method provide:

This method starts the H2 in memory database where all process instances will be stored.

JBPMHelper.startH2Server();

This methods is used to set up a local datasource bound in the JNDI namespace jdbc/jbpm-ds
Inside the META-INF/persistence.xml you can find two persistence units named org.jbpm.persistence.jpa and org.jbpm.task which will be used to persist the process data
 JBPMHelper.setupDataSource();

Finally, this method is used to start a generic Task Service where all Task will be registered.

JBPMHelper.startTaskService();


In real world application you would rather create your own TaskService and adapt it to your process. Here’s a sample code which can be used to start a Task Service:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.task");

TaskService taskService = new TaskService(emf, SystemEventListenerFactory.getSystemEventListener());

MinaTaskServer server = new MinaTaskServer( taskService );

Thread thread = new Thread( server );

thread.start();

Testing the process

 

Ok, now run the application. The process will be started and the first task will be assigned to user “john”. In order to interact with Tasks, switch to the Human Tasks View (Windows | Show view | Others | Drools tasks | Human Tasks View)

Enter user “john” and hit “Refresh”:

jbpm 5 tutorial human tasks jbpm5
The Task has been reserved to user John. In order to proceed, choose “Start” so that the Task will move to “In Progress” state, until the user has completed the Human Task.
When the user has finally completed the Task, choose “Complete”.
jbpm 5 tutorial human tasks jbpm5
At this point, the process execution will move to the next Human Task node which has been assigned to Mary as you can see from the following picture.

jbpm 5 tutorial human tasks jbpm5
Once you complete also this Task the process will reach the end state.

Please note that the life cycle explained above is the normal life cycle. The service also allows a lot of other life cycle methods. (See http://docs.jboss.org/jbpm/v5.2/userguide/ch.Human_Tasks.html#d0e3854) For example  the user can also indicate this using a fault response (possibly with fault data associated), in which case the status is changed to "Failed".