上一篇文章我們將流程實例的啟動與查詢,任務的辦理查詢都進行了介紹,我們這篇文章來介紹activiti中的流程變量。
流程變量與我們平常理解的變量是一樣的,只不過是用在了我們activiti中,所以稱為流程變量,流程變量在整個工作流扮演著很重要的角色。
例如,請假流程中有請假天數、請假原因等一些參數都是流程變量使用的范圍,流程變量的作用域范圍是只對應一個流程實例。也就是說各個流程實例的流程變量是不互相影響的。流程實例結束完成以后流程變量還保存在數據庫中(存放在流程變量的歷史表中)。
如圖:
關于流程實例的例子,我們先來看下流程圖的PRocessVariables.bpmn的配置文件:
[html] view plain copy<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="processVariables" name="processVariables【流程請假】" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <endEvent id="endevent1" name="End"></endEvent> <userTask id="usertask1" name="提交申請"></userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <userTask id="usertask2" name="審批【總經理】" activiti:assignee="王二"></userTask> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_processVariables"> <bpmndi:BPMNPlane bpmnElement="processVariables" id="BPMNPlane_processVariables"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="90.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="420.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="190.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="300.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="367.0" y="125.0"></omgdi:waypoint> <omgdi:waypoint x="367.0" y="190.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="367.0" y="245.0"></omgdi:waypoint> <omgdi:waypoint x="367.0" y="300.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="367.0" y="355.0"></omgdi:waypoint> <omgdi:waypoint x="367.0" y="420.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions> 一個很簡單的流程圖processVariables.png:
部署流程定義:
[java] view plain copy/** * 部署流程定義(從inputStream) */ @Test public void deploymentProcessDefinition_inputStream() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); InputStream inputStreamBpmn = this.getClass().getResourceAsStream( "/diagrams/processVariables.bpmn"); InputStream inputStreamPng = this.getClass().getResourceAsStream( "/diagrams/processVariables.png"); Deployment deployment = processEngine.getRepositoryService()// 與流程定義和部署對象相關的Service .createDeployment()// 創建一個部署對象 .name("流程定義")// 添加部署名稱 .addInputStream("processVariables.bpmn", inputStreamBpmn)// 使用資源文件的名稱(要求:與資源文件的名稱要一致),和輸入流完成部署 .addInputStream("processVariables.png", inputStreamPng)// 使用資源文件的名稱(要求:與資源文件的名稱要一致),和輸入流完成部署 .deploy();// 完成部署 System.out.println("部署ID:" + deployment.getId()); System.out.println("部署名稱:" + deployment.getName()); } 運行結果:
部署ID:701
部署名稱:流程定義
啟動流程實例:
[java] view plain copy /** * 啟動流程實例 */ @Test public void startProcessInstance() { // 流程定義的key String processDefinitionKey = "processVariables"; ProcessInstance pi = processEngine.getRuntimeService()// 與正在執行的流程實例和執行對象相關的service .startProcessInstanceByKey(processDefinitionKey);// 使用流程定義的key啟動流程實例,key對應processVariables文件中的id的屬性值,使用key值啟動,默認是按照最新版本進行啟動 System.out.println("流程實例ID:" + pi.getId()); System.out.println("流程定義ID:" + pi.getProcessDefinitionId()); System.out.println("流程實例ID" + pi.getProcessInstanceId()); } 運行結果:
流程實例ID:801
流程定義ID:processVariables:1:704
流程實例ID801
查詢任務
[java] view plain copy/** * 查詢任務通過流程實例id */ @Test public void findTask(){ String processInstanceId="801"; List<HistoricTaskInstance> list = processEngine.getHistoryService()//與歷史數據(歷史表)相關的service .createHistoricTaskInstanceQuery()//創建歷史任務實例查詢 .processInstanceId(processInstanceId) .list(); if(list!=null && list.size()>0){ for(HistoricTaskInstance hti:list){ System.out.println(hti.getId()+" "+hti.getName()+" "+hti.getProcessInstanceId()+" "+hti.getStartTime()+" "+hti.getEndTime()+" "+hti.getDurationInMillis()); System.out.println("################################"); } } } 運行結果:
804 提交申請 801 Fri Jun 26 10:55:02 CST2015 null null
################################
關于部署流程定義、啟動流程實例和查詢正在辦理的任務我們前面的文章已經介紹過了,所以我們不再詳細介紹,下面開始我們的設置流程變量,設置流程變量我們這里提供了兩種方式,分別是使用基本數據類型和使用javabean的方法,同意獲取流程變量也是不一樣的:
使用基本數據類型:
設置流程變量
[java] view plain copy/** * 設置流程變量 */ @Test public void setVariables() { // 與任務相關的service,正在執行的service TaskService taskService = processEngine.getTaskService(); // 任務ID String taskId = "804"; // 1.設置流程變量,使用基本數據類型 taskService.setVariable(taskId, "請假天數", 7);// 與任務ID邦德 taskService.setVariable(taskId, "請假日期", new Date()); taskService.setVariableLocal(taskId, "請假原因", "回去探親,一起吃個飯123"); System.out.println("設置流程變量成功!"); }運行結果:
設置流程變量成功!
獲取流程變量
[java] view plain copy/** * 獲取流程變量 */ @Test public void getVariables() { // 與任務(正在執行的service) TaskService taskService = processEngine.getTaskService(); // 任務Id String taskId = "804"; // 1.獲取流程變量,使用基本數據類型 Integer days = (Integer) taskService.getVariable(taskId, "請假天數"); Date date = (Date) taskService.getVariable(taskId, "請假日期"); String reason = (String) taskService.getVariable(taskId, "請假原因"); System.out.println("請假天數:" + days); System.out.println("請假日期:" + date); System.out.println("請假原因:" + reason); } 運行結果:
請假天數:7
請假日期:Fri Jun 2611:07:28 CST 2015
請假原因:回去探親,一起吃個飯123
使用javabean
JavaBean的Person類
[java] view plain copypackage com.tgb; import java.io.Serializable; import java.util.Date; public class Person implements Serializable { private static final long serialVersionUID = 361866001729020143L; //請假天數 private int id; //請假人 private String name; //請假原因 private String note; //請假時間 private Date date; public Date getDate() { return date; } public void setDate() { this.date = new Date(); } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
設置流程變量
[java] view plain copy/** * 設置流程變量 */ @Test public void setVariables() { // 與任務相關的service,正在執行的service TaskService taskService = processEngine.getTaskService(); // 任務ID String taskId = "804"; // 設置流程變量,使用javaBean方法 /** * 當一個javaBean(實現序列號)放置到流程變量中,要求javabean的屬性不能在發生變化 如果發生變化,再獲取時,拋出異常 * * 解決方案:在person對象中添加: private static final long * serialVersionUID="6757393795687480331L"; 同時實現序列號接口 * */ Person p = new Person(); p.setName("翠花"); p.setId(20); p.setDate();; p.setNote("回去探親,一起吃個飯123"); taskService.setVariable(taskId, "人員信息(添加固定版本)", p); System.out.println("設置流程變量成功!"); } 運行結果:
設置流程變量成功!
獲取流程變量
[java] view plain copy/** * 獲取流程變量 */ @Test public void getVariables() { // 與任務(正在執行的service) TaskService taskService = processEngine.getTaskService(); // 任務Id String taskId = "804"; // 2.獲取流程變量,使用javaBean類型 Person p = (Person)taskService.getVariable(taskId, "人員信息(添加固定版本)"); System.out.println(" 請假人: "+p.getName()+" 請假天數: "+p.getId()+" 請假時間:"+ p.getDate()+ " 請假原因: "+p.getNote()); } 運行結果:
請假人: 翠花 請假天數: 20 請假時間:Fri Jun 26 11:13:44 CST 2015 請假原因: 回去探親,一起吃個飯123
查詢歷史流程變量
可以根據變量名稱查詢該變量的所有歷史信息
[java] view plain copy可以根據變量名稱查詢該變量的所有歷史信息 /** * 查詢流程變量的歷史表 */ @Test public void findHistoryProcessVariables(){ List<HistoricVariableInstance> list = processEngine.getHistoryService() .createHistoricVariableInstanceQuery()//創建一個歷史的流程變量查詢對象 .variableName("請假原因") .list(); if (list!=null &&list.size()>0) { for (HistoricVariableInstance hvi : list) { System.out.println(hvi.getId()+" "+hvi.getProcessInstanceId()+" "+hvi.getVariableName() +" "+hvi.getVariableTypeName()+" "+hvi.getValue()); System.out.println("########################################"); } } }流程變量支持的數據類型:
流程變量支持的數據類型包括:TypeName、string、integer、short、long、double、boolean、data、binary、serializable,我們可以看出流程變量支持的包括了大部分封裝類型和Date、String和實現了Serializable接口的類的類型。
我們這篇文章將流程變量的相關知識進行了介紹,除了介紹流程變量的相關定義外還通過具體代碼例子介紹了通過不同方式來設置和獲取流程變量以及流程變量支持的數據類型。
http://blog.csdn.net/zwk626542417/article/details/46648139
新聞熱點
疑難解答