1. 运行时变量
流程图
(1)全局变量
测试
package com.sw.camundademo;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.Deployment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
/**
* @Author Suaxi
* @Date 2024/7/4 21:31
* @Description
*/
@SpringBootTest
public class GlobalProcessVariablesTest {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Test
public void deploy() {
Deployment deploy = repositoryService.createDeployment()
.name("请假流程-流程变量-全局变量")
.addClasspathResource("flow/05.流程变量-全局变量.bpmn")
.deploy();
System.out.println(deploy.getId());
}
@Test
public void startProcess() {
String processId = "Process_17lc2nv:1:f92e9184-3a09-11ef-b11b-a8a1592cf182";
Map<String, Object> variables = new HashMap<>();
variables.put("name", "孙笑川");
variables.put("age", 33);
variables.put("flag", true);
runtimeService.startProcessInstanceById(processId, variables);
}
/**
* 获取运行时流程变量
*/
@Test
public void getVariable() {
Map<String, Object> variables = runtimeService.getVariables("649c18cb-3a0a-11ef-a405-a8a1592cf182");
for (String key : variables.keySet()) {
Object obj = variables.get(key);
System.out.println(key + " - " + obj);
}
}
/**
* 修改运行时流程变量
*/
@Test
public void updateVariable() {
String executionId = "649c18cb-3a0a-11ef-a405-a8a1592cf182";
runtimeService.setVariable(executionId, "flag", false);
runtimeService.setVariable(executionId, "address", "四川成都");
}
@Test
public void completeTask() {
String taskId = "649e88d4-3a0a-11ef-a405-a8a1592cf182";
Map<String, Object> variables = taskService.getVariables(taskId);
variables.put("sex", "男");
taskService.complete(taskId, variables);
}
}
(2)局部变量
概念:仅针对在一个任务或一个执行实例中,范围没有流程实例大,不同任务/执行实例的本地变量作用域互不影响,所以不同任务/执行实例的本地变量名可以相同,也可以跟全局变量名相同。
测试
package com.sw.camundademo;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.Deployment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
import java.util.Map;
/**
* @Author Suaxi
* @Date 2024/7/5 22:31
* @Description
*/
@SpringBootTest
public class PartProcessVariablesTest {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Test
public void deploy() {
Deployment deploy = repositoryService.createDeployment()
.name("请假流程-流程变量-局部变量")
.addClasspathResource("flow/06.流程变量-局部变量.bpmn")
.deploy();
System.out.println(deploy.getId());
}
@Test
public void startProcess() {
String processId = "Process_1qnwbgx:1:b64186ea-3ad4-11ef-8a7c-a8a1592cf182";
Map<String, Object> variables = new HashMap<>();
variables.put("name", "孙笑川1");
variables.put("age", 33);
variables.put("flag", true);
runtimeService.startProcessInstanceById(processId, variables);
}
/**
* 获取运行时流程变量
*/
@Test
public void getVariable() {
Map<String, Object> variables = runtimeService.getVariables("57b19eeb-3ad8-11ef-a150-a8a1592cf182");
for (String key : variables.keySet()) {
Object obj = variables.get(key);
System.out.println(key + " - " + obj);
}
}
/**
* 通过 runtimeService 设置 local 流程变量
*/
@Test
public void setLocalVariableByExecutionId() {
String executionId = "57b19eeb-3ad8-11ef-a150-a8a1592cf182";
runtimeService.setVariableLocal(executionId, "no", "0001");
runtimeService.setVariableLocal(executionId, "flag", false);
}
/**
* 通过 taskService 设置 local 流程变量
*/
@Test
public void setLocalVariableByTaskId() {
String taskId = "57b43704-3ad8-11ef-a150-a8a1592cf182";
taskService.setVariableLocal(taskId, "no", "0001");
taskService.setVariableLocal(taskId, "flag", false);
}
@Test
public void completeTask() {
String taskId = "57b43704-3ad8-11ef-a150-a8a1592cf182";
Map<String, Object> variables = taskService.getVariables(taskId);
for (String key : variables.keySet()) {
System.out.println(key + " - " + variables.get(key));
}
taskService.complete(taskId);
}
}
注:
- 通过 runtimeService 设置的本地流程变量的作用域与 executionId绑定,在该流程中有效
- 通过 taskService 设置的本地流程变量的作用域与 taskId绑定,作用域仅在当前 task 的生命周期中
2. 历史变量
历史变量存于 act_hi_varinst
表中,在流程启动时,流程变量同时存入 act_ru_variable
与 act_hi_varinst
表,流程结束后,表 act_hi_varinst
中的数据持久化保存。
@Test
public void getHistoryVariables() {
List<HistoricDetail> historicDetailList = historyService.createHistoricDetailQuery()
.processInstanceId("649c18cb-3a0a-11ef-a405-a8a1592cf182")
.list();
if (historicDetailList != null && historicDetailList.size() > 0) {
for (HistoricDetail historicDetail : historicDetailList) {
System.out.println("historicDetail.getExecutionId() = " + historicDetail.getExecutionId());
System.out.println("historicDetail.getTaskId() = " + historicDetail.getTaskId());
}
System.out.println("================================");
}
List<HistoricVariableInstance> historicVariableInstanceList = historyService.createHistoricVariableInstanceQuery()
.processInstanceId("649c18cb-3a0a-11ef-a405-a8a1592cf182")
.list();
if (historicVariableInstanceList != null && historicVariableInstanceList.size() > 0) {
for (HistoricVariableInstance historicVariableInstance : historicVariableInstanceList) {
System.out.println(historicVariableInstance.getName() + ":" + historicVariableInstance.getValue());
}
}
}
评论 (0)