【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配

文章目录

    • 1. 个人任务
      • 1.1 流程设计
      • 1.2 测试类
      • 1.3 运行
        • 1.3.1 部署
        • 1.3.2 启动流程
        • 1.3.3 查询任务
        • 1.3.4 完成任务
        • 1.3.5 查询任务
        • 1.3.6 完成任务
        • 1.3.7 查询任务
    • 2. 候选人任务
      • 2.1 流程设计
      • 2.2 测试类
      • 2.3 运行
        • 2.3.1 部署
        • 2.3.2 启动流程
        • 2.3.3 查询任务
        • 2.3.4 认领任务
        • 2.3.5 完成任务
        • 2.3.6 查询任务
        • 2.3.7 完成任务
        • 2.3.8 查询任务
    • 3. 组任务
    • 源码地址
    • 项目推荐

Activiti分为个人任务和组任务,又各有3种分配方式。

  1. 指定
  2. 变量
  3. 监听器

*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *

1. 个人任务

1.1 流程设计

流程图
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第1张图片
流程文件bpmn


<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/processdef">
  <process id="assignUser" name="个人任务分配" isExecutable="true">
    <startEvent id="startEvent1">startEvent>
    <userTask id="userTask1" name="指定用户" activiti:assignee="admin">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">modeler:initiator-can-complete>
      extensionElements>
    userTask>
    <sequenceFlow id="sid-EC85E74C-7AB4-42EA-BFED-1FD829AF299D" sourceRef="startEvent1" targetRef="userTask1">sequenceFlow>
    <endEvent id="sid-A8829272-31FB-48E0-81CE-CDB2D74192C1">endEvent>
    <userTask id="userTask2" name="变量" activiti:assignee="${user}">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">modeler:initiator-can-complete>
      extensionElements>
    userTask>
    <sequenceFlow id="sid-B7C8E9BD-7131-4D64-894B-DD7A12687ABB" sourceRef="userTask1" targetRef="userTask2">sequenceFlow>
    <userTask id="userTask3" name="监听器">
      <extensionElements>
        <activiti:taskListener event="create" class="com.it.cloud.activiti.assignuser.UserTaskListener">activiti:taskListener>
      extensionElements>
    userTask>
    <sequenceFlow id="sid-E7BBDF7E-0EC9-4167-8E9C-56A35C5A22FE" sourceRef="userTask2" targetRef="userTask3">sequenceFlow>
    <sequenceFlow id="sid-CE341AD8-C034-41A7-8378-9011B395B1C5" sourceRef="userTask3" targetRef="sid-A8829272-31FB-48E0-81CE-CDB2D74192C1">sequenceFlow>
  process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_assignUser">
    <bpmndi:BPMNPlane bpmnElement="assignUser" id="BPMNPlane_assignUser">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="75.0" y="180.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask1" id="BPMNShape_userTask1">
        <omgdc:Bounds height="80.0" width="100.0" x="240.0" y="155.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-A8829272-31FB-48E0-81CE-CDB2D74192C1" id="BPMNShape_sid-A8829272-31FB-48E0-81CE-CDB2D74192C1">
        <omgdc:Bounds height="28.0" width="28.0" x="900.0" y="181.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask2" id="BPMNShape_userTask2">
        <omgdc:Bounds height="80.0" width="100.0" x="465.0" y="155.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask3" id="BPMNShape_userTask3">
        <omgdc:Bounds height="80.0" width="100.0" x="690.0" y="155.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-B7C8E9BD-7131-4D64-894B-DD7A12687ABB" id="BPMNEdge_sid-B7C8E9BD-7131-4D64-894B-DD7A12687ABB">
        <omgdi:waypoint x="340.0" y="195.0">omgdi:waypoint>
        <omgdi:waypoint x="465.0" y="195.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-EC85E74C-7AB4-42EA-BFED-1FD829AF299D" id="BPMNEdge_sid-EC85E74C-7AB4-42EA-BFED-1FD829AF299D">
        <omgdi:waypoint x="105.0" y="195.0">omgdi:waypoint>
        <omgdi:waypoint x="240.0" y="195.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-E7BBDF7E-0EC9-4167-8E9C-56A35C5A22FE" id="BPMNEdge_sid-E7BBDF7E-0EC9-4167-8E9C-56A35C5A22FE">
        <omgdi:waypoint x="565.0" y="195.0">omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="195.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-CE341AD8-C034-41A7-8378-9011B395B1C5" id="BPMNEdge_sid-CE341AD8-C034-41A7-8378-9011B395B1C5">
        <omgdi:waypoint x="790.0" y="195.0">omgdi:waypoint>
        <omgdi:waypoint x="900.0" y="195.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
