標籤:
使用工作流程的時候必定會附上一些變數。例如,請假的時候有填寫請假理由,天數等等。可以用以下代碼實現
public void setVariables(){ /**與任務(正在執行)*/ TaskService taskService = processEngine.getTaskService(); //任務ID String taskId = "1904"; /**一:設定流程變數,使用基礎資料型別 (Elementary Data Type)*/ //taskService.setVariableLocal(taskId, "請假天數", 3);//與任務ID綁定 local變數與任務id綁定,當進行到下一步的時候該變數在正在執行不再出現 在act_hi_varinst中還存在 //taskService.setVariable(taskId, "請假日期", new Date()); //taskService.setVariable(taskId, "請假原因", "回家探親"); //System.out.println("設定流程變數成功!"); /**二:設定流程變數,使用javabean類型*/ /** * 當一個javabean(實現序號)放置到流程變數中,要求javabean的屬性不能再發生變化 * * 如果發生變化,再擷取的時候,拋出異常 * * 解決方案:在Person對象中添加: * private static final long serialVersionUID = 6757393795687480331L; * 同時實現Serializable * */ Person p = new Person(); p.setId(20); p.setName("翠花"); p.setEducation("翠花123"); taskService.setVariable(taskId, "人員資訊(添加固定版本)", p); System.out.println("設定流程變數成功!"); }
而獲得流程變數則用以下代碼實現
public void getVariables() { TaskService taskService = processEngine.getTaskService(); String taskId = "2102"; /**Integer days = (Integer) taskService.getVariable(taskId, "請假天數"); Date date = (Date) taskService.getVariable(taskId, "請假日期"); String resean = (String) taskService.getVariable(taskId, "請假原因"); System.out.println("請假天數:"+days); System.out.println("請假日期:"+date); System.out.println("請假原因:"+resean);**/ /**二:擷取流程變數,使用javabean類型*/ Person p = (Person)taskService.getVariable(taskId, "人員資訊(添加固定版本)"); System.out.println(p.getId()+" "+p.getName()+" "+p.getEducation()); }
可以看到,這裡有兩種方法設定和擷取流程變數,第一種使用資料基本類型,第二種則是使用javabean。兩者實現效果一樣,但是在資料庫中儲存的位置不相同。前者可以在act_ru_variable中查詢到相關的變數,後者則在act_ge_bytearray中查詢。此外,使用javabean設定變數的時候,如果在流程啟動後修改了bean則會拋出異常,解決方案如下
package d_processVariable;import java.io.Serializable;public class Person implements Serializable{ private static final long serialVersionUID = 6757393795687480331L;//固定版本 private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; private String education; public String getEducation() { return education; } public void setEducation(String education) { this.education = education; }}
只要把bean設定固定版本,即可解決該問題。
設定的函數有兩種,一種是setVariableLocal()另一種是setVariable。前者會把變數和taskId綁定,這樣一來,一但流程進入下一步,該變數不可見。只能在act_hi_varinst表中查詢到變數。在實際過程中可以根據需要選擇
Activiti 變數設定