版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
这是本人工作流引擎开发学习系列的第二篇。展示的是一个简单的工作流审批demo。
构建的Maven项目结构图:
DemoMain:
package com.imooc.activiti.helloworld;
import com.google.common.collect.Maps;
import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.form.DateFormType;
import org.activiti.engine.impl.form.StringFormType;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* @Author: EagleLin
* @Date: 2019/10/13 23:28
* @Version 1.0
* @Title:
* @Description:启动类
*/
public class DemoMain {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);
public static void main(String[] args) throws ParseException {
LOGGER.info("启动我们的程序");
//创建流程引擎
ProcessEngine processEngine = getProcessEngine();
//部署流程定义文件
ProcessDefinition processDefinition = getProcessDefinition(processEngine);
//启动运行流程
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
LOGGER.info("启动流程 {}", processInstance.getProcessDefinitionKey());
//处理流程任务
processTask(processEngine, processInstance);
LOGGER.info("结束我们的程序");
}
private static void processTask(ProcessEngine processEngine, ProcessInstance processInstance) throws ParseException {
Scanner scanner = new Scanner(System.in);
while (processInstance != null && !processInstance.isEnded()) {
TaskService taskService = processEngine.getTaskService();
List<Task> list = taskService.createTaskQuery().list();
LOGGER.info("待处理任务数量{}", list.size());
for (Task task : list) {
LOGGER.info("待处理任务{}", task.getName());
Map<String, Object> variables = getMap(processEngine, scanner, task);
taskService.complete(task.getId(), variables);
processInstance = processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
}
}
scanner.close();
}
private static Map<String, Object> getMap(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
FormService formService = processEngine.getFormService();
TaskFormData taskFormData = formService.getTaskFormData(task.getId());
List<FormProperty> formProperties = taskFormData.getFormProperties();
Map<String, Object> variables = Maps.newHashMap();
String line = null;
for (FormProperty property: formProperties) {
if(StringFormType.class.isInstance(property.getType())){
LOGGER.info("请输入 {} ?", property.getName());
line = scanner.nextLine();
variables.put(property.getId(), line);
}else if(DateFormType.class.isInstance(property.getType())){
LOGGER.info("请输入 {} ? 格式{yyyy-mm-dd}", property.getName());
line = scanner.nextLine();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = dateFormat.parse(line);
variables.put(property.getId(), date);
}else{
LOGGER.info("类型暂不支持 {} ", property.getType());
}
LOGGER.info("你输入的内容是 [{}]", line);
}
return variables;
}
private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
RepositoryService repositoryService = processEngine.getRepositoryService();
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
deploymentBuilder.addClasspathResource("second_approve.bpmn20.xml");
Deployment deployment = deploymentBuilder.deploy();
String deploymentId = deployment.getId();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
LOGGER.info("流程定义文件{},流程的ID {}", processDefinition.getName(), deploymentId);
return processDefinition;
}
private static ProcessEngine getProcessEngine() {
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
ProcessEngine processEngine = cfg.buildProcessEngine();
String name = processEngine.getName();
String version = ProcessEngine.VERSION;
LOGGER.info("流程引擎的名称{},版本{}", name, version);
return processEngine;
}
}
second_approve.bpmn20.xml:
<?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: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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="second_approve" name="二级审批流程" isExecutable="true">
<startEvent id="startEvent" name="开始"></startEvent>
<userTask id="submitForm" name="填写审批信息">
<extensionElements>
<activiti:formProperty id="message" name="申请信息" type="string" required="true"></activiti:formProperty>
<activiti:formProperty id="name" name="申请人姓名" type="string" required="true"></activiti:formProperty>
<activiti:formProperty id="submitTime" name="提交时间" type="date" datePattern="yyyy-mm-dd" required="true"></activiti:formProperty>
<activiti:formProperty id="submitType" name="确认申请" type="string" required="true"></activiti:formProperty>
</extensionElements>
</userTask>
<sequenceFlow id="flow1" sourceRef="startEvent" targetRef="submitForm"></sequenceFlow>
<userTask id="tl_approve" name="主管审批">
<extensionElements>
<activiti:formProperty id="tlApprove" name="主管审批结果" type="string"></activiti:formProperty>
<activiti:formProperty id="tlMessage" name="主管备注" type="string" required="true"></activiti:formProperty>
</extensionElements>
</userTask>
<exclusiveGateway id="decideTLApprove" name="主管审批校验"></exclusiveGateway>
<sequenceFlow id="flow4" sourceRef="tl_approve" targetRef="decideTLApprove"></sequenceFlow>
<userTask id="hr_approve" name="人事审批">
<extensionElements>
<activiti:formProperty id="hrApprove" name="人事审批结果" type="string" required="true"></activiti:formProperty>
<activiti:formProperty id="hrMessage" name="人事审批备注" type="string" required="true"></activiti:formProperty>
</extensionElements>
</userTask>
<sequenceFlow id="flow6" sourceRef="decideTLApprove" targetRef="hr_approve">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove == "y" || tlApprove == "Y"}]]></conditionExpression>
</sequenceFlow>
<exclusiveGateway id="decideHRApprove" name="人事审批校验"></exclusiveGateway>
<sequenceFlow id="flow7" sourceRef="hr_approve" targetRef="decideHRApprove"></sequenceFlow>
<endEvent id="endEvent" name="结束"></endEvent>
<sequenceFlow id="flow8" sourceRef="decideHRApprove" targetRef="endEvent">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove == "y" || hrApprove == "Y"}]]></conditionExpression>
</sequenceFlow>
<exclusiveGateway id="decideSubmit" name="提交OR取消"></exclusiveGateway>
<sequenceFlow id="flow9" sourceRef="submitForm" targetRef="decideSubmit"></sequenceFlow>
<sequenceFlow id="flow10" sourceRef="decideSubmit" targetRef="tl_approve">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType == "y" || submitType == "Y"}]]></conditionExpression>
</sequenceFlow>
<endEvent id="endEventCancel" name="取消"></endEvent>
<sequenceFlow id="flow11" sourceRef="decideSubmit" targetRef="endEventCancel">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType == "n" || submitType == "N"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow12" sourceRef="decideTLApprove" targetRef="submitForm">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove == "n" || tlApprove == "N"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow13" sourceRef="decideHRApprove" targetRef="submitForm">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove == "n" || hrApprove == "N"}]]></conditionExpression>
</sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_second_approve">
<bpmndi:BPMNPlane bpmnElement="second_approve" id="BPMNPlane_second_approve">
<bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
<omgdc:Bounds height="35.0" width="35.0" x="52.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="submitForm" id="BPMNShape_submitForm">
<omgdc:Bounds height="55.0" width="105.0" x="131.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="tl_approve" id="BPMNShape_tl_approve">
<omgdc:Bounds height="55.0" width="105.0" x="350.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideTLApprove" id="BPMNShape_decideTLApprove">
<omgdc:Bounds height="40.0" width="40.0" x="490.0" y="177.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="hr_approve" id="BPMNShape_hr_approve">
<omgdc:Bounds height="55.0" width="105.0" x="566.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideHRApprove" id="BPMNShape_decideHRApprove">
<omgdc:Bounds height="40.0" width="40.0" x="714.0" y="177.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent">
<omgdc:Bounds height="35.0" width="35.0" x="800.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decideSubmit" id="BPMNShape_decideSubmit">
<omgdc:Bounds height="40.0" width="40.0" x="280.0" y="177.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endEventCancel" id="BPMNShape_endEventCancel">
<omgdc:Bounds height="35.0" width="35.0" x="390.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="87.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="131.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="455.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="490.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="530.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="566.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="671.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="714.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="754.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="800.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
<omgdi:waypoint x="236.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="280.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
<omgdi:waypoint x="320.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="350.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
<omgdi:waypoint x="300.0" y="217.0"></omgdi:waypoint>
<omgdi:waypoint x="300.0" y="276.0"></omgdi:waypoint>
<omgdi:waypoint x="390.0" y="277.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12">
<omgdi:waypoint x="510.0" y="217.0"></omgdi:waypoint>
<omgdi:waypoint x="508.0" y="332.0"></omgdi:waypoint>
<omgdi:waypoint x="362.0" y="332.0"></omgdi:waypoint>
<omgdi:waypoint x="182.0" y="332.0"></omgdi:waypoint>
<omgdi:waypoint x="183.0" y="225.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13">
<omgdi:waypoint x="734.0" y="177.0"></omgdi:waypoint>
<omgdi:waypoint x="732.0" y="130.0"></omgdi:waypoint>
<omgdi:waypoint x="178.0" y="130.0"></omgdi:waypoint>
<omgdi:waypoint x="183.0" y="170.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc.activiti</groupId>
<artifactId>activiti6-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
</dependencies>
</project>
效果图: