http://blog.csdn.net/zwk626542417/article/details/46648139html
1、前言
上一篇文章咱們將流程實例的啓動與查詢,任務的辦理查詢都進行了介紹,咱們這篇文章來介紹activiti中的流程變量。java
2、正文
流程變量與咱們日常理解的變量是同樣的,只不過是用在了咱們activiti中,因此稱爲流程變量,流程變量在整個工做流扮演着很重要的角色。數據庫
例如,請假流程中有請假天數、請假緣由等一些參數都是流程變量使用的範圍,流程變量的做用域範圍是只對應一個流程實例。也就是說各個流程實例的流程變量是不互相影響的。流程實例結束完成之後流程變量還保存在數據庫中(存放在流程變量的歷史表中)。express
如圖:this
關於流程實例的例子,咱們先來看下流程圖的processVariables.bpmn的配置文件:spa
- <?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:.net
部署流程定義:xml
- @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()
- .createDeployment()
- .name("流程定義")
- .addInputStream("processVariables.bpmn", inputStreamBpmn)
- .addInputStream("processVariables.png", inputStreamPng)
- .deploy();
- System.out.println("部署ID:" + deployment.getId());
- System.out.println("部署名稱:" + deployment.getName());
- }
運行結果:htm
部署ID:701對象
部署名稱:流程定義
啓動流程實例:
-
- @Test
- public void startProcessInstance() {
-
- String processDefinitionKey = "processVariables";
- ProcessInstance pi = processEngine.getRuntimeService()
- .startProcessInstanceByKey(processDefinitionKey);
-
- 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
查詢任務
- @Test
- public void findTask(){
- String processInstanceId="801";
- List<HistoricTaskInstance> list = processEngine.getHistoryService()
- .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的方法,贊成獲取流程變量也是不同的:
使用基本數據類型:
設置流程變量
- @Test
- public void setVariables() {
-
- TaskService taskService = processEngine.getTaskService();
-
-
- String taskId = "804";
-
-
- taskService.setVariable(taskId, "請假天數", 7);
- taskService.setVariable(taskId, "請假日期", new Date());
- taskService.setVariableLocal(taskId, "請假緣由", "回去探親,一塊兒吃個飯123");
-
- System.out.println("設置流程變量成功!");
-
- }
運行結果:
設置流程變量成功!
獲取流程變量
- @Test
- public void getVariables() {
-
- TaskService taskService = processEngine.getTaskService();
-
- String taskId = "804";
-
- 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類
- package 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;
- }
- }
設置流程變量
- @Test
- public void setVariables() {
-
- TaskService taskService = processEngine.getTaskService();
-
-
- String taskId = "804";
-
-
-
- Person p = new Person();
- p.setName("翠花");
- p.setId(20);
- p.setDate();;
- p.setNote("回去探親,一塊兒吃個飯123");
- taskService.setVariable(taskId, "人員信息(添加固定版本)", p);
-
- System.out.println("設置流程變量成功!");
-
- }
運行結果:
設置流程變量成功!
獲取流程變量
- @Test
- public void getVariables() {
-
- TaskService taskService = processEngine.getTaskService();
-
- String taskId = "804";
-
-
- 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
查詢歷史流程變量
能夠根據變量名稱查詢該變量的全部歷史信息
- 能夠根據變量名稱查詢該變量的全部歷史信息
- @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接口的類的類型。
3、總結
咱們這篇文章將流程變量的相關知識進行了介紹,除了介紹流程變量的相關定義外還經過具體代碼例子介紹了經過不一樣方式來設置和獲取流程變量以及流程變量支持的數據類型。