国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

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

2019-11-08 00:24:45
字體:
供稿:網(wǎng)友

一、前言

 

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

 

二、正文

 

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

 

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

 

如圖:

 

 

    關(guān)于流程實例的例子,我們先來看下流程圖的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="審批【總經(jīng)理】" 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()// 與流程定義和部署對象相關(guān)的Service              .createDeployment()// 創(chuàng)建一個部署對象              .name("流程定義")// 添加部署名稱              .addInputStream("processVariables.bpmn", inputStreamBpmn)// 使用資源文件的名稱(要求:與資源文件的名稱要一致),和輸入流完成部署              .addInputStream("processVariables.png", inputStreamPng)// 使用資源文件的名稱(要求:與資源文件的名稱要一致),和輸入流完成部署              .deploy();// 完成部署      System.out.println("部署ID:" + deployment.getId());      System.out.println("部署名稱:" + deployment.getName());  }  運行結(jié)果:

 

    部署ID:701

    部署名稱:流程定義

 

 

啟動流程實例:

[java] view plain copy    /**      * 啟動流程實例      */      @Test      public void startProcessInstance() {          // 流程定義的key          String processDefinitionKey = "processVariables";          ProcessInstance pi = processEngine.getRuntimeService()// 與正在執(zhí)行的流程實例和執(zhí)行對象相關(guān)的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());    }  運行結(jié)果:

 

    流程實例ID:801

    流程定義ID:processVariables:1:704

    流程實例ID801

 

查詢?nèi)蝿?/strong>

[java] view plain copy/**  * 查詢?nèi)蝿胀ㄟ^流程實例id  */  @Test  public void findTask(){      String processInstanceId="801";      List<HistoricTaskInstance> list = processEngine.getHistoryService()//與歷史數(shù)據(jù)(歷史表)相關(guān)的service              .createHistoricTaskInstanceQuery()//創(chuàng)建歷史任務實例查詢              .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("################################");          }      }     }  運行結(jié)果:

 

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

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

 

 

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

 

使用基本數(shù)據(jù)類型:

 

設(shè)置流程變量

[java] view plain copy/**  * 設(shè)置流程變量  */  @Test  public void setVariables() {      // 與任務相關(guān)的service,正在執(zhí)行的service      TaskService taskService = processEngine.getTaskService();        // 任務ID      String taskId = "804";        // 1.設(shè)置流程變量,使用基本數(shù)據(jù)類型      taskService.setVariable(taskId, "請假天數(shù)", 7);// 與任務ID邦德      taskService.setVariable(taskId, "請假日期", new Date());      taskService.setVariableLocal(taskId, "請假原因", "回去探親,一起吃個飯123");            System.out.println("設(shè)置流程變量成功!");    }  

運行結(jié)果:

 

    設(shè)置流程變量成功!

 

獲取流程變量

[java] view plain copy/**  * 獲取流程變量  */  @Test  public void getVariables() {      // 與任務(正在執(zhí)行的service)      TaskService taskService = processEngine.getTaskService();      // 任務Id      String taskId = "804";      // 1.獲取流程變量,使用基本數(shù)據(jù)類型      Integer days = (Integer) taskService.getVariable(taskId, "請假天數(shù)");      Date date = (Date) taskService.getVariable(taskId, "請假日期");      String reason = (String) taskService.getVariable(taskId, "請假原因");        System.out.println("請假天數(shù):" + days);      System.out.println("請假日期:" + date);      System.out.println("請假原因:" + reason);    }  運行結(jié)果:

 

    請假天數(shù):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;      //請假天數(shù)      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;      }  }  

 

設(shè)置流程變量

[java] view plain copy/**  * 設(shè)置流程變量  */  @Test  public void setVariables() {      // 與任務相關(guān)的service,正在執(zhí)行的service      TaskService taskService = processEngine.getTaskService();        // 任務ID      String taskId = "804";        // 設(shè)置流程變量,使用javaBean方法      /**      * 當一個javaBean(實現(xiàn)序列號)放置到流程變量中,要求javabean的屬性不能在發(fā)生變化 如果發(fā)生變化,再獲取時,拋出異常      *       * 解決方案:在person對象中添加: private static final long      * serialVersionUID="6757393795687480331L"; 同時實現(xiàn)序列號接口      *       */      Person p = new Person();      p.setName("翠花");      p.setId(20);      p.setDate();;      p.setNote("回去探親,一起吃個飯123");      taskService.setVariable(taskId, "人員信息(添加固定版本)", p);            System.out.println("設(shè)置流程變量成功!");    }  運行結(jié)果:

 

    設(shè)置流程變量成功!

 

獲取流程變量

[java] view plain copy/**  * 獲取流程變量  */  @Test  public void getVariables() {      // 與任務(正在執(zhí)行的service)      TaskService taskService = processEngine.getTaskService();      // 任務Id      String taskId = "804";        // 2.獲取流程變量,使用javaBean類型      Person p = (Person)taskService.getVariable(taskId, "人員信息(添加固定版本)");      System.out.println(" 請假人:  "+p.getName()+"  請假天數(shù):  "+p.getId()+"   請假時間:"+ p.getDate()+ "   請假原因: "+p.getNote());    }  運行結(jié)果:

 

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

 

 

查詢歷史流程變量

 

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

[java] view plain copy可以根據(jù)變量名稱查詢該變量的所有歷史信息  /**  * 查詢流程變量的歷史表  */  @Test  public void findHistoryProcessVariables(){      List<HistoricVariableInstance> list = processEngine.getHistoryService()              .createHistoricVariableInstanceQuery()//創(chuàng)建一個歷史的流程變量查詢對象              .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("########################################");          }      }    }   

流程變量支持的數(shù)據(jù)類型:

 

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

 

三、總結(jié)

 

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

http://blog.csdn.net/zwk626542417/article/details/46648139


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石狮市| 永丰县| 涞源县| 皋兰县| 茶陵县| 安乡县| 鄂托克前旗| 兴业县| 金昌市| 涞源县| 松江区| 饶平县| 岐山县| 平泉县| 泊头市| 蓬溪县| 于都县| 宁武县| 额敏县| 梧州市| 油尖旺区| 涡阳县| 耿马| 天长市| 疏勒县| 斗六市| 镇平县| 菏泽市| 札达县| 汉寿县| 丘北县| 大埔县| 太谷县| 延津县| 宜都市| 龙口市| 汶川县| 西乌| 榆林市| 榆林市| 宾川县|