工作流學習——Activiti流程變量五步曲

轉載:https://blog.csdn.net/zwk626542417/article/details/46648139

一、前言

 

    上一篇文章我們將流程實例的啓動與查詢,任務的辦理查詢都進行了介紹,我們這篇文章來介紹activiti中的流程變量。

 

二、正文

 

    流程變量與我們平常理解的變量是一樣的,只不過是用在了我們activiti中,所以稱爲流程變量,流程變量在整個工作流扮演着很重要的角色。

 

    例如,請假流程中有請假天數、請假原因等一些參數都是流程變量使用的範圍,流程變量的作用域範圍是只對應一個流程實例。也就是說各個流程實例的流程變量是不互相影響的。流程實例結束完成以後流程變量還保存在數據庫中(存放在流程變量的歷史表中)。

 

如圖:


 

 

    關於流程實例的例子,我們先來看下流程圖的processVariables.bpmn的配置文件:

 

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <process id="processVariables" name="processVariables【流程請假】" isExecutable="true">  
  4.     <startEvent id="startevent1" name="Start"></startEvent>  
  5.     <endEvent id="endevent1" name="End"></endEvent>  
  6.     <userTask id="usertask1" name="提交申請"></userTask>  
  7.     <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>  
  8.     <userTask id="usertask2" name="審批【總經理】" activiti:assignee="王二"></userTask>  
  9.     <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>  
  10.     <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>  
  11.   </process>  
  12.   <bpmndi:BPMNDiagram id="BPMNDiagram_processVariables">  
  13.     <bpmndi:BPMNPlane bpmnElement="processVariables" id="BPMNPlane_processVariables">  
  14.       <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">  
  15.         <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="90.0"></omgdc:Bounds>  
  16.       </bpmndi:BPMNShape>  
  17.       <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">  
  18.         <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="420.0"></omgdc:Bounds>  
  19.       </bpmndi:BPMNShape>  
  20.       <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">  
  21.         <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="190.0"></omgdc:Bounds>  
  22.       </bpmndi:BPMNShape>  
  23.       <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">  
  24.         <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="300.0"></omgdc:Bounds>  
  25.       </bpmndi:BPMNShape>  
  26.       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">  
  27.         <omgdi:waypoint x="367.0" y="125.0"></omgdi:waypoint>  
  28.         <omgdi:waypoint x="367.0" y="190.0"></omgdi:waypoint>  
  29.       </bpmndi:BPMNEdge>  
  30.       <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">  
  31.         <omgdi:waypoint x="367.0" y="245.0"></omgdi:waypoint>  
  32.         <omgdi:waypoint x="367.0" y="300.0"></omgdi:waypoint>  
  33.       </bpmndi:BPMNEdge>  
  34.       <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">  
  35.         <omgdi:waypoint x="367.0" y="355.0"></omgdi:waypoint>  
  36.         <omgdi:waypoint x="367.0" y="420.0"></omgdi:waypoint>  
  37.       </bpmndi:BPMNEdge>  
  38.     </bpmndi:BPMNPlane>  
  39.   </bpmndi:BPMNDiagram>  
  40. </definitions>  

    一個很簡單的流程圖processVariables.png


 

 

部署流程定義:

[java] view plain copy
  1. /** 
  2.  * 部署流程定義(從inputStream) 
  3.  */  
  4. @Test  
  5. public void deploymentProcessDefinition_inputStream() {  
  6.     ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();  
  7.   
  8.     InputStream inputStreamBpmn = this.getClass().getResourceAsStream(  
  9.             "/diagrams/processVariables.bpmn");  
  10.     InputStream inputStreamPng = this.getClass().getResourceAsStream(  
  11.             "/diagrams/processVariables.png");  
  12.     Deployment deployment = processEngine.getRepositoryService()// 與流程定義和部署對象相關的Service  
  13.             .createDeployment()// 創建一個部署對象  
  14.             .name("流程定義")// 添加部署名稱  
  15.             .addInputStream("processVariables.bpmn", inputStreamBpmn)// 使用資源文件的名稱(要求:與資源文件的名稱要一致),和輸入流完成部署  
  16.             .addInputStream("processVariables.png", inputStreamPng)// 使用資源文件的名稱(要求:與資源文件的名稱要一致),和輸入流完成部署  
  17.             .deploy();// 完成部署  
  18.     System.out.println("部署ID:" + deployment.getId());  
  19.     System.out.println("部署名稱:" + deployment.getName());  
  20. }  

運行結果:

 

    部署ID:701

    部署名稱:流程定義

 

 

啓動流程實例:

