Loading

Activiti7进阶之组任务和网关

1 组任务

1.1 概述

  • 在流程定义中的任务结点的assignee固定设置任务负责人,如果临时任务负责人变更则需要修改流程定义,系统的可扩展性差。针对这种情况可以给任务设置多个候选人,可以从候选人中选择参与者来完成任务。

1.2 设置任务候选人

  • 在流程图中任务结点的配置中设置Candidate users,多个候选人之间用逗号隔开。

设置任务候选人.

  • 查看holiday.bpmn文件:
<process id="holiday" name="请假申请流程" isExecutable="true">
    <startEvent id="startevent1" name="开始"></startEvent>
    <userTask id="usertask1" name="填写请假申请单" activiti:assignee="张三"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <userTask id="usertask2" name="部门经理审批" activiti:candidateUsers="李四,王五"></userTask>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <userTask id="usertask3" name="总经理审批" activiti:assignee="赵六"></userTask>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
    <endEvent id="endevent1" name="结束"></endEvent>
    <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
</process>
  • 我们可以看到部门经理审批的审核人已经设置为"李四,王五"这样的一组候选人了。

1.3 组任务的办理流程

  • 1️⃣查询组任务:
    • 指定候选人,查询该候选人当前的待办任务。
    • 候选人不能办理任务。
  • 2️⃣拾取(claim)任务:
    • 该组任务的所有候选人都能拾取任务。
    • 所谓的拾取任务就是将候选热的组任务变成个人任务,即原来的候选人变成了任务的负责人。
    • 如果拾取任务后不想办理任务,只需要将已经拾取的个人任务归还到组里面,那么此时的个人任务将变为组任务。
  • 3️⃣查询个人任务:
    • 查询方式同个人任务部分,根据assignee查询用户负责的个人任务。
  • 4️⃣办理个人任务。

1.4 组任务办理流程的应用示例

1.4.1 准备工作

  • 创建请假流程的BPMN文件(其中填写请假申请单的assignee是张三,部门经理审批的candidate users是李四,王五,总经理审批的assignee是赵六):

创建请假申请流程的bmpn文件

  • 部署流程定义以及启动流程实例:
package com.sunxiaping;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Before;
import org.junit.Test;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }
}

1.4.2 张三完成请假申请任务

  • 示例:
package com.sunxiaping;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }

    /**
     * 张三完成请假申请任务
     */
    @Test
    public void testTaskZhangsan() {
        String processDefinitionKey = "holiday";

        String assignee = "张三";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }

    }
}

1.4.3 李四查询组任务

  • 示例:
package com.sunxiaping;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }

    /**
     * 张三完成请假申请任务
     */
    @Test
    public void testTaskZhangsan() {
        String processDefinitionKey = "holiday";

        String assignee = "张三";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }

    }

    /**
     * 查询候选人组任务
     */
    @Test
    public void testQueryCandidateGroupTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();
        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);
            String processInstanceId = task.getProcessInstanceId();
            System.out.println("流程实例的id = " + processInstanceId);
            String name = task.getName();
            System.out.println("任务名称 = " + name);
            String assignee = task.getAssignee(); //null,说明当前的张三只是一个候选人,不是任务的负责人
            System.out.println("任务的负责人是 = " + assignee);
        }
    }
}

1.4.4 李四拾取任务

  • 示例:
package com.sunxiaping;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }

    /**
     * 张三完成请假申请任务
     */
    @Test
    public void testTaskZhangsan() {
        String processDefinitionKey = "holiday";

        String assignee = "张三";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }

    }

    /**
     * 查询候选人组任务
     */
    @Test
    public void testQueryCandidateGroupTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();
        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);
            String processInstanceId = task.getProcessInstanceId();
            System.out.println("流程实例的id = " + processInstanceId);
            String name = task.getName();
            System.out.println("任务名称 = " + name);
            String assignee = task.getAssignee(); //null,说明当前的张三只是一个候选人,不是任务的负责人
            System.out.println("任务的负责人是 = " + assignee);
        }
    }

    /**
     * 用户拾取组任务:将候选人的组任务,变成个人任务。
     * 注意:
     *      ①即使该用户不是候选人也能拾取,建议拾取的时候校验是否有资格拾取任务。
     *      ②组任务拾取之后,该任务就有负责人了,通过候选人将查询不到该任务
     */
    @Test
    public void testClaimTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();

        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.claim(taskId,candidateUser);

            System.out.println("任务拾取完毕");
        }
    }
}

1.4.5 李四查询任务并完成任务

  • 示例:
