用户任务(User Task)
流程图
代码块
java代码:
package com.sample;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler;
import org.jbpm.test.JbpmJUnitBaseTestCase;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.api.task.TaskService;
import org.kie.api.task.model.TaskSummary;
/**
* This is a sample file to test a process.
*/
public class ProcessTest extends JbpmJUnitBaseTestCase {
@Test
public void testProcess() {
RuntimeManager manager = createRuntimeManager("Test01.bpmn");
RuntimeEngine engine = getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
TaskService taskService = engine.getTaskService();
//参数
Map<String, Object>map=new HashMap<String, Object>();
map.put("recipient", "小样");
ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn",map);
// let john execute Task 1
List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
TaskSummary task = list.get(0);
taskService.start(task.getId(), "john");
taskService.complete(task.getId(), "john", null);
// let mary execute Task 2
list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
task = list.get(0);
taskService.start(task.getId(), "mary");
taskService.complete(task.getId(), "mary", null);
// check whether the process instance has completed successfully
assertProcessInstanceCompleted(processInstance.getId(), ksession);
assertNodeTriggered(processInstance.getId(), "ddd");
manager.disposeRuntimeEngine(engine);
manager.close();
}
public ProcessTest(){
super(true,true);
}
}
流程图代码
<?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="ItemDefinition_1" structureRef="String"/>
<bpmn2:itemDefinition id="ItemDefinition_2" structureRef="Integer"/>
<bpmn2:itemDefinition id="ItemDefinition_3" structureRef="Boolean"/>
<bpmn2:process id="com.sample.bpmn" tns:version="1" tns:packageName="defaultPackage" tns:adHoc="false" name="Sample Process" isExecutable="true" processType="Private">
<bpmn2:startEvent id="_1" name="">
<bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:scriptTask id="ScriptTask_1" name="ddd" scriptFormat="http://www.java.com/java">
<bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
<bpmn2:script>long id = kcontext.getProcessInstance().getId();
String name = (String)kcontext.getVariable("recipient");
String log = "process " + id + " started by " + name;
System.out.println(log);</bpmn2:script>
</bpmn2:scriptTask>
<bpmn2:sequenceFlow id="SequenceFlow_3" tns:priority="1" name="" sourceRef="_1" targetRef="ScriptTask_1"/>
<bpmn2:userTask id="UserTask_1" name="john">
<bpmn2:documentation id="Documentation_15">StartApproval by PM</bpmn2:documentation>
<bpmn2:extensionElements>
<tns:onExit-script scriptFormat="http://www.java.com/java">
<tns:script>long id = kcontext.getProcessInstance().getId();
String log = "john approved process " + id;
System.out.println(log);</tns:script>
</tns:onExit-script>
</bpmn2:extensionElements>
<bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
<bpmn2:ioSpecification id="_InputOutputSpecification_3">
<bpmn2:dataInput id="_DataInput_9" itemSubjectRef="ItemDefinition_1" name="TaskName"/>
<bpmn2:dataInput id="_DataInput_10" itemSubjectRef="ItemDefinition_2" name="Priority"/>
<bpmn2:dataInput id="_DataInput_11" itemSubjectRef="ItemDefinition_1" name="Comment"/>
<bpmn2:dataInput id="_DataInput_12" itemSubjectRef="ItemDefinition_1" name="GroupId"/>
<bpmn2:dataInput id="_DataInput_13" itemSubjectRef="ItemDefinition_3" name="Skippable"/>
<bpmn2:dataInput id="_DataInput_14" itemSubjectRef="ItemDefinition_1" name="Content"/>
<bpmn2:dataInput id="_DataInput_15" itemSubjectRef="ItemDefinition_1" name="Locale"/>
<bpmn2:inputSet id="_InputSet_3" name="New Input Set">
<bpmn2:dataInputRefs>_DataInput_9</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_10</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_11</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_12</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_13</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_14</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_15</bpmn2:dataInputRefs>
</bpmn2:inputSet>
<bpmn2:outputSet id="_OutputSet_3" name="Output Set"/>
</bpmn2:ioSpecification>
<bpmn2:dataInputAssociation id="_DataInputAssociation_9">
<bpmn2:targetRef>_DataInput_9</bpmn2:targetRef>
<bpmn2:assignment id="Assignment_106">
<bpmn2:from xsi:type="bpmn2:tFormalExpression" id="FormalExpression_214">111</bpmn2:from>
<bpmn2:to xsi:type="bpmn2:tFormalExpression" id="FormalExpression_213">_DataInput_9</bpmn2:to>
</bpmn2:assignment>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_10">
<bpmn2:targetRef>_DataInput_10</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_11">
<bpmn2:targetRef>_DataInput_11</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_12">
<bpmn2:targetRef>_DataInput_12</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_13">
<bpmn2:targetRef>_DataInput_13</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_14">
<bpmn2:targetRef>_DataInput_14</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_15">
<bpmn2:targetRef>_DataInput_15</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:potentialOwner id="PotentialOwner_1">
<bpmn2:resourceAssignmentExpression id="ResourceAssignmentExpression_1">
<bpmn2:formalExpression id="FormalExpression_29" language="http://www.mvel.org/2.0">john</bpmn2:formalExpression>
</bpmn2:resourceAssignmentExpression>
</bpmn2:potentialOwner>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_1" tns:priority="1" sourceRef="ScriptTask_1" targetRef="UserTask_1"/>
<bpmn2:userTask id="UserTask_2" name="mary">
<bpmn2:documentation id="Documentation_44">StartApproval by AM</bpmn2:documentation>
<bpmn2:extensionElements>
<tns:onExit-script scriptFormat="http://www.java.com/java">
<tns:script>long id = kcontext.getProcessInstance().getId();
String log = "mary approved process " + id;
System.out.println(log);</tns:script>
</tns:onExit-script>
</bpmn2:extensionElements>
<bpmn2:incoming>SequenceFlow_2</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_5</bpmn2:outgoing>
<bpmn2:ioSpecification id="_InputOutputSpecification_4">
<bpmn2:dataInput id="_DataInput_16" itemSubjectRef="ItemDefinition_1" name="TaskName"/>
<bpmn2:dataInput id="_DataInput_17" itemSubjectRef="ItemDefinition_2" name="Priority"/>
<bpmn2:dataInput id="_DataInput_18" itemSubjectRef="ItemDefinition_1" name="Comment"/>
<bpmn2:dataInput id="_DataInput_19" itemSubjectRef="ItemDefinition_1" name="GroupId"/>
<bpmn2:dataInput id="_DataInput_20" itemSubjectRef="ItemDefinition_3" name="Skippable"/>
<bpmn2:dataInput id="_DataInput_21" itemSubjectRef="ItemDefinition_1" name="Content"/>
<bpmn2:dataInput id="_DataInput_22" itemSubjectRef="ItemDefinition_1" name="Locale"/>
<bpmn2:inputSet id="_InputSet_4" name="New Input Set">
<bpmn2:dataInputRefs>_DataInput_16</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_17</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_18</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_19</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_20</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_21</bpmn2:dataInputRefs>
<bpmn2:dataInputRefs>_DataInput_22</bpmn2:dataInputRefs>
</bpmn2:inputSet>
<bpmn2:outputSet id="_OutputSet_4" name="Output Set"/>
</bpmn2:ioSpecification>
<bpmn2:dataInputAssociation id="_DataInputAssociation_16">
<bpmn2:targetRef>_DataInput_16</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_17">
<bpmn2:targetRef>_DataInput_17</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_18">
<bpmn2:targetRef>_DataInput_18</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_19">
<bpmn2:targetRef>_DataInput_19</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_20">
<bpmn2:targetRef>_DataInput_20</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_21">
<bpmn2:targetRef>_DataInput_21</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:dataInputAssociation id="_DataInputAssociation_22">
<bpmn2:targetRef>_DataInput_22</bpmn2:targetRef>
</bpmn2:dataInputAssociation>
<bpmn2:potentialOwner id="PotentialOwner_2">
<bpmn2:resourceAssignmentExpression id="ResourceAssignmentExpression_2">
<bpmn2:formalExpression id="FormalExpression_58">mary</bpmn2:formalExpression>
</bpmn2:resourceAssignmentExpression>
</bpmn2:potentialOwner>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_2" tns:priority="1" sourceRef="UserTask_1" targetRef="UserTask_2"/>
<bpmn2:scriptTask id="ScriptTask_2" name="Script Task 2" scriptFormat="http://www.java.com/java">
<bpmn2:incoming>SequenceFlow_5</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_6</bpmn2:outgoing>
<bpmn2:script>long id = kcontext.getProcessInstance().getId();

