第一步:
在pom.xml中添加依赖
<!-- 添加Activiti 依赖 begin-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.21.0</version>
</dependency>
<!-- 添加Activiti 依赖 end-->
第二步:
建立流程bpmn文件。
例子如下:(注:流程启动的时候key需与process id相同)
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/testm1510735932336" id="m1510735932336" name="">
<process id="leave" isExecutable="true" isClosed="false" processType="None">
<startEvent id="_2" name="StartEvent"></startEvent>
<endEvent id="_3" name="EndEvent"></endEvent>
<userTask id="approve" name="经理审批" activiti:assignee="${approve}"></userTask>
<exclusiveGateway id="_5" name="ExclusiveGateway"></exclusiveGateway>
<sequenceFlow id="_6" sourceRef="approve" targetRef="_5"></sequenceFlow>
<sequenceFlow id="_7" name="通过" sourceRef="_5" targetRef="_3">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${pass}]]></conditionExpression>
</sequenceFlow>
<userTask id="application" name="提交申请" activiti:assignee="${apply}"></userTask>
<sequenceFlow id="_9" sourceRef="_2" targetRef="application"></sequenceFlow>
<sequenceFlow id="_10" sourceRef="application" targetRef="approve"></sequenceFlow>
<userTask id="modify" name="修改申请" activiti:assignee="${apply}"></userTask>
<sequenceFlow id="_12" name="不通过" sourceRef="_5" targetRef="modify">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${!pass}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="_13" sourceRef="modify" targetRef="approve"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_leave">
<bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
<bpmndi:BPMNShape bpmnElement="_2" id="BPMNShape__2">
<omgdc:Bounds height="35.0" width="35.0" x="15.0" y="60.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_3" id="BPMNShape__3">
<omgdc:Bounds height="35.0" width="35.0" x="630.0" y="63.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="approve" id="BPMNShape_approve">
<omgdc:Bounds height="55.0" width="85.0" x="315.0" y="50.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_5" id="BPMNShape__5">
<omgdc:Bounds height="40.0" width="40.0" x="505.0" y="60.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="application" id="BPMNShape_application">
<omgdc:Bounds height="55.0" width="85.0" x="135.0" y="50.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="modify" id="BPMNShape_modify">
<omgdc:Bounds height="55.0" width="85.0" x="315.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6">
<omgdi:waypoint x="400.0" y="77.0"></omgdi:waypoint>
<omgdi:waypoint x="505.0" y="80.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7">
<omgdi:waypoint x="545.0" y="80.0"></omgdi:waypoint>
<omgdi:waypoint x="630.0" y="80.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9">
<omgdi:waypoint x="50.0" y="77.0"></omgdi:waypoint>
<omgdi:waypoint x="135.0" y="77.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10">
<omgdi:waypoint x="220.0" y="77.0"></omgdi:waypoint>
<omgdi:waypoint x="315.0" y="77.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12">
<omgdi:waypoint x="525.0" y="100.0"></omgdi:waypoint>
<omgdi:waypoint x="525.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="400.0" y="177.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_13" id="BPMNEdge__13">
<omgdi:waypoint x="357.0" y="150.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="105.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
第三步:
在配置文件中添加配置。
#activiti
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=MYSQL
第四步:
启动流程,编写流程各个环节的处理。
示例如下:
public boolean startActivityDemo() {
System.out.println("method startActivityDemo begin....");
Map<String,Object> map = new HashMap<String,Object>();
map.put("apply","zhangsan");
map.put("approve","lisi");
//流程启动
ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave",map);
String processId = pi1.getId();
String taskId = pi1.getTasks().get(0).getId();
taskService.complete(taskId, map);//完成第一步申请
Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
String taskId2 = task.getId();
map.put("pass", false);
taskService.complete(taskId2, map);//驳回申请
System.out.println("method startActivityDemo end....");
return false;
}