jBPM:WorkItemHandlers

One of the important feature in jBPM is work item handlers. Using work item handlers we can create the custom service task which accepts certain parameter as argument and can have the output after some processing.There are many things which are achieved in jBPM using work item handlers only.Few of the things are making rest/soap calls,Email Task and the bit of complex example will be to have an work item handlers for saving JPA entity.

Let's begin with the steps for creating work item handler.

WorkItemDefinition

First we need to define the work item handler.The definition of work item handler contains the name of it, parameter which can be passed in to the work item handler,display name of the handler,icon of the handler and the category to which it will belong.Simple definition of work item handler will look like below.

  [
    "name" : "Email",
    "parameters" : [
      "From" : new StringDataType(),
      "To" : new StringDataType(),
      "Subject" : new StringDataType(),
      "Body" : new StringDataType()
    ],
    "displayName" : "Email",
    "icon" : "defaultemailicon.gif"
  ]

Note : Please keep in the mind the the name which we are using over here will be used in reference in future. for mapping between this definition and the implementation of work item handler.

One we have defined our work item handler in work item definition file we can see that handler in the BPMN designer as shown in below screenshot.




Implementation of WorkItemHandler 

Once the handler has has been defined inside the work item definition we need to write the implementation of work item handler.For implementation we need to implement the existing interface in our new java class.Name of interface is WorkItemHandler. Let's create one WorkItemHandler which will accept 2 numeric argument and we will display sum of it(Just making it simpler).

 
import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.runtime.process.WorkItemManager;

public class RestWorkItemHandler implements WorkItemHandler{

 @Override
 public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
  Integer number1=Integer.parseInt(workItem.getParameter("number1").toString());
  Integer number2=Integer.parseInt(workItem.getParameter("number2").toString());
  System.out.println(number1+number2);
 }

 @Override
 public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
 }
 
}



Mapping between work item definition and workitem handler.
Final step is to create mapping between work item handler and the workitem definition.This mapping we are creating inside kie-deployment-descriptor.Below is the code which we need to add inside above mentioned file.Below configuration can also be done using UI of kie-workbench.

The name which we have defined inside the work item handler will be used in below for mapping purpose.
 
 <work-item-handlers>
        <work-item-handler>
            <resolver>mvel</resolver>
            <identifier>new com.myspace.employeeevaluation.wih.RestWorkItemHandler()</identifier>
            <parameters/>
            <name>Rest</name>
        </work-item-handler>
 </work-item-handlers>







Share this:

CONVERSATION

0 comments:

Post a Comment