Skip to content
This repository was archived by the owner on May 25, 2020. It is now read-only.

Creating Service Workflows

Constantin Jucovschi edited this page Oct 18, 2013 · 2 revisions

Creating Service Workflows

To create a new Workflow, open Drools-Guvnor to the Knowledge Base Tab and click on Create New → New BPMN2 Process. You need to specify a name for the process e.g. “asm editor” service (avoid spaces) and select the package Sally. Your workflow ID will be the contatenation of the package name and given workflow name i.e. “Sally.asm editor”. This is the ID that needs to be given everytime you want to instantiate new workflow. I would recommend reading a book on BPMN before starting doing your own workflows. It helps to understand some best practices as well as under- stand in which certain BPMN constructs should be used. Andrea has the book “Praxishandbuch BPMN 2.0” that I found very useful. http://www.bpmn.org seems to have some nice books on BPMN.

Creating Events

Most of events you need are “Intermediate Events” i.e. events that are triggered at some point at the middle of the workflow. There are several types of Inter- mediate Events. For messages like SallyFrame from the previous section, you need “Message Intermedia Event” (first in the list of jBPM). For messages like “Switch App” you need “Signal Intermediate Event” (6th on the jBPM list). Each event has an ID that is used to identify it. In jBPM it is called “MessageRef”. You can edit/set this ID in the properties view (on the right) under the group “Extra”.

Creating Tasks

Just like events have IDs, tasks are identified by their “Task Name”. Figure 3 gives an example of a Java implementation for executing a task with name “CreateDoc”.

@SallyTask(action="CreateDoc")
public class CreateDoc implements WorkItemHandler {
  ISallyKnowledgeBase kb;

  @Inject
  public CreateDoc(ISallyKnowledgeBase kb) {
    this.kb = kb;
  }

  @Override
  public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
    manager.completeWorkItem(workItem.getId(), null);
  }

  @Override
  public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    WhoAmI alexInfo = HandlerUtils.getFirstTypedParameter(workItem.getParameters(), WhoAmI.class);
    
    Map<String, Object> variable = HandlerUtils.getProcessVariables(kb.getProcessInstance(workItem.getProcessInstanceId()));
    INetworkSender networkSender = HandlerUtils.safeGet(variable, "NetworkSender", INetworkSender.class);

    try{

      workItem.getResults().put("result", "42");
    } catch (Exception e) {
      log.error(e.getMessage());
    } finally {
      manager.completeWorkItem(workItem.getId(), workItem.getResults());
    }
  }
}

Figure 3: Sample Task Implementation Each task should provide an implementation in it’s own class. This class is annotated with “@SallyTask” a have attribute “action” match the task name. The class should also implement the org.drools.process.instance.WorkItemHandler interface. Unfortunately there are two interfaces called WorkItemHandler pro- vided by jBPMN. You need to make sure you use the one above. Tasks get their inputs from three sources:

  • Data Objects (grey boxes like clickEvt) connected to the current task.
  • DataInputSet and Assignments properties of the task.
  • Workflow global variables which can be edited/updated in the Variable Definition property of the BPMN workflow.