【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量

文章目录

    • 1. 任务变量设置获取
      • 1.1 测试类
      • 1.2 运行
        • 1.2.1 设置变量
        • 1.2.2 获取变量
        • 1.2.3 MAP形式变量设置获取
    • 2. 流程变量设置获取
      • 2.1 测试类
      • 2.2 运行
        • 2.2.1 设置变量
        • 2.2.2 获取变量
        • 2.2.3 MAP形式变量设置获取
    • 3. 启动流程设置流程变量
      • 3.1 测试类
      • 3.2 运行
        • 3.2.1 启动流程
        • 3.2.1 获取变量
    • 4. 完成任务设置流程变量
      • 3.1 测试类
      • 4.2 运行
        • 4.2.1 完成任务
        • 4.2.1 获取变量
    • 参考
    • 源码地址
    • 项目推荐

Activiti中变量类型分为流程变量和任务变量,变量的设置又分基本类型设置,JavaBean类型设置,下面将分别讲解。

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

1. 任务变量设置获取

1.1 测试类

TaskVariableTest.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 TaskVariableTest {

    @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/helloworld.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("helloworld"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @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("85005");
    }


    /**
     * 设置任务变量--直接方式
     */
    @Test
    public void setVariable1() {
        String taskId = "92505";
        // 基本类型
        taskService.setVariable(taskId, "days", 5);
        taskService.setVariable(taskId, "reason", "回家结婚");
        // setVariableLocal设置局部变量,只能在当前任务中得到
        taskService.setVariableLocal(taskId, "date", new Date());

        // 序列化对象
        Leave leave = new Leave();
        leave.setDays(5);
        leave.setReason("相亲");
        taskService.setVariable(taskId, "leave", leave);
    }

    /**
     * 获取任务变量--直接方式
     */
    @Test
    public void getVariable1() {
        String taskId = "92505";
        // 获取基本类型
        Integer days = (Integer) taskService.getVariable(taskId, "days");
        Date date = (Date) taskService.getVariableLocal(taskId, "date");
        String reason = (String) taskService.getVariable(taskId, "reason");
        // 获取对象类型
        Leave leave = (Leave) taskService.getVariable(taskId, "student");

        System.out.println("天数:" + days);
        System.out.println("日期:" + date);
        System.out.println("原因:" + reason);
        System.out.println("对象:" + leave.getDays() + ", " + leave.getReason());
    }

    /**
     * 设置任务变量--MAP方式
     */
    @Test
    public void setVariable2() {
        String taskId = "92505";
        Leave leave = new Leave();
        leave.setDays(5);
        leave.setReason("相亲");
        // 填充map
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("days", 5);
        variables.put("reason", "相亲");
        variables.put("date", new Date());
        variables.put("leave", leave);

        taskService.setVariables(taskId, variables);
    }

    /**
     * 获取任务变量--MAP方式
     */
    @Test
    public void getVariable2() {
        String taskId = "92505";
        // 获取变量
        Map<String, Object> variables = taskService.getVariables(taskId);
        Integer days = (Integer) variables.get("days");
        String reason = (String) variables.get("reason");
        Date date = (Date) variables.get("date");
        Leave leave = (Leave) variables.get("leave");

        System.out.println("天数:" + days);
        System.out.println("日期:" + date);
        System.out.println("原因:" + reason);
        System.out.println("对象:" + leave.getDays() + "," + leave.getReason());
    }
}

1.2 运行

1.2.1 设置变量

运行setVariable1()

效果:
数据库表变更ACT_RU_VARIABLE
【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第1张图片
局部变量会设置task_id,表示只能在当前任务中获取

1.2.2 获取变量

运行getVariable1()

效果:
【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第2张图片

1.2.3 MAP形式变量设置获取

运行setVariable2(), getVariable2()

效果:
同1.2.1 , 1.2.2

2. 流程变量设置获取

2.1 测试类

