Activiti搭建简单业务流

本章在创建表的基础上进行,如果对Activit创建表不了解的可以看一下Activiti入门

1.绘制流程

在resources下创建diagrams文件夹,右键新建BPMN文件CompanyLeaveProcess.bpmn

使用图标绘制

 CompanyLeaveProcess.bpmn.xml代码如下(绘制设置参数后自动生成的)

<process id="leaves" isClosed="false" isExecutable="true" name="leaves" processType="None">
    <startEvent id="start" name="开始">
      <documentation id="_2_D_1"><![CDATA[公司请假流程开始]]></documentation>
    </startEvent>
    <userTask activiti:assignee="${staffName}" activiti:exclusive="true" id="staff" name="员工申请请假">
      <documentation id="_3_D_1"><![CDATA[员工发起请假申请]]></documentation>
    </userTask>
    <sequenceFlow id="_4" sourceRef="start" targetRef="staff"/>
    <userTask activiti:assignee="${deploymentHeadName}" activiti:exclusive="true" id="deploymentHead" name="部门领导审核">
      <documentation id="_5_D_1"><![CDATA[部门领导对请假消息进行审核]]></documentation>
    </userTask>
    <exclusiveGateway gatewayDirection="Unspecified" id="_6" name="ExclusiveGateway"/>
    <sequenceFlow id="_7" sourceRef="staff" targetRef="deploymentHead"/>
    <sequenceFlow id="_8" sourceRef="deploymentHead" targetRef="_6"/>
    <sequenceFlow id="_9" name="部门领导不同意,重新请假" sourceRef="_6" targetRef="staff">
      <documentation id="_9_D_1"><![CDATA[部门领导不同意请假]]></documentation>
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${deployCheck==0}]]></conditionExpression>
    </sequenceFlow>
    <userTask activiti:assignee="${managerName1}" activiti:exclusive="true" id="manager1" name="总经理1">
      <documentation id="_10_D_1"><![CDATA[总经理1对员工的请假进行审核]]></documentation>
    </userTask>
    <userTask activiti:assignee="${managerName2}" activiti:exclusive="true" id="manage2" name="总经理2">
      <documentation id="_11_D_1"><![CDATA[总经理2对员工请假申请进行审核]]></documentation>
    </userTask>
    <exclusiveGateway gatewayDirection="Unspecified" id="_12" name="ExclusiveGateway"/>
    <endEvent id="end" name="结束">
      <documentation id="_13_D_1"><![CDATA[请假成功,流程结束]]></documentation>
    </endEvent>
    <sequenceFlow id="_14" name="总经理同意" sourceRef="_12" targetRef="end">
      <documentation id="_14_D_1"><![CDATA[领导同意请假]]></documentation>
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result==1}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_15" sourceRef="manager1" targetRef="_12"/>
    <sequenceFlow id="_16" sourceRef="manage2" targetRef="_12"/>
    <sequenceFlow id="_2" name="请假3天以内" sourceRef="_6" targetRef="manager1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day<=3&&day>0&&deployCheck==1}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_3" name="请假大于3天" sourceRef="_6" targetRef="manage2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day>3&&deployCheck==1}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_17" name="请假失败" sourceRef="_12" targetRef="staff">
      <documentation id="_17_D_1"><![CDATA[总经理不同意请假,重新请假]]></documentation>
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result==0}]]></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="leaves">
      <bpmndi:BPMNShape bpmnElement="start" id="Shape-start">
        <omgdc:Bounds height="32.0" width="32.0" x="60.0" y="220.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="staff" id="Shape-staff">
        <omgdc:Bounds height="55.0" width="85.0" x="160.0" y="210.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="deploymentHead" id="Shape-deploymentHead">
        <omgdc:Bounds height="55.0" width="85.0" x="330.0" y="210.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.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="505.0" y="220.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="manager1" id="Shape-manager1">
        <omgdc:Bounds height="55.0" width="85.0" x="700.0" y="140.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="manage2" id="Shape-manage2">
        <omgdc:Bounds height="55.0" width="85.0" x="700.0" y="275.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_12" id="Shape-_12" isMarkerVisible="false">
        <omgdc:Bounds height="32.0" width="32.0" x="860.0" y="210.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end" id="Shape-end">
        <omgdc:Bounds height="32.0" width="32.0" x="990.0" y="210.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="manager1" targetElement="_12">
        <omgdi:waypoint x="785.0" y="167.5"/>
        <omgdi:waypoint x="860.0" y="226.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_14" id="BPMNEdge__14" sourceElement="_12" targetElement="end">
        <omgdi:waypoint x="892.0" y="226.0"/>
        <omgdi:waypoint x="990.0" y="226.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_17" id="BPMNEdge__17" sourceElement="_12" targetElement="staff">
        <omgdi:waypoint x="876.0" y="242.0"/>
        <omgdi:waypoint x="550.0" y="470.0"/>
        <omgdi:waypoint x="202.5" y="265.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="manage2" targetElement="_12">
        <omgdi:waypoint x="785.0" y="302.5"/>
        <omgdi:waypoint x="860.0" y="226.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_2" id="BPMNEdge__2" sourceElement="_6" targetElement="manager1">
        <omgdi:waypoint x="537.0" y="236.0"/>
        <omgdi:waypoint x="700.0" y="167.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_3" id="BPMNEdge__3" sourceElement="_6" targetElement="manage2">
        <omgdi:waypoint x="537.0" y="236.0"/>
        <omgdi:waypoint x="700.0" y="302.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_4" id="BPMNEdge__4" sourceElement="start" targetElement="staff">
        <omgdi:waypoint x="92.0" y="236.0"/>
        <omgdi:waypoint x="160.0" y="237.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7" sourceElement="staff" targetElement="deploymentHead">
        <omgdi:waypoint x="245.0" y="237.5"/>
        <omgdi:waypoint x="330.0" y="237.5"/>
        <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="deploymentHead" targetElement="_6">
        <omgdi:waypoint x="415.0" y="237.5"/>
        <omgdi:waypoint x="505.0" y="236.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9" sourceElement="_6" targetElement="staff">
        <omgdi:waypoint x="521.0" y="220.0"/>
        <omgdi:waypoint x="385.0" y="100.0"/>
        <omgdi:waypoint x="202.5" y="210.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>
