Advertise with us

Search Mastertheboss

Our eBooks

jBPM 5 on JBoss AS 7

Coding the Task client

Now we will code a simple Servlet which will be used to start a sample process containing two HumanTasks.

In this simple bpmn diagram two user Tasks are included: Task 1 which is in charge to the user “frank” and Task 2 which is in charge to the user “mary”.

jbpm 5 jboss 7

Here is the Servlet which connects to the TaskClient and starts the process:


package com.mastertheboss.jbpm5;

import java.util.Properties;

import javax.naming.InitialContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.TransactionManager;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.base.MapGlobalResolver;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.impl.EnvironmentFactory;
import org.drools.persistence.jpa.JPAKnowledgeService;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.KnowledgeSessionConfiguration;
import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.process.audit.JPAProcessInstanceDbLog;
import org.jbpm.process.audit.JPAWorkingMemoryDbLogger;
import org.jbpm.process.workitem.wsht.WSHumanTaskHandler;

public class StartProcess extends HttpServlet {
   private static final long serialVersionUID = 1L;
   StatefulKnowledgeSession ksession = null;
   KnowledgeBase kbase = null;
   public StartProcess() {
       // TODO Auto-generated constructor stub

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       try {
        * Reads the KnowledgeBase
           kbase = readKnowledgeBase();
       } catch (Exception e) {
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
        * Creates the knowledge session that uses JPA to persists runtime state
       StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
        * Registers the Human Task Service in the jBPM engine
       ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new WSHumanTaskHandler());
       System.out.println("Process started successfully...");
    private static StatefulKnowledgeSession createKnowledgeSession(KnowledgeBase kbase) {

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

           Environment env = KnowledgeBaseFactory.newEnvironment();

           env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );
           env.set( EnvironmentName.TRANSACTION_MANAGER, getTransactionManager() );
           env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );

           Properties properties = new Properties();
           properties.put("drools.processInstanceManagerFactory", "org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory");
           properties.put("drools.processSignalManagerFactory", "org.jbpm.persistence.processinstance.JPASignalManagerFactory");

           KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);

           return JPAKnowledgeService.newStatefulKnowledgeSession(kbase, config, env);


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

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
    public static TransactionManager getTransactionManager()   {      
        TransactionManager tm  = null;
        try {
        InitialContext ic = new InitialContext();      
        tm  = (TransactionManager) ic.lookup("java:jboss/TransactionManager");
        if (tm == null) throw new IllegalStateException("Couldn't find the transaction mamager in JNDI");      
        }catch (Exception exc) {
        return tm;  

As you can see this sample Servlet creates a new KnowledgeBase adding to the classpath the bpmn diagram.

Next the KnowledgeSession needs to be created. This in turn uses JPA to persists the process's runtime state.
The persistence unit which is associated with this example is org.jbpm.persistence.jpa
Please notice that in order to create the StatefulKnowledgeSession some properties needs to be set in the jBPM environment, such as the TransactionManager which is, in our case, the AS 7 TransactionManager bound to the JNDI name java:jboss/TransactionManager
Finally, the human task service needs to be integrated with the jBPM engine by registering a work item handler that is responsible for translating the human task to a specific invocation of a service.


Setting up application libraries


You need a jBPM 5 runtime available (this can be obtained using the following ant command on the jbpm full installer distribution)

C:\jbpm-installer>ant install.jBPM.runtime
Buildfile: build.xml

     [echo] Checking jBPM binaries download ...


    [mkdir] Created dir: C:\jbpm-installer\runtime
    [unzip] Expanding: C:\jbpm-installer\lib\ into C:\jbpm-installer\runtime


Now link your application with jBPM libraries which are found in the "lib" folder of the jBPM runtime and its dependencies (which can be located in the lib subfolder).

09/12/2011  10.55            17.904 jbpm-bam-5.2.0.Final.jar
09/12/2011  10.55           176.271 jbpm-bpmn2-5.2.0.Final.jar
09/12/2011  10.55           324.927 jbpm-flow-5.2.0.Final.jar
09/12/2011  10.55           135.693 jbpm-flow-builder-5.2.0.Final.jar
09/12/2011  10.55           276.296 jbpm-human-task-5.2.0.Final.jar
09/12/2011  10.55            20.434 jbpm-persistence-jpa-5.2.0.Final.jar
09/12/2011  10.55            22.012 jbpm-test-5.2.0.Final.jar
09/12/2011  10.55            44.324 jbpm-workitems-5.2.0.Final.jar
22/01/2012  12.25    <DIR>          lib


Any feedback, suggestion is highly welcome !

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.