RuntimeVariableTest.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 RuntimeVariableTest {

    @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/helloworld.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("helloworld"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @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("85005");
    }


    /**
     * 设置流程变量--直接方式
     */
    @Test
    public void setVariable1() {
        String executionId = "92501";
        // 基本类型
        runtimeService.setVariable(executionId, "exe_days", 5);
        runtimeService.setVariable(executionId, "exe_reason", "回家结婚");
        // setVariableLocal设置局部变量,只能在当前执行流中得到
        runtimeService.setVariableLocal(executionId, "exe_date", new Date());

        // 序列化对象
        Leave leave = new Leave();
        leave.setDays(5);
        leave.setReason("相亲");
        runtimeService.setVariable(executionId, "exe_leave", leave);
    }

    /**
     * 获取流程变量--直接方式
     */
    @Test
    public void getVariable1() {
        String executionId = "92501";
        // 获取基本类型
        Integer days = (Integer) runtimeService.getVariable(executionId, "exe_days");
        Date date = (Date) runtimeService.getVariableLocal(executionId, "exe_date");
        String reason = (String) runtimeService.getVariable(executionId, "exe_reason");
        // 获取对象类型
        Leave leave = (Leave) runtimeService.getVariable(executionId, "exe_leave");

        System.out.println("天数:" + days);
        System.out.println("日期:" + date);
        System.out.println("原因:" + reason);
        System.out.println("对象:" + leave.getDays() + ", " + leave.getReason());
    }

    /**
     * 设置流程变量--MAP方式
     */
    @Test
    public void setVariable2() {
        String executionId = "92501";
        Leave leave = new Leave();
        leave.setDays(5);
        leave.setReason("相亲");
        // 填充map
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("exe_days", 5);
        variables.put("exe_reason", "相亲");
        variables.put("exe_date", new Date());
        variables.put("exe_leave", leave);

        runtimeService.setVariables(executionId, variables);
    }

    /**
     * 获取流程变量--MAP方式
     */
    @Test
    public void getVariable2() {
        String executionId = "92501";
        // 获取变量
        Map<String, Object> variables = runtimeService.getVariables(executionId);
        Integer days = (Integer) variables.get("exe_days");
        String reason = (String) variables.get("exe_reason");
        Date date = (Date) variables.get("exe_date");
        Leave leave = (Leave) variables.get("exe_leave");

        System.out.println("天数:" + days);
        System.out.println("日期:" + date);
        System.out.println("原因:" + reason);
        System.out.println("对象:" + leave.getDays() + "," + leave.getReason());
    }
}

2.2 运行

2.2.1 设置变量

运行setVariable1()

效果:
数据库表变更ACT_RU_VARIABLE
【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第3张图片
局部变量表示只能在当前执行流中获取,虽然功能好使,但是我并没有在数据库中看出来有什么异同

2.2.2 获取变量

运行getVariable1()
把executionId 设置成用户任务执行流,因为Activiti6用户任务默认启动两条执行流,92501是主执行流,92502是用户任务执行流。
String executionId = “92502”;

效果:
【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第4张图片

2.2.3 MAP形式变量设置获取

运行setVariable2(), getVariable2()

效果:
同2.2.1 , 2.2.2

3. 启动流程设置流程变量

3.1 测试类

package com.it.cloud.activiti.variable;

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 StartVariableTest {

    @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/helloworld.bpmn20.xml") // 加载流程资源文件
                .name("helloworld流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("var_start", "var_start");
        // 流程定义表的KEY字段值, 启动变量
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("helloworld", variables);
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 获取流程变量
     */
    @Test
    public void getVariable() {
        String executionId = "112501";
        String params = (String) runtimeService.getVariable(executionId, "var_start");

        System.out.println("启动流程变量:" + params);
    }
}

3.2 运行

3.2.1 启动流程

运行start()

效果:
数据库表变更ACT_RU_VARIABLE
【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第5张图片

3.2.1 获取变量

运行getVariable()

效果:

【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第6张图片

4. 完成任务设置流程变量

3.1 测试类

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.history.HistoricVariableInstance;
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 CompleteTaskVariableTest {

    @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/helloworld.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("helloworld"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }

    /**
     * 查看任务
     */
    @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("85005");
    }

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

    /**
     * 获取流程变量
     */
    @Test
    public void getVariable() {
        List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
                .executionId("92501")
                .variableName("var_complete")
                .list();

        if (list != null && list.size() > 0) {
            for (HistoricVariableInstance hiv : list) {
                System.out.println(hiv.getVariableName() + "=" + hiv.getValue());
            }
        }
    }
}

4.2 运行

4.2.1 完成任务

运行completeTask2()

效果:
数据库表变更ACT_HI_VARINST
在这里插入图片描述

4.2.1 获取变量

运行getVariable()

效果:
【弄nèng - Activiti6】Activiti6入门篇(三)—— 流程变量_第7张图片


参考

1.http://www.java1234.com/ activiti系列视频

源码地址

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

项目推荐

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

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