package com.sunxiaping;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }

    /**
     * 张三完成请假申请任务
     */
    @Test
    public void testTaskZhangsan() {
        String processDefinitionKey = "holiday";

        String assignee = "张三";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }

    }

    /**
     * 查询候选人组任务
     */
    @Test
    public void testQueryCandidateGroupTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();
        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);
            String processInstanceId = task.getProcessInstanceId();
            System.out.println("流程实例的id = " + processInstanceId);
            String name = task.getName();
            System.out.println("任务名称 = " + name);
            String assignee = task.getAssignee(); //null,说明当前的张三只是一个候选人,不是任务的负责人
            System.out.println("任务的负责人是 = " + assignee);
        }
    }

    /**
     * 用户拾取组任务:将候选人的组任务,变成个人任务。
     * 注意:
     *      ①即使该用户不是候选人也能拾取,建议拾取的时候校验是否有资格拾取任务。
     *      ②组任务拾取之后,该任务就有负责人了,通过候选人将查询不到该任务
     */
    @Test
    public void testClaimTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();

        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.claim(taskId,candidateUser);

            System.out.println("任务拾取完毕");
        }
    }


    /**
     * 查询任务并完成任务
     */
    @Test
    public void testCompleteTask(){
        String processDefinitionKey = "holiday";

        String assignee = "李四";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }
    }
}

1.4.6 当前用户归还组任务

  • 示例:
package com.sunxiaping;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }

    /**
     * 张三完成请假申请任务
     */
    @Test
    public void testTaskZhangsan() {
        String processDefinitionKey = "holiday";

        String assignee = "张三";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }

    }

    /**
     * 查询候选人组任务
     */
    @Test
    public void testQueryCandidateGroupTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();
        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);
            String processInstanceId = task.getProcessInstanceId();
            System.out.println("流程实例的id = " + processInstanceId);
            String name = task.getName();
            System.out.println("任务名称 = " + name);
            String assignee = task.getAssignee(); //null,说明当前的张三只是一个候选人,不是任务的负责人
            System.out.println("任务的负责人是 = " + assignee);
        }
    }

    /**
     * 用户拾取组任务:将候选人的组任务,变成个人任务。
     * 注意:
     *      ①即使该用户不是候选人也能拾取,建议拾取的时候校验是否有资格拾取任务。
     *      ②组任务拾取之后,该任务就有负责人了,通过候选人将查询不到该任务
     */
    @Test
    public void testClaimTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();

        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.claim(taskId,candidateUser);

            System.out.println("任务拾取完毕");
        }
    }

    /**
     * 归还组任务
     * 说明:建议归还任务前校验用户是否是该任务的负责人,也可以通过setAssignee方法将任务委托给其他用户负责,注意被委托的用户可以不是候选人(建议不要这么做)
     */
    @Test
    public void testUnclaim(){
        String processDefinitionKey = "holiday";

        String assignee = "李四";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            // 如果设置为null,归还组任务,该 任务没有负责人
            taskService.setAssignee(taskId, null);
        }
    }
    
//    /**
//     * 查询任务并完成任务
//     */
//    @Test
//    public void testCompleteTask(){
//        String processDefinitionKey = "holiday";
//
//        String assignee = "李四";
//
//        TaskService taskService = processEngine.getTaskService();
//
//        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();
//
//        for (Task task : taskList) {
//            String taskId = task.getId();
//            System.out.println("任务的id = " + taskId);
//
//            taskService.complete(taskId);
//        }
//    }
    
}

1.4.7 任务交接

  • 任务交接就是任务负责人将任务交给其它候选人办理该任务。

  • 示例:

package com.sunxiaping;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
 * @version 1.0
 * 2020-08-10 21:37
 */
public class GroupTest {

    ProcessEngine processEngine = null;

    @Before
    public void before() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        processEngine = configuration.buildProcessEngine();
    }

    @Test
    public void after() {
        if (processEngine != null) {
            processEngine.close();
        }
    }

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday.bpmn")
                .addClasspathResource("diagram/holiday.png")
                .name("请假申请流程")
                .deploy();

        String id = deployment.getId();
        System.out.println("流程部署的id = " + id);
        String name = deployment.getName();
        System.out.println("流程部署的名称 = " + name);
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {

        String processDefinitionKey = "holiday";

        RuntimeService runtimeService = processEngine.getRuntimeService();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        String id = processInstance.getId();
        System.out.println("流程实例的id = " + id);
    }

    /**
     * 张三完成请假申请任务
     */
    @Test
    public void testTaskZhangsan() {
        String processDefinitionKey = "holiday";

        String assignee = "张三";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.complete(taskId);
        }

    }

    /**
     * 查询候选人组任务
     */
    @Test
    public void testQueryCandidateGroupTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();
        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);
            String processInstanceId = task.getProcessInstanceId();
            System.out.println("流程实例的id = " + processInstanceId);
            String name = task.getName();
            System.out.println("任务名称 = " + name);
            String assignee = task.getAssignee(); //null,说明当前的张三只是一个候选人,不是任务的负责人
            System.out.println("任务的负责人是 = " + assignee);
        }
    }

    /**
     * 用户拾取组任务:将候选人的组任务,变成个人任务。
     * 注意:
     * ①即使该用户不是候选人也能拾取,建议拾取的时候校验是否有资格拾取任务。
     * ②组任务拾取之后,该任务就有负责人了,通过候选人将查询不到该任务
     */
    @Test
    public void testClaimTask() {
        String processDefinitionKey = "holiday";

        String candidateUser = "李四";

        TaskService taskService = processEngine.getTaskService();

        //查询组任务
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskCandidateUser(candidateUser).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.claim(taskId, candidateUser);

            System.out.println("任务拾取完毕");
        }
    }

    /**
     * 任务交接
     */
    @Test
    public void testExchangeTask() {
        String processDefinitionKey = "holiday";

        String assignee = "李四";

        TaskService taskService = processEngine.getTaskService();

        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();

        for (Task task : taskList) {
            String taskId = task.getId();
            System.out.println("任务的id = " + taskId);

            taskService.setAssignee(taskId, "王五");
        }

    }


