介绍:在启动流程后,每个流程实例都会有执行流(存储在act_ru_execution表中)。实例都有主执行流,没有父id的执行流是流程实例,其后如果流程中有一个分支则有一个子执行流,分支和子执行流一一对应。对执行流可以设置变量,设置子执行流的变量为本地(临时)变量(自在当前执行流有效),设置主执行流的变量为全局变量。
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
//发布资源
Deployment deployment = repositoryService.createDeployment().addClasspathResource("single.bpmn").deploy();
//获取流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//获取流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
//打印流程实例id,即主执行流。结果:97505
System.out.println(processInstance.getId());
//关闭
processEngine.close();
System.exit(0);
概述:
测试:
//获取流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//获取流程实例,此处的第二个参数对应的是数据库act_ru_execution的BUSINESS_KEY_
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), "abc");
概述: 对执行流可以设置本地变量和全局变量。本地变量会在当前执行流有效,如果当前执行流complete,则数据库中的本地变量会被删除,无法再使用。全局变量是与主执行流绑定的,在当前流程实例中会一直存在。测试如下:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
TaskService taskService = processEngine.getTaskService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("scope.bpmn").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
System.out.println("流程实例id:" processInstance.getId());
System.out.println("========================================================");
//查询任务,首次查询时两个任务是并行的,所有会有两个任务
List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
taskList.forEach(tempTask-> {
//首先查询执行流
Execution execution = runtimeService.createExecutionQuery().executionId(tempTask.getExecutionId()).singleResult();
if ("TaskA".equals(tempTask.getName())){
//给执行流A设置本地变量,即为在当前执行流执行.如果当前执行流complete,则该变量会被删除。绑定在执行流上的
runtimeService.setVariableLocal(execution.getId(),"taskVarA","varA");
System.out.println("给执行流TaskA设置本地变量 varA 完成");
}else {
//给执行流B设置全局变量,在当前实例都可以获取到,是直接绑定到流程实例上的
runtimeService.setVariable(execution.getId(),"taskVarB","varB");
runtimeService.setVariable(execution.getId(),"taskVarB2","varB2");
System.out.println("给执行流TaskB设置全局变量 varB和varB2 完成");
}
});
//临时暂停30s,查看数据库变化
Thread.sleep(30000);
//将TaskA和TaskB完成,到达TaskC可以进行参数查询
taskList.forEach(task -> taskService.complete(task.getId()));
System.out.println("========================================================");
//进行参数查询
Task taskC = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
System.out.println("当前任务:" taskC.getName() ". 获取执行流参数如下:");
System.out.println(runtimeService.getVariable(taskC.getExecutionId(), "taskVarA"));
System.out.println(runtimeService.getVariable(taskC.getExecutionId(), "taskVarB"));
processEngine.close();
System.exit(0);
变量如下(act_ru_variable表),其中全局变量是绑定的EXECUTION_ID_是主执行流:
变量,可以看到本地变量被删除了,只有全局变量被保存下来: