一、流程變量
1.1 概念
- 如果,當流程走到"學生請假"這個任務節點的時候,此時可以用TaskService設置流程變量,變量值包含請假人、請假時間、請假理由等信息,這些信息存在表act_ru_variable中,當"學生請假"這個任務節點完成之后,“班長審批”這個任務節點可以看到"學生請假"任務節點設置的所有流程變量值(請假人等信息),“班主任審批”這個任務節點也可以看到"學生請假"任務節點設置的所有流程變量值。 但是當整個流程走完的時候,運行時的表(以act_ru開頭的表)數據清空,自然也就讀取不到。
1.2 設置流程變量
- 本章案例是執行完學生請假流程中的 1.2 啟動流程實例之后在執行,此時流程走到下圖紅圈這個任務節點,設置請假人信息。
/** * 設置流程變量數據 */ @Test public void setVariableValues(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="15004"; taskService.setVariable(taskId, "days", 2); taskService.setVariable(taskId, "date", new Date()); taskService.setVariable(taskId, "reason", "發燒"); Student student=new Student(); student.setId(1); student.setName("張三"); taskService.setVariable(taskId, "student", student); // 存序列化對象 }
- taskId="15004"; 是由“學生請假”這個任務節點的id
- 注意:Student 必須序列化。
- 結果如下圖,說明流程變量的值已經存入表中:
1.3 獲取流程變量
- 上述步驟在“學生請假”任務節點設置流程變量之后,接着運行學生請假流程中的 1.4 張三完成學生請假任務,即流程走到下圖,獲取流程變量值。
/** * 獲取流程變量數據 */ @Test public void getVariableValues(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="20002"; Integer days=(Integer) taskService.getVariable(taskId, "days"); Date date=(Date) taskService.getVariable(taskId, "date"); String reason=(String) taskService.getVariable(taskId, "reason"); Student student=(Student) taskService.getVariable(taskId, "student"); System.out.println("請假天數:"+days); System.out.println("請假日期:"+date); System.out.println("請假原因:"+reason); System.out.println("請假對象:"+student.getId()+","+student.getName()); }
-
taskId="20002"; 20002是“班長審批”任務的id
- 結果如下圖,成功獲取到流程變量。
1.4 另一種方法(將數據存在map中)設置流程變量
/** * 設置流程變量數據 */ @Test public void setVariableValues2(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="15004"; Student student=new Student(); student.setId(1); student.setName("張三"); Map<String, Object> variables=new HashMap<String,Object>(); variables.put("days", 2); variables.put("date", new Date()); variables.put("reason", "發燒"); variables.put("student", student); taskService.setVariables(taskId, variables); }
/** * 獲取流程變量數據 */ @Test public void getVariableValues2(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="20002"; Map<String,Object> variables=taskService.getVariables(taskId); Integer days=(Integer) variables.get("days"); Date date=(Date) variables.get("date"); String reason=(String) variables.get("reason"); Student student=(Student)variables.get("student"); System.out.println("請假天數:"+days); System.out.println("請假日期:"+date); System.out.println("請假原因:"+reason); System.out.println("請假對象:"+student.getId()+","+student.getName()); }
二、流程局部變量
- 局部流程變量只能在該任務節點中設置和獲取。如下圖,“學生請假”任務節點設置的流程局部變量只能在該“學生請假”節點獲取,“班長審批”節點或者"班主任審批"節點獲取就為空。
- 設置局部流程變量
TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="72504"; taskService.setVariableLocal(taskId,"date", new Date());
- 獲取局部流程變量
TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="80002"; Date date=(Date) taskService.getVariableLocal(taskId, "date");