repositoryService.createDeployment().name("全局局部变量流程").addClasspathResource("bpmn/global_local_variable.bpmn").deploy();
identityService.setAuthenticatedUserId("huihui");
VariableMap startVariables = Variables.createVariables().putValue("start", "start");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("GlobaAndLocalProcess", startVariables);
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
taskService.setVariableLocal(task.getId(), "usertask1-local", "value1");
taskService.complete(task.getId(), Variables.createVariables().putValue("usertask1-global", "value2"));
task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
taskService.setVariableLocal(task.getId(), "usertask2-local", "value3");
Map<String, Object> variables = taskService.getVariables(task.getId());
Map<String, Object> variablesLocal = taskService.getVariablesLocal(task.getId());
- taskService.getVariables(task.getId()):获取所有任务的全局变量和当前任务的局部变量。
- taskService.getVariablesLocal(task.getId()):获取当前认为的局部变量,局部变量的作用域只在当前节点,超过当前节点就获取不到。