View Code

部署流程定义

@Test
public void deploymentProcessDefine(){
Deployment deployment=processEngine.getRepositoryService()
.createDeployment()//创建部署对象
.name("部署公司请假流程")//设置部署对象名称
.addClasspathResource("diagrams/CompanyLeaveProcess.bpmn.xml")//加载资源文件
.deploy();//创建完成
System.out.println("部署ID"+deployment.getId());
System.out.println("Name"+deployment.getName());
}

运行部署流程后

数据库ACT_RE_PROCDEF与 ACT_RE_DEPLOYMENT会相继生成一个实例

启动实例

@Test
    public void startProcess(){
        System.out.println("开启流程");
        Random random=new Random();
        int i=random.nextInt(10);
        Map<String,Object>variables=new HashMap<>();
        variables.put("staffName","张三");
        variables.put("deploymentHeadName","李组长");
        variables.put("managerName1","王经理");
        variables.put("managerName2","张经理");
        variables.put("day",i);


        RuntimeService runtimeService=processEngine.getRuntimeService();

        String processKey="leaves";

        ProcessInstance processInstance=runtimeService.startProcessInstanceByKey(processKey,variables);
        System.out.println("实例ID----"+processInstance.getId());
    }
View Code

此时查询数据库select * from ACT_RU_TASK会发现有一个任务开启

获取任务完成流程

@Test
    public void getStartAndComplete(){
        TaskService taskService=processEngine.getTaskService();




        /*taskService.setAssignee("","");
        while(this.processEngine.getTaskService().createTaskQuery().taskAssignee("王经理").singleResult()!=null){

        }*/
        while(this.processEngine.getTaskService().createTaskQuery()
                .processInstanceId("5001").singleResult()!=null){
            Task task=taskService.createTaskQuery()
                    .processInstanceId("5001").singleResult();

            String taskName=task.getName();
            if (taskName.equals("员工申请请假")){
                completeStaffTask(task);
            }else if(taskName.equals("部门领导审核")){
                completeEmploymentHeadTask(task);
            }else{
                completeManagerTask(task);
            }
        }

    }

    private void completeManagerTask(Task task) {
        System.out.println("经理领导开始审核.......");
        Map<String,Object>variables=new HashMap<>();
        Random random=new Random();
        int i=random.nextInt(2);

        variables.put("result",i);
        processEngine.getTaskService().complete(task.getId(),variables);
        System.out.println("经理完成审核......."+task.getName());
    }

    private void completeEmploymentHeadTask(Task task) {
        System.out.println("部门领导开始审核.......");
        Map<String,Object>variables=new HashMap<>();
        Random random=new Random();
        int i=random.nextInt(2);
        variables.put("deployCheck",i);
        processEngine.getTaskService().complete(task.getId(),variables);
        System.out.println("部门领导完成审核.......");
    }

    private void completeStaffTask(Task task) {

        /*Map<String,Object>variables=new HashMap<>();
        variables.put("staffName","张三");*/
        processEngine.getTaskService().complete(task.getId());
        System.out.println("职员已经提交申请.......");
    }
View Code

 任务执行完后select * from ACT_HI_TASKINST,select * from ACT_HI_ACTINST两表内会看到流程的历史记录  同时select * from ACT_RU_TASK表中关于此任务信息会消失。

因为任务流程中在组长审核与经理审核时使用随机数,所以才会看到下面很多条记录

 

posted @ 2019-08-22 17:18  wangyufeng  阅读(381)  评论(0编辑  收藏  举报