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:
Download here the code from this example

0
0
0
s2smodern

Related articles available on mastertheboss.com

How do I fire an action every time a JBPM Node enters?

JBoss recipe of the day

JBoss Jbpm superstates

What is a superstate ? a Superstate is a group of nodes. Supersta

Jbpm Mail delivery

Almost every workflow needs a notification of the process activit

How do you embed Java code in your JPDL ?

  You can use a BeanShell expression to add a Java script in you

JBPM best practices

There's no perfect rule to model your workflow, it depends on the

How to delete/persist your JBPM process after restart ?

JBoss recipe of the day