[java] view plain copy
  1.     /** 
  2.      * 啓動流程實例 
  3.      */  
  4.     @Test  
  5.     public void startProcessInstance() {  
  6.         // 流程定義的key  
  7.         String processDefinitionKey = "processVariables";  
  8.         ProcessInstance pi = processEngine.getRuntimeService()// 與正在執行的流程實例和執行對象相關的service  
  9.                 .startProcessInstanceByKey(processDefinitionKey);// 使用流程定義的key啓動流程實例,key對應processVariables文件中的id的屬性值,使用key值啓動,默認是按照最新版本進行啓動  
  10.   
  11.         System.out.println("流程實例ID:" + pi.getId());  
  12.         System.out.println("流程定義ID:" + pi.getProcessDefinitionId());  
  13.         System.out.println("流程實例ID" + pi.getProcessInstanceId());  
  14.   
  15. }  

運行結果:

 

    流程實例ID:801

    流程定義ID:processVariables:1:704

    流程實例ID801

 

查詢任務

[java] view plain copy
  1. /** 
  2.  * 查詢任務通過流程實例id 
  3.  */  
  4. @Test  
  5. public void findTask(){  
  6.     String processInstanceId="801";  
  7.     List<HistoricTaskInstance> list = processEngine.getHistoryService()//與歷史數據(歷史表)相關的service  
  8.             .createHistoricTaskInstanceQuery()//創建歷史任務實例查詢  
  9.             .processInstanceId(processInstanceId)  
  10.             .list();  
  11.     if(list!=null && list.size()>0){  
  12.         for(HistoricTaskInstance hti:list){  
  13.             System.out.println(hti.getId()+"    "+hti.getName()+"    "+hti.getProcessInstanceId()+"   "+hti.getStartTime()+"   "+hti.getEndTime()+"   "+hti.getDurationInMillis());  
  14.             System.out.println("################################");  
  15.         }  
  16.     }     
  17. }  

運行結果:

 

    804    提交申請   801   Fri Jun 26 10:55:02 CST2015   null   null

    ################################

 

 

    關於部署流程定義、啓動流程實例和查詢正在辦理的任務我們前面的文章已經介紹過了,所以我們不再詳細介紹,下面開始我們的設置流程變量,設置流程變量我們這裏提供了兩種方式,分別是使用基本數據類型和使用javabean的方法,同意獲取流程變量也是不一樣的:

 

使用基本數據類型:

 

設置流程變量

[java] view plain copy
  1. /** 
  2.  * 設置流程變量 
  3.  */  
  4. @Test  
  5. public void setVariables() {  
  6.     // 與任務相關的service,正在執行的service  
  7.     TaskService taskService = processEngine.getTaskService();  
  8.   
  9.     // 任務ID  
  10.     String taskId = "804";  
  11.   
  12.     // 1.設置流程變量,使用基本數據類型  
  13.     taskService.setVariable(taskId, "請假天數"7);// 與任務ID邦德  
  14.     taskService.setVariable(taskId, "請假日期"new Date());  
  15.     taskService.setVariableLocal(taskId, "請假原因""回去探親,一起吃個飯123");  
  16.       
  17.     System.out.println("設置流程變量成功!");  
  18.   
  19. }  

運行結果:

 

    設置流程變量成功!

 

獲取流程變量

[java] view plain copy
  1. /** 
  2.  * 獲取流程變量 
  3.  */  
  4. @Test  
  5. public void getVariables() {  
  6.     // 與任務(正在執行的service)  
  7.     TaskService taskService = processEngine.getTaskService();  
  8.     // 任務Id  
  9.     String taskId = "804";  
  10.     // 1.獲取流程變量,使用基本數據類型  
  11.     Integer days = (Integer) taskService.getVariable(taskId, "請假天數");  
  12.     Date date = (Date) taskService.getVariable(taskId, "請假日期");  
  13.     String reason = (String) taskService.getVariable(taskId, "請假原因");  
  14.   
  15.     System.out.println("請假天數:" + days);  
  16.     System.out.println("請假日期:" + date);  
  17.     System.out.println("請假原因:" + reason);  
  18.   
  19. }  

運行結果:

 

    請假天數:7

    請假日期:Fri Jun 2611:07:28 CST 2015

    請假原因:回去探親,一起吃個飯123

 

 

使用javabean

 

JavaBeanPerson

