0 Replies Latest reply on Sep 29, 2015 3:04 AM by srinu1985btech

    Boundary Error Event is not working to custom service task

    srinu1985btech

      Hi, I am trying to impliment Custom service task with Exception handling.

       

       

      Please find below BPMN code

       

       

      BMPN code

      <?xml version="1.0" encoding="UTF-8"?>

      <bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.jboss.org/drools" xmlns="http://www.jboss.org/drools" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd http://www.jboss.org/drools drools.xsd http://www.bpsim.org/schemas/1.0 bpsim.xsd" id="Definition" expressionLanguage="http://www.mvel.org/2.0" targetNamespace="http://www.jboss.org/drools" typeLanguage="http://www.java.com/javaTypes">

        <bpmn2:itemDefinition id="_String_1" isCollection="false" structureRef="org.jbpm.bpmn2.handler.WorkItemHandlerRuntimeException"/>

        <bpmn2:itemDefinition id="_String" isCollection="false" structureRef="String"/>

        <bpmn2:itemDefinition id="_Integer" isCollection="false" structureRef="Integer"/>

        <bpmn2:itemDefinition id="ItemDefinition_1" isCollection="false" structureRef="StringDataType"/>

        <bpmn2:itemDefinition id="ItemDefinition_2" isCollection="false" structureRef="java.lang.RuntimeException"/>

        <bpmn2:error id="Error_2" errorCode="Error_2" name="Error 2" structureRef="_String_1"/>

        <bpmn2:error id="RuntimeException_error" errorCode="RuntimeException_error" name="RuntimeException_error" structureRef="ItemDefinition_2"/>

        <bpmn2:process id="com.sample.bpmn" tns:version="1" tns:packageName="defaultPackage" tns:adHoc="false" name="ExceptionHandling" isExecutable="true" processType="Private">

          <bpmn2:extensionElements>

            <tns:import name="org.jbpm.bpmn2.handler.WorkItemHandlerRuntimeException"/>

            <tns:import name="org.jbpm.bpmn2.handler.ServiceTaskHandler"/>

            <tns:import name="java.lang.RuntimeException"/>

          </bpmn2:extensionElements>

          <bpmn2:property id="ErrorDefination" itemSubjectRef="_String_1" name="ErrorDefination"/>

          <bpmn2:property id="firstval" itemSubjectRef="_String" name="firstval"/>

          <bpmn2:property id="secondval" itemSubjectRef="_String" name="secondval"/>

          <bpmn2:property id="expectiontype" itemSubjectRef="_String" name="expectiontype"/>

          <bpmn2:property id="runtimeException" itemSubjectRef="ItemDefinition_2" name="runtimeException"/>

          <bpmn2:startEvent id="_1" name="">

            <bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>

          </bpmn2:startEvent>

          <bpmn2:endEvent id="EndEvent_1" name="">

            <bpmn2:incoming>SequenceFlow_4</bpmn2:incoming>

          </bpmn2:endEvent>

          <bpmn2:endEvent id="EndEvent_3" name="">

            <bpmn2:incoming>SequenceFlow_6</bpmn2:incoming>

          </bpmn2:endEvent>

          <bpmn2:scriptTask id="ScriptTask_2" name="Success" scriptFormat="http://www.java.com/java">

            <bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>

            <bpmn2:outgoing>SequenceFlow_4</bpmn2:outgoing>

            <bpmn2:script>System.out.println(&quot;we are in success script   :&quot;);&#xD;

      System.out.println(&quot;firstvalue   :&quot; +kcontext.getVariable(&quot;firstval&quot;));&#xD;

      System.out.println(&quot;secondvalue   :&quot; +kcontext.getVariable(&quot;secondval&quot;));</bpmn2:script>

          </bpmn2:scriptTask>

          <bpmn2:scriptTask id="ScriptTask_1" name="Error" scriptFormat="http://www.java.com/java">

            <bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>

            <bpmn2:outgoing>SequenceFlow_6</bpmn2:outgoing>

            <bpmn2:script>System.out.println(&quot;we are in Error Script task::&quot;);&#xD;

      System.out.println(&quot;firstvalue   :&quot; +kcontext.getVariable(&quot;firstval&quot;));&#xD;

      System.out.println(&quot;secondvalue   :&quot; +kcontext.getVariable(&quot;secondval&quot;));&#xD;

      System.out.println(&quot;Expection Type   :&quot; +kcontext.getVariable(&quot;expectiontype&quot;));&#xD;

      </bpmn2:script>

          </bpmn2:scriptTask>

          <bpmn2:sequenceFlow id="SequenceFlow_6" tns:priority="1" name="" sourceRef="ScriptTask_1" targetRef="EndEvent_3"/>

          <bpmn2:sequenceFlow id="SequenceFlow_4" tns:priority="1" name="" sourceRef="ScriptTask_2" targetRef="EndEvent_1"/>

          <bpmn2:task id="Task_1" tns:taskName="ServiceTask" tns:displayName="ServiceTask" tns:icon="setting.png" name="ServiceTask">

            <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>

            <bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>

            <bpmn2:ioSpecification id="_InputOutputSpecification_2">

              <bpmn2:dataInput id="_DataInput_2" itemSubjectRef="_String_1" name="ErrorDefination"/>

              <bpmn2:dataInput id="_DataInput_3" itemSubjectRef="_String" name="firstval"/>

              <bpmn2:dataInput id="_DataInput_4" itemSubjectRef="_String" name="secondval"/>

              <bpmn2:dataInput id="_DataInput_5" itemSubjectRef="_String" name="expectiontype"/>

              <bpmn2:inputSet id="_InputSet_2" name="Input Set 2">

                <bpmn2:dataInputRefs>_DataInput_2</bpmn2:dataInputRefs>

                <bpmn2:dataInputRefs>_DataInput_3</bpmn2:dataInputRefs>

                <bpmn2:dataInputRefs>_DataInput_4</bpmn2:dataInputRefs>

                <bpmn2:dataInputRefs>_DataInput_5</bpmn2:dataInputRefs>

              </bpmn2:inputSet>

              <bpmn2:outputSet id="OutputSet_1" name="Output Set 1"/>

            </bpmn2:ioSpecification>

            <bpmn2:dataInputAssociation id="_DataInputAssociation_2">

              <bpmn2:sourceRef>ErrorDefination</bpmn2:sourceRef>

              <bpmn2:targetRef>_DataInput_2</bpmn2:targetRef>

            </bpmn2:dataInputAssociation>

            <bpmn2:dataInputAssociation id="_DataInputAssociation_3">

              <bpmn2:sourceRef>firstval</bpmn2:sourceRef>

              <bpmn2:targetRef>_DataInput_3</bpmn2:targetRef>

            </bpmn2:dataInputAssociation>

            <bpmn2:dataInputAssociation id="_DataInputAssociation_4">

              <bpmn2:sourceRef>secondval</bpmn2:sourceRef>

              <bpmn2:targetRef>_DataInput_4</bpmn2:targetRef>

            </bpmn2:dataInputAssociation>

            <bpmn2:dataInputAssociation id="_DataInputAssociation_5">

              <bpmn2:sourceRef>expectiontype</bpmn2:sourceRef>

              <bpmn2:targetRef>_DataInput_5</bpmn2:targetRef>

            </bpmn2:dataInputAssociation>

          </bpmn2:task>

          <bpmn2:sequenceFlow id="SequenceFlow_1" tns:priority="1" name="" sourceRef="_1" targetRef="Task_1"/>

          <bpmn2:sequenceFlow id="SequenceFlow_2" tns:priority="1" name="" sourceRef="Task_1" targetRef="ScriptTask_2"/>

          <bpmn2:boundaryEvent id="BoundaryEvent_1" name="" attachedToRef="Task_1">

            <bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>

            <bpmn2:dataOutput id="DataOutput_3" itemSubjectRef="_String" name="event"/>

            <bpmn2:dataOutputAssociation id="DataOutputAssociation_2">

              <bpmn2:sourceRef>DataOutput_3</bpmn2:sourceRef>

              <bpmn2:targetRef>expectiontype</bpmn2:targetRef>

            </bpmn2:dataOutputAssociation>

            <bpmn2:outputSet id="OutputSet_2" name="Output Set 2">

              <bpmn2:dataOutputRefs>DataOutput_3</bpmn2:dataOutputRefs>

            </bpmn2:outputSet>

            <bpmn2:errorEventDefinition id="ErrorEventDefinition_2" errorRef="Error_2"/>

          </bpmn2:boundaryEvent>

          <bpmn2:sequenceFlow id="SequenceFlow_3" tns:priority="1" name="" sourceRef="BoundaryEvent_1" targetRef="ScriptTask_1"/>

        </bpmn2:process>

        <bpmndi:BPMNDiagram id="BPMNDiagram_1" name="ExceptionHandling">

          <bpmndi:BPMNPlane id="BPMNPlane_Process_1" bpmnElement="com.sample.bpmn">

            <bpmndi:BPMNShape id="BPMNShape_StartEvent_1" bpmnElement="_1">

              <dc:Bounds height="36.0" width="36.0" x="45.0" y="127.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape id="BPMNShape_EndEvent_1" bpmnElement="EndEvent_1">

              <dc:Bounds height="36.0" width="36.0" x="510.0" y="128.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape id="BPMNShape_EndEvent_3" bpmnElement="EndEvent_3">

              <dc:Bounds height="36.0" width="36.0" x="510.0" y="58.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape id="BPMNShape_ScriptTask_2" bpmnElement="ScriptTask_2">

              <dc:Bounds height="50.0" width="110.0" x="292.0" y="121.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape id="BPMNShape_ScriptTask_1" bpmnElement="ScriptTask_1">

              <dc:Bounds height="50.0" width="110.0" x="292.0" y="52.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape id="BPMNShape_Task_1" bpmnElement="Task_1">

              <dc:Bounds height="50.0" width="110.0" x="120.0" y="120.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape id="BPMNShape_BoundaryEvent_1" bpmnElement="BoundaryEvent_1">

              <dc:Bounds height="36.0" width="36.0" x="152.0" y="102.0"/>

            </bpmndi:BPMNShape>

            <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_6" bpmnElement="SequenceFlow_6" sourceElement="BPMNShape_ScriptTask_1" targetElement="BPMNShape_EndEvent_3">

              <di:waypoint xsi:type="dc:Point" x="402.0" y="77.0"/>

              <di:waypoint xsi:type="dc:Point" x="450.0" y="77.0"/>

              <di:waypoint xsi:type="dc:Point" x="450.0" y="76.0"/>

              <di:waypoint xsi:type="dc:Point" x="510.0" y="76.0"/>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_4" bpmnElement="SequenceFlow_4" sourceElement="BPMNShape_ScriptTask_2" targetElement="BPMNShape_EndEvent_1">

              <di:waypoint xsi:type="dc:Point" x="402.0" y="146.0"/>

              <di:waypoint xsi:type="dc:Point" x="450.0" y="146.0"/>

              <di:waypoint xsi:type="dc:Point" x="450.0" y="146.0"/>

              <di:waypoint xsi:type="dc:Point" x="510.0" y="146.0"/>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_1" bpmnElement="SequenceFlow_1" sourceElement="BPMNShape_StartEvent_1" targetElement="BPMNShape_Task_1">

              <di:waypoint xsi:type="dc:Point" x="81.0" y="145.0"/>

              <di:waypoint xsi:type="dc:Point" x="98.0" y="145.0"/>

              <di:waypoint xsi:type="dc:Point" x="98.0" y="145.0"/>

              <di:waypoint xsi:type="dc:Point" x="120.0" y="145.0"/>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_2" bpmnElement="SequenceFlow_2" sourceElement="BPMNShape_Task_1" targetElement="BPMNShape_ScriptTask_2">

              <di:waypoint xsi:type="dc:Point" x="230.0" y="145.0"/>

              <di:waypoint xsi:type="dc:Point" x="257.0" y="145.0"/>

              <di:waypoint xsi:type="dc:Point" x="257.0" y="146.0"/>

              <di:waypoint xsi:type="dc:Point" x="292.0" y="146.0"/>

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_3" bpmnElement="SequenceFlow_3" sourceElement="BPMNShape_BoundaryEvent_1" targetElement="BPMNShape_ScriptTask_1">

              <di:waypoint xsi:type="dc:Point" x="170.0" y="102.0"/>

              <di:waypoint xsi:type="dc:Point" x="170.0" y="77.0"/>

              <di:waypoint xsi:type="dc:Point" x="292.0" y="77.0"/>

            </bpmndi:BPMNEdge>

          </bpmndi:BPMNPlane>

        </bpmndi:BPMNDiagram>

      </bpmn2:definitions>

       

       

      diagram like attachment.

       

       

      I am able to implement Custom service task every thing working fine. same time i am throwing exception from service task. this error i am handling with Boundary error Event handler. But when exception rises it will go in both the direction like Error flow and success flow.

       

       

      Please fine below java code

       

       

      import java.lang.reflect.InvocationTargetException;

      import java.util.Map;

       

       

      import org.jbpm.bpmn2.handler.WorkItemHandlerRuntimeException;

      import org.jbpm.workflow.instance.WorkflowProcessInstance;

      import org.jbpm.workflow.instance.WorkflowRuntimeException;

      import org.kie.api.runtime.KieSession;

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

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

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

       

       

       

       

      public class ServiceTask implements WorkItemHandler{

       

          public KieSession ksession = null;

       

        public ServiceTask(){}

        public ServiceTask(KieSession ksession)

        {

        this.ksession = ksession;

        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {

        manager.abortWorkItem(workItem.getId());

              }

       

       

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager)// throws WorkItemHandlerRuntimeException

        {

        System.out.println("::::::::   ServiceTask :::::::::::");

        WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(workItem.getProcessInstanceId());

         int result=0;

        System.out.println(workItem.getParameter("firstval")+" and  "+workItem.getParameter("secondval"));

             try

             {

         

          int a = Integer.parseInt((String) workItem.getParameter("firstval"));

          int b = Integer.parseInt((String) workItem.getParameter("secondval"));

               result=a/b;

               System.out.println("The result is"+result);

               manager.completeWorkItem(workItem.getId(), null);

             }

             catch(Exception ex)

             {    

                 System.out.println("Chained exception thrown manually : "+ex);         

                 processInstance.setVariable("expectiontype", ex);

                 //throw new RuntimeException(ex);

                 throw new WorkItemHandlerRuntimeException(ex);     

             }  

         }

      }

       

       

      Can any on ple suggest me how to resolve this problem in Boundary Error Event handling.