当前位置:网站首页>Activiti目录(三)部署流程、发起流程

Activiti目录(三)部署流程、发起流程

2022-07-06 09:30:00 爱机车的程序猿

把Activiti打入你的脑袋,通过画流程、部署流程、发起流程的方式快速打入

目录

Activiti(一)入门了解

Activiti(二)整合

Activiti零零碎碎的坑

一、BPMN流程

内容讲解:
applyState:0学生发起请假 1老师审核通过 2老师领导审核通过 3院长审核通过结束 4请假驳回 6学生重新申请
applyExamine:驳回后学生重新申请到谁
Gareway:必须要满足一个条件不如会报错
在这里插入图片描述
struApply.bpmn20.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1651751051110" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
    <startEvent activiti:formKey="LeaveShanghaiApply" id="_2" name="StartEvent"/>
    <userTask activiti:assignee="${userName1}" activiti:async="false" activiti:candidateUsers="${userIds1}" activiti:exclusive="true" id="_3" name="teacher">
      <documentation id="_3_D_1"/>
    </userTask>
    <userTask activiti:assignee="${userName2}" activiti:candidateUsers="${userIds2}" activiti:exclusive="true" id="_4" name="academy"/>
    <userTask activiti:assignee="${userName3}" activiti:candidateUsers="${userIds3}" activiti:exclusive="true" id="_5" name="epidemicGroup"/>
    <endEvent id="_17" name="EndEvent"/>
    <exclusiveGateway gatewayDirection="Unspecified" id="_9" name="ExclusiveGateway1"/>
    <sequenceFlow id="_10" name="success" sourceRef="_9" targetRef="_4">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==1}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway gatewayDirection="Unspecified" id="_13" name="ExclusiveGateway2"/>
    <sequenceFlow id="_15" sourceRef="_4" targetRef="_13"/>
    <sequenceFlow id="_16" name="success" sourceRef="_13" targetRef="_5">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==2}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_27" sourceRef="_3" targetRef="_9"/>
    <userTask activiti:assignee="${userId}" activiti:exclusive="true" id="_28" name="strudent"/>
    <sequenceFlow id="_29" name="reject" sourceRef="_9" targetRef="_28">
      <documentation id="_29_D_1"><![CDATA[1]]></documentation>
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==4}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_30" name="reject" sourceRef="_13" targetRef="_28">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==4}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_32" sourceRef="_2" targetRef="_28"/>
    <exclusiveGateway gatewayDirection="Unspecified" id="_35" name="ExclusiveGateway3"/>
    <sequenceFlow id="_36" sourceRef="_5" targetRef="_35"/>
    <sequenceFlow id="_37" name="success" sourceRef="_35" targetRef="_17">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==3}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_38" name="reject" sourceRef="_35" targetRef="_28">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==4}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway gatewayDirection="Unspecified" id="_45" name="ExclusiveGateway"/>
    <sequenceFlow id="_46" sourceRef="_45" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyExamine==teacher}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_48" sourceRef="_45" targetRef="_4">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyExamine==academy}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_49" sourceRef="_45" targetRef="_5">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyExamine==epidemicGroup}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway gatewayDirection="Unspecified" id="_6" name="ExclusiveGateway"/>
    <sequenceFlow id="_7" sourceRef="_28" targetRef="_6"/>
    <sequenceFlow id="_8" sourceRef="_6" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==0}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_11" sourceRef="_6" targetRef="_45">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${applyState==6}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#FFFFFF;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="myProcess_1">
      <bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
        <omgdc:Bounds height="32.0" width="32.0" x="130.0" y="690.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
        <omgdc:Bounds height="55.0" width="85.0" x="440.0" y="290.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
        <omgdc:Bounds height="55.0" width="90.0" x="715.0" y="235.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="90.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5">
        <omgdc:Bounds height="55.0" width="110.0" x="965.0" y="155.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="110.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_17" id="Shape-_17">
        <omgdc:Bounds height="32.0" width="32.0" x="1005.0" y="715.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_9" id="Shape-_9" isMarkerVisible="false">
        <omgdc:Bounds height="32.0" width="32.0" x="470.0" y="415.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_13" id="Shape-_13" isMarkerVisible="false">
        <omgdc:Bounds height="32.0" width="32.0" x="740.0" y="460.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_28" id="Shape-_28">
        <omgdc:Bounds height="55.0" width="85.0" x="75.0" y="545.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_35" id="Shape-_35" isMarkerVisible="false">
        <omgdc:Bounds height="32.0" width="32.0" x="1000.0" y="545.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_45" id="Shape-_45" isMarkerVisible="false">
        <omgdc:Bounds height="32.0" width="32.0" x="100.0" y="170.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_6" id="Shape-_6" isMarkerVisible="false">
        <omgdc:Bounds height="32.0" width="32.0" x="100.0" y="325.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_15" id="BPMNEdge__15" sourceElement="_4" targetElement="_13">
        <omgdi:waypoint x="756.0" y="290.0"/>
        <omgdi:waypoint x="756.0" y="460.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_37" id="BPMNEdge__37" sourceElement="_35" targetElement="_17">
        <omgdi:waypoint x="1018.5" y="574.5"/>
        <omgdi:waypoint x="1018.5" y="715.1965193707209"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_36" id="BPMNEdge__36" sourceElement="_5" targetElement="_35">
        <omgdi:waypoint x="1016.0" y="210.0"/>
        <omgdi:waypoint x="1016.0" y="545.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_16" id="BPMNEdge__16" sourceElement="_13" targetElement="_5">
        <omgdi:waypoint x="772.0" y="476.0"/>
        <omgdi:waypoint x="965.0" y="182.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_38" id="BPMNEdge__38" sourceElement="_35" targetElement="_28">
        <omgdi:waypoint x="1008.0" y="569.0"/>
        <omgdi:waypoint x="681.0" y="569.0"/>
        <omgdi:waypoint x="160.0" y="569.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="99.0" x="0.0" y="21.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_46" id="BPMNEdge__46" sourceElement="_45" targetElement="_3">
        <omgdi:waypoint x="132.0" y="186.0"/>
        <omgdi:waypoint x="440.0" y="317.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_48" id="BPMNEdge__48" sourceElement="_45" targetElement="_4">
        <omgdi:waypoint x="132.0" y="186.0"/>
        <omgdi:waypoint x="715.0" y="262.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-2.0" width="0.0" x="0.0" y="2.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_27" id="BPMNEdge__27" sourceElement="_3" targetElement="_9">
        <omgdi:waypoint x="486.0" y="345.0"/>
        <omgdi:waypoint x="486.0" y="415.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_49" id="BPMNEdge__49" sourceElement="_45" targetElement="_5">
        <omgdi:waypoint x="131.0" y="185.0"/>
        <omgdi:waypoint x="565.0" y="185.0"/>
        <omgdi:waypoint x="965.0" y="185.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="95.0" x="0.0" y="-10.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_29" id="BPMNEdge__29" sourceElement="_9" targetElement="_28">
        <omgdi:waypoint x="470.0" y="431.0"/>
        <omgdi:waypoint x="335.0" y="505.0"/>
        <omgdi:waypoint x="160.0" y="572.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="69.0" width="-75.0" x="-1.0" y="-20.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7" sourceElement="_28" targetElement="_6">
        <omgdi:waypoint x="116.0" y="545.0"/>
        <omgdi:waypoint x="116.0" y="357.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_6" targetElement="_3">
        <omgdi:waypoint x="132.0" y="341.0"/>
        <omgdi:waypoint x="440.0" y="317.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_30" id="BPMNEdge__30" sourceElement="_13" targetElement="_28">
        <omgdi:waypoint x="740.0" y="476.0"/>
        <omgdi:waypoint x="490.0" y="570.0"/>
        <omgdi:waypoint x="160.0" y="570.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-94.0" width="46.0" x="0.0" y="19.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="_6" targetElement="_45">
        <omgdi:waypoint x="116.0" y="325.0"/>
        <omgdi:waypoint x="116.0" y="202.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10" sourceElement="_9" targetElement="_4">
        <omgdi:waypoint x="502.0" y="431.0"/>
        <omgdi:waypoint x="715.0" y="262.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_32" id="BPMNEdge__32" sourceElement="_2" targetElement="_28">
        <omgdi:waypoint x="145.0" y="690.0312805773287"/>
        <omgdi:waypoint x="145.0" y="600.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