[java] view plain copy
  1. package com.tgb;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class Person implements Serializable {  
  7.   
  8.     private static final long serialVersionUID = 361866001729020143L;  
  9.     //請假天數  
  10.     private int id;  
  11.     //請假人  
  12.     private String name;  
  13.     //請假原因  
  14.     private String note;  
  15.     //請假時間  
  16.     private Date date;  
  17.     public Date getDate() {  
  18.         return date;  
  19.     }  
  20.     public void setDate() {  
  21.         this.date = new Date();  
  22.     }  
  23.     public String getNote() {  
  24.         return note;  
  25.     }  
  26.     public void setNote(String note) {  
  27.         this.note = note;  
  28.     }  
  29.     public int getId() {  
  30.         return id;  
  31.     }  
  32.     public void setId(int id) {  
  33.         this.id = id;  
  34.     }  
  35.     public String getName() {  
  36.         return name;  
  37.     }  
  38.     public void setName(String name) {  
  39.         this.name = name;  
  40.     }  
  41. }  


 

設置流程變量

[java] view plain copy
  1. /** 
  2.  * 設置流程變量 
  3.  */  
  4. @Test  
  5. public void setVariables() {  
  6.     // 與任務相關的service,正在執行的service  
  7.     TaskService taskService = processEngine.getTaskService();  
  8.   
  9.     // 任務ID  
  10.     String taskId = "804";  
  11.   
  12.     // 設置流程變量,使用javaBean方法  
  13.     /** 
  14.      * 當一個javaBean(實現序列號)放置到流程變量中,要求javabean的屬性不能在發生變化 如果發生變化,再獲取時,拋出異常 
  15.      *  
  16.      * 解決方案:在person對象中添加: private static final long 
  17.      * serialVersionUID="6757393795687480331L"; 同時實現序列號接口 
  18.      *  
  19.      */  
  20.     Person p = new Person();  
  21.     p.setName("翠花");  
  22.     p.setId(20);  
  23.     p.setDate();;  
  24.     p.setNote("回去探親,一起吃個飯123");  
  25.     taskService.setVariable(taskId, "人員信息(添加固定版本)", p);  
  26.       
  27.     System.out.println("設置流程變量成功!");  
  28.   
  29. }  

運行結果:

 

    設置流程變量成功!

 

獲取流程變量

[java] view plain copy
  1. /** 
  2.  * 獲取流程變量 
  3.  */  
  4. @Test  
  5. public void getVariables() {  
  6.     // 與任務(正在執行的service)  
  7.     TaskService taskService = processEngine.getTaskService();  
  8.     // 任務Id  
  9.     String taskId = "804";  
  10.   
  11.     // 2.獲取流程變量,使用javaBean類型  
  12.     Person p = (Person)taskService.getVariable(taskId, "人員信息(添加固定版本)");  
  13.     System.out.println(" 請假人:  "+p.getName()+"  請假天數:  "+p.getId()+"   請假時間:"+ p.getDate()+ "   請假原因: "+p.getNote());  
  14.   
  15. }  

運行結果:

 

    請假人: 翠花  請假天數:  20  請假時間:Fri Jun 26 11:13:44 CST 2015  請假原因: 回去探親,一起吃個飯123

 

 

查詢歷史流程變量

 

    可以根據變量名稱查詢該變量的所有歷史信息

[java] view plain copy
  1. 可以根據變量名稱查詢該變量的所有歷史信息  
  2. /** 
  3.  * 查詢流程變量的歷史表 
  4.  */  
  5. @Test  
  6. public void findHistoryProcessVariables(){  
  7.     List<HistoricVariableInstance> list = processEngine.getHistoryService()  
  8.             .createHistoricVariableInstanceQuery()//創建一個歷史的流程變量查詢對象  
  9.             .variableName("請假原因")  
  10.             .list();  
  11.     if (list!=null &&list.size()>0) {  
  12.         for (HistoricVariableInstance hvi : list) {  
  13.             System.out.println(hvi.getId()+"     "+hvi.getProcessInstanceId()+"   "+hvi.getVariableName()  
  14.                     +"   "+hvi.getVariableTypeName()+"    "+hvi.getValue());  
  15.             System.out.println("########################################");  
  16.         }  
  17.     }  
  18.   
  19. }  

 

流程變量支持的數據類型:

 

    流程變量支持的數據類型包括:TypeName、string、integer、short、long、double、boolean、data、binary、serializable,我們可以看出流程變量支持的包括了大部分封裝類型和Date、String和實現了Serializable接口的類的類型。


 

三、總結

 

    我們這篇文章將流程變量的相關知識進行了介紹,除了介紹流程變量的相關定義外還通過具體代碼例子介紹了通過不同方式來設置和獲取流程變量以及流程變量支持的數據類型。


發佈了22 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章