definitions>

1.2 测试类

AssignUserProcessTest.java

import com.it.cloud.activiti.entity.Leave;
import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务变量测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class AssignUserProcessTest {

    @Autowired
    private IActReModelService actReModelService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("diagrams/assignUser.bpmn20.xml") // 加载流程资源文件
                .name("helloworld流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("assignUser"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @Test
    public void queryTask() {
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("zhangsan") // 指定某个人
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask() {
        taskService.complete("127504");
    }

    /**
     * 完成任务2
     */
    @Test
    public void completeTask2() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "李二");
        taskService.complete("125005", variables);
    }
}

1.3 运行

1.3.1 部署

运行deploy()

1.3.2 启动流程

运行start()

1.3.3 查询任务

运行queryTask(), 查询第一个用户节点(指定用户)

效果:
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第2张图片

1.3.4 完成任务

运行completeTask2(),设置流程变量,指定下一节点处理人

1.3.5 查询任务

运行queryTask(), 查询第二个用户节点(变量)

效果:
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第3张图片

1.3.6 完成任务

运行completeTask()

1.3.7 查询任务

运行queryTask(), 查询第三个用户节点(监听器)

效果:

【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第4张图片

2. 候选人任务

2.1 流程设计

流程图
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第5张图片
流程文件bpmn


<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/processdef">
  <process id="candidateUserAssign" name="候选人任务分配" isExecutable="true">
    <startEvent id="startEvent1">startEvent>
    <userTask id="userTask1" name="指定候选人" activiti:candidateUsers="admin,admin1,admin2">userTask>
    <sequenceFlow id="sid-EC85E74C-7AB4-42EA-BFED-1FD829AF299D" sourceRef="startEvent1" targetRef="userTask1">sequenceFlow>
    <endEvent id="sid-A8829272-31FB-48E0-81CE-CDB2D74192C1">endEvent>
    <userTask id="userTask2" name="变量" activiti:candidateUsers="${user}">userTask>
    <sequenceFlow id="sid-B7C8E9BD-7131-4D64-894B-DD7A12687ABB" sourceRef="userTask1" targetRef="userTask2">sequenceFlow>
    <userTask id="userTask3" name="监听器">
      <extensionElements>
        <activiti:taskListener event="create" class="com.it.cloud.activiti.assignuser.CandidateUserTaskListener">activiti:taskListener>
      extensionElements>
    userTask>
    <sequenceFlow id="sid-E7BBDF7E-0EC9-4167-8E9C-56A35C5A22FE" sourceRef="userTask2" targetRef="userTask3">sequenceFlow>
    <sequenceFlow id="sid-CE341AD8-C034-41A7-8378-9011B395B1C5" sourceRef="userTask3" targetRef="sid-A8829272-31FB-48E0-81CE-CDB2D74192C1">sequenceFlow>
  process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_candidateUserAssign">
    <bpmndi:BPMNPlane bpmnElement="candidateUserAssign" id="BPMNPlane_candidateUserAssign">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="75.0" y="175.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask1" id="BPMNShape_userTask1">
        <omgdc:Bounds height="80.0" width="100.0" x="240.0" y="150.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-A8829272-31FB-48E0-81CE-CDB2D74192C1" id="BPMNShape_sid-A8829272-31FB-48E0-81CE-CDB2D74192C1">
        <omgdc:Bounds height="28.0" width="28.0" x="900.0" y="176.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask2" id="BPMNShape_userTask2">
        <omgdc:Bounds height="80.0" width="100.0" x="465.0" y="150.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask3" id="BPMNShape_userTask3">
        <omgdc:Bounds height="80.0" width="100.0" x="690.0" y="150.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-B7C8E9BD-7131-4D64-894B-DD7A12687ABB" id="BPMNEdge_sid-B7C8E9BD-7131-4D64-894B-DD7A12687ABB">
        <omgdi:waypoint x="340.0" y="190.0">omgdi:waypoint>
        <omgdi:waypoint x="465.0" y="190.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-EC85E74C-7AB4-42EA-BFED-1FD829AF299D" id="BPMNEdge_sid-EC85E74C-7AB4-42EA-BFED-1FD829AF299D">
        <omgdi:waypoint x="105.0" y="190.0">omgdi:waypoint>
        <omgdi:waypoint x="240.0" y="190.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-E7BBDF7E-0EC9-4167-8E9C-56A35C5A22FE" id="BPMNEdge_sid-E7BBDF7E-0EC9-4167-8E9C-56A35C5A22FE">
        <omgdi:waypoint x="565.0" y="190.0">omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="190.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-CE341AD8-C034-41A7-8378-9011B395B1C5" id="BPMNEdge_sid-CE341AD8-C034-41A7-8378-9011B395B1C5">
        <omgdi:waypoint x="790.0" y="190.0">omgdi:waypoint>
        <omgdi:waypoint x="900.0" y="190.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