String log = "process " + id + " finished";
System.out.println(log);</bpmn2:script>
</bpmn2:scriptTask>
<bpmn2:sequenceFlow id="SequenceFlow_5" tns:priority="1" sourceRef="UserTask_2" targetRef="ScriptTask_2"/>
<bpmn2:endEvent id="EndEvent_2" name="">
<bpmn2:incoming>SequenceFlow_6</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="SequenceFlow_6" tns:priority="1" sourceRef="ScriptTask_2" targetRef="EndEvent_2"/>
</bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<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="52.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_ScriptTask_1" bpmnElement="ScriptTask_1">
<dc:Bounds height="50.0" width="110.0" x="155.0" y="45.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_UserTask_1" bpmnElement="UserTask_1">
<dc:Bounds height="50.0" width="110.0" x="360.0" y="45.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_UserTask_2" bpmnElement="UserTask_2">
<dc:Bounds height="50.0" width="110.0" x="540.0" y="46.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_ScriptTask_2" bpmnElement="ScriptTask_2">
<dc:Bounds height="50.0" width="110.0" x="680.0" y="45.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_EndEvent_2" bpmnElement="EndEvent_2">
<dc:Bounds height="36.0" width="36.0" x="812.0" y="52.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_3" bpmnElement="SequenceFlow_3" sourceElement="BPMNShape_StartEvent_1" targetElement="BPMNShape_ScriptTask_1">
<di:waypoint xsi:type="dc:Point" x="81.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="114.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="114.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="155.0" y="70.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_1" bpmnElement="SequenceFlow_1" sourceElement="BPMNShape_ScriptTask_1" targetElement="BPMNShape_UserTask_1">
<di:waypoint xsi:type="dc:Point" x="265.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="307.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="307.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="360.0" y="70.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_2" bpmnElement="SequenceFlow_2" sourceElement="BPMNShape_UserTask_1" targetElement="BPMNShape_UserTask_2">
<di:waypoint xsi:type="dc:Point" x="470.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="540.0" y="71.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_5" bpmnElement="SequenceFlow_5" sourceElement="BPMNShape_UserTask_2" targetElement="BPMNShape_ScriptTask_2">
<di:waypoint xsi:type="dc:Point" x="650.0" y="71.0"/>
<di:waypoint xsi:type="dc:Point" x="663.0" y="71.0"/>
<di:waypoint xsi:type="dc:Point" x="663.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="680.0" y="70.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_6" bpmnElement="SequenceFlow_6" sourceElement="BPMNShape_ScriptTask_2" targetElement="BPMNShape_EndEvent_2">
<di:waypoint xsi:type="dc:Point" x="790.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="799.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="799.0" y="70.0"/>
<di:waypoint xsi:type="dc:Point" x="812.0" y="70.0"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>
注意
- 注册用户任务 ksession.getWorkItemManager().registerWorkItemHandler(“Human Task”, new SystemOutWorkItemHandler());
public ProcessTest(){
super(true,true);
}特别注意以上两点,否则会报错Unknown entity: org.jbpm.services.task.audit.TaskEventImpl
备注
欢迎各位指导,qq:2696663734;