一 代码与数据库变化
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: SetVariableLocal.java
* Author: cakin
* Date: 2020/4/21
* Description: 流程参数作用域
*/
package com.syc.activiti;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
/**
* @className: SetVariableLocal
* @description: 流程参数作用域
* @date: 2020/4/21
* @author: cakin
*/
public class SetVariableLocal {
public static void main( String[] args ) {
// 创建流程引擎
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 得到流程存储服务实例
RepositoryService repositoryService = engine.getRepositoryService();
// 得到运行时服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 得到任务
TaskService taskService = engine.getTaskService();
// 部署流程描述文件
repositoryService.createDeployment()
.addClasspathResource("bpmn/localVariable.bpmn20.xml").deploy();
//启动流程
ProcessInstance pi = runtimeService.startProcessInstanceByKey("vacationRequest");
//查询全部的任务,得到相应的执行流,设置不同的参数
List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
/************************************************************************************** 当执行到这里
mysql> select * from act_ru_execution;
ERROR 1046 (3D000): No database selected
mysql> use act;
Database changed
mysql> select ID_, REV_,PROC_INST_ID_ ,BUSINESS_KEY_ ,PARENT_ID_ ,PROC_DEF_ID_ ,SUPER_EXEC_,ROOT_PROC_INST_ID_ ,ACT_ID_ from act_ru_execution;
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| ID_ | REV_ | PROC_INST_ID_ | BUSINESS_KEY_ | PARENT_ID_ | PROC_DEF_ID_ | SUPER_EXEC_ | ROOT_PROC_INST_ID_ | ACT_ID_ |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| 182505 | 1 | 182505 | NULL | NULL | vacationRequest:7:182504 | NULL | 182505 | NULL |
| 182506 | 1 | 182505 | NULL | 182505 | vacationRequest:7:182504 | NULL | 182505 | usertask1 |
| 182509 | 1 | 182505 | NULL | 182505 | vacationRequest:7:182504 | NULL | 182505 | usertask2 |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
mysql> select ID_ ,REV_, EXECUTION_ID_, PROC_INST_ID_ , PROC_DEF_ID_, NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , TASK_DEF_KEY_ , OWNER_, ASSIGNEE_ , DELEGATION_ , PRIORITY_ ,CREATE_TIME_ from act_ru_task;
+--------+------+---------------+---------------+--------------------------+---------------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| ID_ | REV_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | TASK_DEF_KEY_ | OWNER_ | ASSIGNEE_ | DELEGATION_ | PRIORITY_ | CREATE_TIME_ |
+--------+------+---------------+---------------+--------------------------+---------------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| 182511 | 1 | 182506 | 182505 | vacationRequest:7:182504 | Manager Audit | NULL | NULL | usertask1 | NULL | NULL | NULL | 50 | 2020-04-21 21:00:07.638 |
| 182513 | 1 | 182509 | 182505 | vacationRequest:7:182504 | HR Audit | NULL | NULL | usertask2 | NULL | NULL | NULL | 50 | 2020-04-21 21:00:07.703 |
+--------+------+---------------+---------------+--------------------------+---------------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
2 rows in set (0.00 sec)
mysql> select * from act_ru_variable;
Empty set (0.00 sec)
********************************************************************************/
for (Task task : tasks) {
Execution exe = runtimeService.createExecutionQuery()
.executionId(task.getExecutionId()).singleResult();
if ("Manager Audit".equals(task.getName())) {
//经理审核节点,设置Local参数
runtimeService.setVariableLocal(exe.getId(), "managerVar", "manager var");
} else {
//HR审核节点,设置全局参数
runtimeService.setVariable(exe.getId(), "hrVar", "hr var");
}
}
/************************************************************************************** 当执行到这里
mysql> select * from act_ru_execution;
ERROR 1046 (3D000): No database selected
mysql> use act;
Database changed
mysql> select ID_, REV_,PROC_INST_ID_ ,BUSINESS_KEY_ ,PARENT_ID_ ,PROC_DEF_ID_ ,SUPER_EXEC_,ROOT_PROC_INST_ID_ ,ACT_ID_ from act_ru_execution;
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| ID_ | REV_ | PROC_INST_ID_ | BUSINESS_KEY_ | PARENT_ID_ | PROC_DEF_ID_ | SUPER_EXEC_ | ROOT_PROC_INST_ID_ | ACT_ID_ |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| 182505 | 1 | 182505 | NULL | NULL | vacationRequest:7:182504 | NULL | 182505 | NULL |
| 182506 | 1 | 182505 | NULL | 182505 | vacationRequest:7:182504 | NULL | 182505 | usertask1 |
| 182509 | 1 | 182505 | NULL | 182505 | vacationRequest:7:182504 | NULL | 182505 | usertask2 |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
mysql> select ID_ ,REV_, EXECUTION_ID_, PROC_INST_ID_ , PROC_DEF_ID_, NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , TASK_DEF_KEY_ , OWNER_, ASSIGNEE_ , DELEGATION_ , PRIORITY_ ,CREATE_TIME_ from act_ru_task;
+--------+------+---------------+---------------+--------------------------+---------------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| ID_ | REV_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | TASK_DEF_KEY_ | OWNER_ | ASSIGNEE_ | DELEGATION_ | PRIORITY_ | CREATE_TIME_ |
+--------+------+---------------+---------------+--------------------------+---------------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| 182511 | 1 | 182506 | 182505 | vacationRequest:7:182504 | Manager Audit | NULL | NULL | usertask1 | NULL | NULL | NULL | 50 | 2020-04-21 21:00:07.638 |
| 182513 | 1 | 182509 | 182505 | vacationRequest:7:182504 | HR Audit | NULL | NULL | usertask2 | NULL | NULL | NULL | 50 | 2020-04-21 21:00:07.703 |
+--------+------+---------------+---------------+--------------------------+---------------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
2 rows in set (0.00 sec)
mysql> select * from act_ru_variable;
Empty set (0.00 sec)
********************************************************************************/
//两个执行流时输出参数
for (Task task : tasks) {
Execution exe = runtimeService.createExecutionQuery()
.executionId(task.getExecutionId()).singleResult();
if ("Manager Audit".equals(task.getName())) {
System.out.println("使用getVariableLocal方法获取经理参数:"
+ runtimeService.getVariableLocal(exe.getId(), "managerVar"));
System.out.println("使用getVariable方法获取经理参数:"
+ runtimeService.getVariableLocal(exe.getId(), "managerVar"));
} else {
System.out.println("使用getVariableLocal方法获取HR参数:"
+ runtimeService.getVariableLocal(exe.getId(), "hrVar"));
System.out.println("使用getVariable方法获取HR参数:"
+ runtimeService.getVariable(exe.getId(), "hrVar"));
}
}
//完成任务
for (Task task : tasks) {
taskService.complete(task.getId());
}
System.out.println("======== 完成流程分支后 ========");
/************************************************************************************** 当执行到这里
mysql> select ID_, REV_,PROC_INST_ID_ ,BUSINESS_KEY_ ,PARENT_ID_ ,PROC_DEF_ID_ ,SUPER_EXEC_,ROOT_PROC_INST_ID_ ,ACT_ID_ from act_ru_execution;
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| ID_ | REV_ | PROC_INST_ID_ | BUSINESS_KEY_ | PARENT_ID_ | PROC_DEF_ID_ | SUPER_EXEC_ | ROOT_PROC_INST_ID_ | ACT_ID_ |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| 182505 | 3 | 182505 | NULL | NULL | vacationRequest:7:182504 | NULL | 182505 | NULL |
| 182509 | 3 | 182505 | NULL | 182505 | vacationRequest:7:182504 | NULL | 182505 | usertask3 |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
2 rows in set (0.00 sec)
mysql> select ID_ ,REV_, EXECUTION_ID_, PROC_INST_ID_ , PROC_DEF_ID_, NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , TASK_DEF_KEY_ , OWNER_, ASSIGNEE_ , DELEGATION_ , PRIORITY_ ,CREATE_TIME_ from act_ru_task;
+--------+------+---------------+---------------+--------------------------+----------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| ID_ | REV_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | TASK_DEF_KEY_ | OWNER_ | ASSIGNEE_ | DELEGATION_ | PRIORITY_ | CREATE_TIME_ |
+--------+------+---------------+---------------+--------------------------+----------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| 182519 | 1 | 182509 | 182505 | vacationRequest:7:182504 | End Task | NULL | NULL | usertask3 | NULL | NULL | NULL | 50 | 2020-04-21 21:15:49.497 |
+--------+------+---------------+---------------+--------------------------+----------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
1 row in set (0.00 sec)
mysql> select * from act_ru_variable;
+--------+------+--------+-------+---------------+---------------+----------+---------------+---------+-------+--------+--------+
| ID_ | REV_ | TYPE_ | NAME_ | EXECUTION_ID_ | PROC_INST_ID_ | TASK_ID_ | BYTEARRAY_ID_ | DOUBLE_ | LONG_ | TEXT_ | TEXT2_ |
+--------+------+--------+-------+---------------+---------------+----------+---------------+---------+-------+--------+--------+
| 182515 | 1 | string | hrVar | 182505 | 182505 | NULL | NULL | NULL | NULL | hr var | NULL |
+--------+------+--------+-------+---------------+---------------+----------+---------------+---------+-------+--------+--------+
1 row in set (0.00 sec)
********************************************************************************/
//重新查找流程任务
tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
for (Task task : tasks) {
System.out.println("当前流程节点:" + task.getName());
Execution exe = runtimeService.createExecutionQuery()
.executionId(task.getExecutionId()).singleResult();
System.out.println("经理参数:" + runtimeService.getVariable(exe.getId(), "managerVar"));
System.out.println("HR参数:" + runtimeService.getVariable(exe.getId(), "hrVar"));
}
}
/************************************************************************************** 当执行到这里
mysql> select ID_, REV_,PROC_INST_ID_ ,BUSINESS_KEY_ ,PARENT_ID_ ,PROC_DEF_ID_ ,SUPER_EXEC_,ROOT_PROC_INST_ID_ ,ACT_ID_ from act_ru_execution;
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| ID_ | REV_ | PROC_INST_ID_ | BUSINESS_KEY_ | PARENT_ID_ | PROC_DEF_ID_ | SUPER_EXEC_ | ROOT_PROC_INST_ID_ | ACT_ID_ |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
| 182505 | 3 | 182505 | NULL | NULL | vacationRequest:7:182504 | NULL | 182505 | NULL |
| 182509 | 3 | 182505 | NULL | 182505 | vacationRequest:7:182504 | NULL | 182505 | usertask3 |
+--------+------+---------------+---------------+------------+--------------------------+-------------+--------------------+-----------+
2 rows in set (0.00 sec)
mysql> select ID_ ,REV_, EXECUTION_ID_, PROC_INST_ID_ , PROC_DEF_ID_, NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , TASK_DEF_KEY_ , OWNER_, ASSIGNEE_ , DELEGATION_ , PRIORITY_ ,CREATE_TIME_ from act_ru_task;
+--------+------+---------------+---------------+--------------------------+----------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| ID_ | REV_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | TASK_DEF_KEY_ | OWNER_ | ASSIGNEE_ | DELEGATION_ | PRIORITY_ | CREATE_TIME_ |
+--------+------+---------------+---------------+--------------------------+----------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
| 182519 | 1 | 182509 | 182505 | vacationRequest:7:182504 | End Task | NULL | NULL | usertask3 | NULL | NULL | NULL | 50 | 2020-04-21 21:15:49.497 |
+--------+------+---------------+---------------+--------------------------+----------+-----------------+--------------+---------------+--------+-----------+-------------+-----------+-------------------------+
1 row in set (0.00 sec)
mysql> select * from act_ru_variable;
+--------+------+--------+-------+---------------+---------------+----------+---------------+---------+-------+--------+--------+
| ID_ | REV_ | TYPE_ | NAME_ | EXECUTION_ID_ | PROC_INST_ID_ | TASK_ID_ | BYTEARRAY_ID_ | DOUBLE_ | LONG_ | TEXT_ | TEXT2_ |
+--------+------+--------+-------+---------------+---------------+----------+---------------+---------+-------+--------+--------+
| 182515 | 1 | string | hrVar | 182505 | 182505 | NULL | NULL | NULL | NULL | hr var | NULL |
+--------+------+--------+-------+---------------+---------------+----------+---------------+---------+-------+--------+--------+
1 row in set (0.00 sec)
********************************************************************************/
}
二 执行结果
使用getVariableLocal方法获取经理参数:manager var
使用getVariable方法获取经理参数:manager var
使用getVariableLocal方法获取HR参数:null
使用getVariable方法获取HR参数:hr var
======== 完成流程分支后 ========
当前流程节点:End Task
经理参数:null
HR参数:hr var
来源:oschina
链接:https://my.oschina.net/u/4287100/blog/3300972