definitions>

2.2 测试类

CandidateUserProcessTest.java

import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务变量测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class CandidateUserProcessTest {

    @Autowired
    private IActReModelService actReModelService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("diagrams/candidateUserAssign.bpmn20.xml") // 加载流程资源文件
                .name("candidateUserAssign流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("candidateUserAssign"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @Test
    public void queryTaskByProcessInstanceId() {
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .processInstanceId("142501") // 流程实例ID
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }

    /**
     * 认领
     */
    @Test
    public void claimTask() {
        String taskId = "142505";
        taskService.claim(taskId, "admin");
    }

    /**
     * 查看任务
     */
    @Test
    public void queryTask() {
        List<Task> taskList = taskService.createTaskQuery() // 创建任务查询
                .taskAssignee("admin") // 指定某个人
                .list();
        for (Task task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务创建时间:" + task.getCreateTime());
            System.out.println("任务委派人:" + task.getAssignee());
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask() {
        taskService.complete("145004");
    }

    /**
     * 完成任务2
     */
    @Test
    public void completeTask2() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "李二,杨六");
        taskService.complete("142505", variables);
    }
}

2.3 运行

2.3.1 部署

运行deploy()

2.3.2 启动流程

运行start()

效果:
数据表ACT_RU_IDENTITYLINK中添加了6条数据,admin分为candidate和participant,代表任务候选人和流程实例参与者。
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第6张图片

2.3.3 查询任务

运行queryTaskByProcessInstanceId(), 查询第一个用户节点(指定候选人)

效果:
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第7张图片

2.3.4 认领任务

运行claimTask(),用户admin认领当前任务

效果
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第8张图片

2.3.5 完成任务

运行completeTask2(),设置流程变量,指定下一节点候选人

效果
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第9张图片

2.3.6 查询任务

运行queryTask(), 查询第二个用户节点(变量)

效果:
【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第10张图片

2.3.7 完成任务

运行completeTask()

2.3.8 查询任务

运行queryTask(), 查询第三个用户节点(监听器)

效果:

【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第11张图片

3. 组任务

组任务跟个人任务设置方式一样,区别在于流程设计器中使用

【弄nèng - Activiti6】Activiti6入门篇(四)—— 任务分配_第12张图片
还有就是,组的数据从ACT_ID_GROUP获取,设置成表中name字段的值。 因为Activiti的权限表功能单一,所以一般不建议使用,适用业务自己的权限表。
如果想用,那就同步业务权限数据到Activiti,用户表对应ACT_ID_USER,角色表对应ACT_ID_GROUP,用户角色关系表对应ACT_ID_MEMBERSHIP


源码地址

IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~

项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~

你可能感兴趣的:(Activiti6,activiti)