Activiti 个人任务(三种指派方式)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caoyue_new/article/details/52180539

用户任务需要用户手动来完成的任务,可以直接分配给一个用户,也可以指定用户组,再由用户认领。本文我们来介绍一下如何将任务指派给个人,流程图如下:
这里写图片描述

方式一:

定义流程图时直接指定完成任务人(项目开发中任务的办理人不要放置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: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="personalAssignee1" name="PersonalAssignee1" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="审批" activiti:assignee="crystal"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_personalAssignee1">
    <bpmndi:BPMNPlane bpmnElement="personalAssignee1" id="BPMNPlane_personalAssignee1">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="360.0" y="20.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="325.0" y="100.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="360.0" y="200.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="377.0" y="55.0"></omgdi:waypoint>
        <omgdi:waypoint x="377.0" y="100.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="377.0" y="155.0"></omgdi:waypoint>
        <omgdi:waypoint x="377.0" y="200.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

方式二:

定义流程图时配置任务节点变量,完成任务之前由流程变量指定任务办理人。在开发中,可以在页面中指定下一个任务的办理人,通过流程变量设置下一个任务的办理人。
这里写图片描述

流程图代码

<?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="personalTask2" name="PersonalTask2" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="审批" activiti:assignee="#{userId}"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_personalTask2">
    <bpmndi:BPMNPlane bpmnElement="personalTask2" id="BPMNPlane_personalTask2">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="380.0" y="40.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="345.0" y="110.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="380.0" y="210.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="397.0" y="75.0"></omgdi:waypoint>
        <omgdi:waypoint x="397.0" y="110.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="397.0" y="165.0"></omgdi:waypoint>
        <omgdi:waypoint x="397.0" y="210.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

方式三:

流程图中不指定任务办理人,添加监听类,需要实现TaskListener接口。
这里写图片描述
这里写图片描述

流程图代码:

<?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="personalTask3" name="PersonalTask3" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="审批">
      <extensionElements>
        <activiti:taskListener event="create" class="com.activiti.test.TaskListenerImpl"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_personalTask3">
    <bpmndi:BPMNPlane bpmnElement="personalTask3" id="BPMNPlane_personalTask3">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="310.0" y="20.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="275.0" y="100.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="310.0" y="200.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="327.0" y="55.0"></omgdi:waypoint>
        <omgdi:waypoint x="327.0" y="100.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="327.0" y="155.0"></omgdi:waypoint>
        <omgdi:waypoint x="327.0" y="200.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

监听类 TaskListenerImpl.java

package com.activiti.test;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class TaskListenerImpl implements TaskListener {

    /**
     * 指定个人任务和组任务的办理人
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        delegateTask.setAssignee("crystal");// 指派个人任务
    }
}

测试类:

package com.activiti.test;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class PersonalTask {
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程定义,启动流程实例
     */
    @Test
    public void deployAndStart() {
        processEngine.getRepositoryService()
            .createDeployment()
            .name("指定个人任务")
            .addClasspathResource("diagrams/personalTask3.bpmn")
            .addClasspathResource("diagrams/personalTask3.png")
            .deploy();
        System.out.println("部署成功!");
        // 方式一
        /*ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("personalTask1");*/
        // 方式二
        /*Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("userId", "crystal");
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("personalTask2", variables);*/
        // 方式三
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("personalTask3");
        System.out.println("流程实例启动成功!实例ID:" + pi.getId());
    }

    /**
     * 查询我的个人任务列表
     */
    @Test
    public void findMyTaskList(){
        String userId = "crystal";
        List<Task> list = processEngine.getTaskService()
                        .createTaskQuery()
                        .taskAssignee(userId)//指定个人任务查询
                        .list();
        if (list != null && list.size()>0) {
            for(Task task : list){
                System.out.println("task id="+task.getId());
                System.out.println("name="+task.getName());
                System.out.println("assinee="+task.getAssignee());
                System.out.println("executionId="+task.getExecutionId());
                System.out.println("=====================================");
            }
        }
    }

    /**
     * 完成我的个人任务
     */
    @Test
    public void complete() {
        String taskId = "167508";
        processEngine.getTaskService().complete(taskId);
        System.out.println("完成任务");
    }

    /**
     * 可以分配个人任务从一个人到另一个人
     */
    @Test
    public void setAssigneeTask(){
        String taskId = "172508";//任务ID
        String userId = "barry";//指定任务的办理者
        processEngine.getTaskService().setAssignee(taskId, userId);
        System.out.println("指派成功");
    }
}

总结:

个人任务及三种分配方式:
1:在taskProcess.bpmn中直接写 assignee=”crystal”
2:在taskProcess.bpmn中写 assignee=“#{userID}”,变量的值要是String的(使用流程变量指定办理人)。
3: 使用TaskListener接口,要使类实现该接口,在类中定义:
delegateTask.setAssignee(assignee);// 指定个人任务的办理人
使用任务ID和办理人重新指定办理人:
processEngine.getTaskService().setAssignee(taskId, userId);

展开阅读全文

没有更多推荐了,返回首页