在这里插入图片描述

二、部署流程

 /** * 部署流程 * TODO 1:获取配置对象 * TODO 2:创建核心引擎 * TODO 3:获取仓库服务 * TODO 4:部署流程(指向BPMN文件、BPMN图地址) * @author hjj */
    public Result deploymentProcess() {
    
        // 部署
        Deployment deployment = repositoryService // 获取部署相关Service
                .createDeployment() // 创建部署
                .key("struApplyProcess") //设置部署流程键值
                .addClasspathResource("bpmn/struApply.bpmn20.xml") //添加bpmn文件
// .addClasspathResource("bpmn/struApply.png") //添加png文件
                .name("学生请假流程") //设置流程名称
                .deploy(); // 部署
       return Result.success("流程部署ID:"+deployment.getId() + " - " + "流程部署Name:"+deployment.getName()+ " - " + "流程部署KEY:"+deployment.getKey());
    }

三、发起流程

/** * 启动一个流程 * TODO 1:查询用户 * TODO 2:启动申请流程设置流程实例ID及审核人员 * TODO 3:根据流程实例ID获取流程实例申请任务 * TODO 4:设置申请任务执行人 * TODO 5:发起申请任务 * TODO 6:根据流程实例ID关联业务信息并添加业务信息(一对一) * TODO 7:返回申请是否成功状态 * @param apply 申请记录信息 * @return * @author hjj */
        public Task startProcess(Apply apply){
    
        // 当前登录用户信息
        if (null == apply.getApplyEmail()) return null;
        String userId = apply.getApplyEmail();
        //查询正在执行的流程
        HistoricTaskInstanceQuery histTask = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).orderByHistoricTaskInstanceStartTime().desc();
        List<HistoricTaskInstance> tasks = null != histTask && 0 < histTask.list().size() ?
                histTask.list() : new ArrayList<>();
        // 流程是否存在
        Execution execution =  0 < tasks.size() ?
                runtimeService.createExecutionQuery().parentId(tasks.get(0).getProcessInstanceId()).singleResult() : null;
        if (Objects.isNull(execution)) {
    // 流程不存在
            // 老师Ids
            String teacherIds = "130,134,124";
            // 老师领导Ids
            String academyIds = "133,145";
            // 院长Ids
            String epidemicGroupIds = "160";
            //流程变量的map设定任务的负责人
            Map<String,Object> variablesMap = new HashMap<>();
            variablesMap.put("userId",userId);
            variablesMap.put("userName1","teacher");//联络老师IDs
            variablesMap.put("userIds1",teacherIds);
            variablesMap.put("userName2","academy");//老师领导ID
            variablesMap.put("userIds2",academyIds);
            variablesMap.put("userName3","epidemicGroup");//院长IDs
            variablesMap.put("userIds3",epidemicGroupIds);
            try {
    
                // 启动流程实例 key为部署流程图process-ID
                ProcessInstance processInstance = this.runtimeService.startProcessInstanceByKey("myProcess_1", variablesMap);
                // 流程实例ID
                String processInstanceId = processInstance.getId();
                //流程实例启动后,流程会跳转到申请节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
                // 发起申请任务
                Map<String, Object> args = new HashMap<>();
                args.put("applyState", "0");//设置流程参数
                taskService.complete(task.getId(), args);
                return task;
            }catch (Exception e){
    
                log.error(e.getMessage());
                return null;
            }
        } else {
    // 流程已存在
            String processInstanceId = execution.getProcessInstanceId();
            //流程实例启动后,流程会跳转到申请节点
            Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
            if(task == null ) return null;
            String msg=null != task ?"流程实例ID"+processInstanceId+
                    "\n任务ID:"+task.getId()+"任务名称:"+task.getName()+"任务负责人:"+task.getAssignee()+"任务开始时间:"+task.getCreateTime() : "";
            return task;
        }

    }
原网站

版权声明
本文为[爱机车的程序猿]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45067120/article/details/124816394