//    /**
//     * 归还组任务
//     * 说明:建议归还任务前校验用户是否是该任务的负责人,也可以通过setAssignee方法将任务委托给其他用户负责,注意被委托的用户可以不是候选人(建议不要这么做)
//     */
//    @Test
//    public void testUnclaim() {
//        String processDefinitionKey = "holiday";
//
//        String assignee = "李四";
//
//        TaskService taskService = processEngine.getTaskService();
//
//        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();
//
//        for (Task task : taskList) {
//            String taskId = task.getId();
//            System.out.println("任务的id = " + taskId);
//
//            // 如果设置为null,归还组任务,该 任务没有负责人
//            taskService.setAssignee(taskId, null);
//        }
//    }

//    /**
//     * 查询任务并完成任务
//     */
//    @Test
//    public void testCompleteTask(){
//        String processDefinitionKey = "holiday";
//
//        String assignee = "李四";
//
//        TaskService taskService = processEngine.getTaskService();
//
//        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(assignee).list();
//
//        for (Task task : taskList) {
//            String taskId = task.getId();
//            System.out.println("任务的id = " + taskId);
//
//            taskService.complete(taskId);
//        }
//    }

}

1.4.8 数据库表操作

  • act_ru_task:任务执行表,记录当前执行的任务,由于该任务当前是组任务,所有的assignee为空,当拾取任务后该字段就是拾取用户的id。

act_ru_task任务执行表

  • act_ru_identitylink:任务参与者,记录当前参与任务用户或组,当前任务如果设置了候选人,会向该表插入候选人记录,有几个候选人就插入几个。

act_ru_identitylink任务参与者表

  • act_ru_identitylink对应一张历史表act_hi_identitylink,向act_ru_identitylink插入记录的同时也会向历史表插入记录。

act_hi_identitylink任务参与者历史表

2 网关

2.1 排他网关

2.1.1 什么是排他网关?

  • 排他网关(异或网关或基于数据的排他网关),用来在流程中实现决策。当流程执行到这个网关的时候,所有分支都会判断条件是否为true,如果为true则执行该分支。

注意:

  • 排他网关只会选择一个为true的分支执行(即使有两个分支条件都为true,排他网关也只会选择一条分支去执行)。

2.1.2 流程定义

  • 如果不用排他网关,如果条件都满足或都不满足,流程就结束(异常结束)了。

不使用排他网关

  • 如果使用了排他网关来决定分支的走向,如下:

使用排他网关

注意:

  • 如果从网关出去的所有分支都不满足条件,系统将抛出异常。换言之,经过排他网关有且仅有一条分支。

2.2 并行网关

2.2.1 什么是并行网关?

  • 并行网关允许将流程分成多条分支,也可以把多条分支汇聚到一起,并行网关的功能是基于进入和外出的顺序流的。
  • Fork分支:并行后的所有外出顺序流,为每个顺序流都创建一个并行分支。
  • Join汇聚:所有到达并行网关,在此等待的进入分支,直到所有进入顺序流的分支都到达以后,流程就会通过汇聚网关。

注意:

  • 如果桶一个并行网关有多个进入和多个外出顺序流,它就同时具有分支和汇聚功能。这时,网关会先汇聚所有进入的顺序流,然后再切分成多个并行分支。
  • 和其他网关不同的是,并行网关并不会解析条件。即使顺序流中定义了条件,也会被忽略。

2.2.2 流程定义

并行网关

2.3 包含网关

2.3.1 什么是包含网关?

  • 包含网关可以看做是排他网关和并行网关的结合体。和排他网关一样,你可以在外出顺序流上定义条件,包含网关会解析它们。但是主要的区别是包含网关可以选择多于一条顺序流,这和并行网关一样。
  • 包含网关的功能是基于进入和外出顺序流的:
  • 分支:所有外出顺序流的条件都会被解析,结果为true的顺序流会以并行方式继续执行,会为每个顺序流创建一个分支。
  • 汇聚:所有并行分支到达包含网关,会进入等待状态,直到每个包含流程的进入顺序流的分支都到达。这是和并行网关的最大不同。换言之,包含网关只会等待被选中执行了的进入顺序流。在汇聚之后,流程会穿过包含网关继续执行。

2.3.2 流程定义

  • 企业体检流程,公司全体员工进行常规项检查、抽血化验,公司管理层除常规检查和抽血化验还要进行增加项检查。

包含网关

posted @ 2020-09-13 09:58  许大仙  阅读(1573)  评论(0编辑  收藏  举报