工作流学习之入门demo
Posted java-le
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工作流学习之入门demo相关的知识,希望对你有一定的参考价值。
1 /** 2 * Copyright (C), 2015-2018, XXX有限公司 3 * FileName: DemoMain 4 * Author: happy 5 * Date: 2018/6/23 16:33 6 * Description: 7 * History: 8 * <author> <time> <version> <desc> 9 * 作者姓名 修改时间 版本号 描述 10 */ 11 package com.imooc.activiti.helloworld; 12 13 import com.google.common.collect.Maps; 14 import org.activiti.engine.*; 15 import org.activiti.engine.form.FormProperty; 16 import org.activiti.engine.form.TaskFormData; 17 import org.activiti.engine.impl.form.DateFormType; 18 import org.activiti.engine.impl.form.StringFormType; 19 import org.activiti.engine.repository.Deployment; 20 import org.activiti.engine.repository.DeploymentBuilder; 21 import org.activiti.engine.repository.ProcessDefinition; 22 import org.activiti.engine.runtime.ProcessInstance; 23 import org.activiti.engine.task.Task; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 27 import java.text.ParseException; 28 import java.text.SimpleDateFormat; 29 import java.util.Date; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.Scanner; 33 34 /** 35 * 〈一句话功能简述〉<br> 36 * 〈〉 37 * 38 * @author happy 39 * @create 2018/6/23 40 * @since 1.0.0 41 */ 42 public class DemoMain { 43 44 public static final Logger log = LoggerFactory.getLogger(DemoMain.class); 45 46 public static void main(String[] args) { 47 48 log.info("启动我们的程序"); 49 50 //创建流程引擎 51 ProcessEngine processEngine = getProcessEngine(); 52 53 //部署流程定义文件 54 ProcessDefinition processDefinition = getProcessDefinition(processEngine); 55 56 //启动运行流程 57 final ProcessInstance[] processInstance = {getProcessInstance(processEngine, processDefinition)}; 58 59 //处理流程任务 60 Scanner scanner = new Scanner(System.in); 61 62 while (processInstance[0] != null && !processInstance[0].isEnded()) { 63 64 TaskService taskService = processEngine.getTaskService(); 65 List<Task> taskList = taskService.createTaskQuery().list(); 66 log.info("待处理任务数量 [{}]", taskList.size()); 67 taskList.stream().forEach(task -> { 68 log.info("待处理任务 [{}]", task.getName()); 69 FormService formService = processEngine.getFormService(); 70 TaskFormData taskFormData = formService.getTaskFormData(task.getId()); 71 List<FormProperty> formProperties = taskFormData.getFormProperties(); 72 Map<String, Object> variables = Maps.newHashMap(); 73 formProperties.stream().forEach(property -> { 74 String line = null; 75 if (StringFormType.class.isInstance(property.getType())) { 76 log.info("请输入 {} ?", property.getName()); 77 line = scanner.nextLine(); 78 variables.put(property.getId(), line); 79 } else if (DateFormType.class.isInstance(property.getType())) { 80 log.info("请输入 {} ? 格式 (yyyy-MM-dd)", property.getName()); 81 line = scanner.nextLine(); 82 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 83 Date date = null; 84 try { 85 date = dateFormat.parse(line); 86 } catch (ParseException e) { 87 e.printStackTrace(); 88 } 89 variables.put(property.getId(), date); 90 } else { 91 log.info("类型暂不支持 {}", property.getType()); 92 } 93 log.info("您输入的内容是 [{}]", line); 94 }); 95 taskService.complete(task.getId(), variables); 96 //Variable used in lambda expression should be final or effectively final
//(解决参考 https://www.jianshu.com/p/d957dd5560e8) 97 processInstance[0] = processEngine.getRuntimeService().createProcessInstanceQuery(). 98 processInstanceId(processInstance[0].getId()).singleResult(); 99 }); 100 101 } 102 103 log.info("结束我们的程序"); 104 105 } 106 107 private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) { 108 RuntimeService runtimeService = processEngine.getRuntimeService(); 109 ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId()); 110 log.info("启动流程 {}", processInstance.getProcessDefinitionKey()); 111 return processInstance; 112 } 113 114 private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) { 115 RepositoryService repositoryService = processEngine.getRepositoryService(); 116 DeploymentBuilder deploymentBuilder = repositoryService.createDeployment(); 117 deploymentBuilder.addClasspathResource("second_approve.bpmn20.xml"); 118 Deployment deployment = deploymentBuilder.deploy(); 119 String deploymentId = deployment.getId(); 120 ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() 121 .deploymentId(deploymentId) 122 .singleResult(); 123 124 log.info("流程定义文件 {},id {}", processDefinition.getName(), processDefinition.getId()); 125 return processDefinition; 126 } 127 128 private static ProcessEngine getProcessEngine() { 129 ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration(); 130 ProcessEngine processEngine = cfg.buildProcessEngine(); 131 String name = processEngine.getName(); 132 String version = ProcessEngine.VERSION; 133 134 log.info("流程引擎的名称 {},版本号 {}", name, version); 135 return processEngine; 136 } 137 138 }
<?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" 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> <exclusiveGateway id="decideSubmit" name="提交or取消"></exclusiveGateway> <sequenceFlow id="flow2" sourceRef="submitForm" targetRef="decideSubmit"></sequenceFlow> <endEvent id="endEventCancle" name="结束"></endEvent> <sequenceFlow id="flow3" sourceRef="decideSubmit" targetRef="endEventCancle"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="N" || submitType=="n"}]]></conditionExpression> </sequenceFlow> <userTask id="tlApprove" name="主管审批"> <extensionElements> <activiti:formProperty id="tlApprove" name="主管审批结果" type="string" required="true"></activiti:formProperty> <activiti:formProperty id="tlMessage" name="主管备注" type="string" required="true"></activiti:formProperty> </extensionElements> </userTask> <sequenceFlow id="flow4" sourceRef="decideSubmit" targetRef="tlApprove"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${submitType=="Y" || submitType=="y"}]]></conditionExpression> </sequenceFlow> <exclusiveGateway id="decideTlApprove" name="主管审批校验"></exclusiveGateway> <sequenceFlow id="flow5" sourceRef="tlApprove" 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> <sequenceFlow id="flow10" sourceRef="decideTlApprove" targetRef="submitForm"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${tlApprove=="N" || tlApprove=="n"}]]></conditionExpression> </sequenceFlow> <exclusiveGateway id="decideHrApprove" name="人事审批校验"></exclusiveGateway> <sequenceFlow id="flow11" sourceRef="hr_approve" targetRef="decideHrApprove"></sequenceFlow> <endEvent id="endEvent" name="结束"></endEvent> <sequenceFlow id="flow12" sourceRef="decideHrApprove" targetRef="endEvent"> <conditionExpression xsi:type="tFormalExpression"><![CDATA[${hrApprove=="Y" ||hrApprove=="y"}]]></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="50.0" y="130.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="submitForm" id="BPMNShape_submitForm"> <omgdc:Bounds height="55.0" width="105.0" x="130.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="decideSubmit" id="BPMNShape_decideSubmit"> <omgdc:Bounds height="40.0" width="40.0" x="280.0" y="128.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endEventCancle" id="BPMNShape_endEventCancle"> <omgdc:Bounds height="35.0" width="35.0" x="400.0" y="230.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="tlApprove" id="BPMNShape_tlApprove"> <omgdc:Bounds height="55.0" width="105.0" x="365.0" y="121.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="decideTlApprove" id="BPMNShape_decideTlApprove"> <omgdc:Bounds height="40.0" width="40.0" x="515.0" y="129.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="hr_approve" id="BPMNShape_hr_approve"> <omgdc:Bounds height="55.0" width="105.0" x="601.0" y="123.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="decideHrApprove" id="BPMNShape_decideHrApprove"> <omgdc:Bounds height="40.0" width="40.0" x="751.0" y="131.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endEvent" id="BPMNShape_endEvent"> <omgdc:Bounds height="35.0" width="35.0" x="836.0" y="134.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="85.0" y="147.0"></omgdi:waypoint> <omgdi:waypoint x="130.0" y="147.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="235.0" y="147.0"></omgdi:waypoint> <omgdi:waypoint x="280.0" y="148.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="300.0" y="168.0"></omgdi:waypoint> <omgdi:waypoint x="299.0" y="247.0"></omgdi:waypoint> <omgdi:waypoint x="400.0" y="247.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="320.0" y="148.0"></omgdi:waypoint> <omgdi:waypoint x="365.0" y="148.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"> <omgdi:waypoint x="470.0" y="148.0"></omgdi:waypoint> <omgdi:waypoint x="515.0" y="149.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6"> <omgdi:waypoint x="555.0" y="149.0"></omgdi:waypoint> <omgdi:waypoint x="601.0" y="150.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10"> <omgdi:waypoint x="535.0" y="169.0"></omgdi:waypoint> <omgdi:waypoint x="535.0" y="296.0"></omgdi:waypoint> <omgdi:waypoint x="182.0" y="296.0"></omgdi:waypoint> <omgdi:waypoint x="182.0" y="175.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11"> <omgdi:waypoint x="706.0" y="150.0"></omgdi:waypoint> <omgdi:waypoint x="751.0" y="151.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12"> <omgdi:waypoint x="791.0" y="151.0"></omgdi:waypoint> <omgdi:waypoint x="836.0" y="151.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13"> <omgdi:waypoint x="771.0" y="131.0"></omgdi:waypoint> <omgdi:waypoint x="770.0" y="50.0"></omgdi:waypoint> <omgdi:waypoint x="182.0" y="50.0"></omgdi:waypoint> <omgdi:waypoint x="182.0" y="120.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
以上是关于工作流学习之入门demo的主要内容,如果未能解决你的问题,请参考以下文章