Workflow learning-Activiti five steps of process variables, workflow activiti

Source: Internet
Author: User

Workflow learning-Activiti five steps of process variables, workflow activiti
I. Preface

 

In the previous article, we introduced the process instance startup and query, and the task handling and query. In this article, we will introduce the process variables in activiti.

 

Ii. Text

 

Process variables are the same as the variables we usually understand. They are only used in activiti, so they are called process variables. process variables play an important role in the entire workflow.

 

For example, some parameters in the leave process, such as the number of days for leave and the reason for leave, are used by the process variable. The scope of the process variable corresponds to only one process instance. That is to say, the Process Variables of each process instance do not affect each other. After the process instance is completed, the process variables are stored in the database (stored in the History Table of the process variables ).

 


 

 

For an example of a process instance, Let's first look at the configuration file of processVariables. bpmn In the flowchart:

 

<? 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 [process leave]" isExecutable =" true "> <startEvent id =" startevent1 "name =" Start "> </startEvent> <endEvent id =" endevent1 "name =" End "> </endEvent> <userTask id = "usertask1" name = "Submit Application"> </userTask> <sequenceFlow id = "flow1" sourceRef = "startevent1" targetRef = "usertask1"> </sequenceFlow> <userTask id = "usertask2" name = "approval [General Manager]" activiti: assignee = ""> </userTask> <sequenceFlow id = "flow2" sourceRef = "usertask1" targetRef = "usertask2"> </sequenceFlow> <sequenceFlow id = "flow3" sourceRef = "usertask2" targetRef = "endevent1"> </sequenceFlow> </process> <bpmndi: bpmndientid = "inherit"> <bpmndi: describpmnelement = "processVariables" id = "inherit"> <bpmndi: BPMNShape bpmnElement = "startevent1" id = "inherit"> <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: bpmndions> </definitions>

A simple flow chart processvariables.png:


 

 

Deployment process definition:

/*** Deployment process definition (from inputStream) */@ Testpublic void deploymentProcessDefinition_inputStream () {ProcessEngine processEngine = ProcessEngines. getdefaprocesprocessengine (); InputStream inputStreamBpmn = this. getClass (). getResourceAsStream ("/diagrams/processVariables. bpmn "); InputStream inputStreamPng = this. getClass (). getResourceAsStream ("/diagrams/processVariables.png"); Deployment deployment = processEngine. getRepositoryService () // The Service related to the process definition and deployment object. createDeployment () // create a deployment object. name ("Process Definition") // Add deployment name. addInputStream ("processVariables. bpmn ", inputStreamBpmn) // use the name of the resource file (required: Same as the name of the resource file), and complete the deployment with the input stream. addInputStream ("processVariables.png", inputStreamPng) // use the name of the resource file (required: the name must be the same as that of the resource file) to complete deployment with the input stream. deploy (); // complete System deployment. out. println ("deployment ID:" + deployment. getId (); System. out. println ("deployment name:" + deployment. getName ());}

Running result:

 

Deployment ID: 701

Deployment name: Process Definition

 

 

Start Process instance:

/*** START process instance */@ Testpublic void startProcessInstance () {// process-defined keyString processDefinitionKey = "processVariables"; ProcessInstance pi = processEngine. getRuntimeService () // The service related to the ongoing process instance and execution object. startProcessInstanceByKey (processDefinitionKey); // use the key defined in the process to start the process instance. The key corresponds to the property value of the id in the processVariables file and is started with the key value. By default, the System is started according to the latest version. out. println ("process instance ID:" + pi. getId (); System. out. println ("process definition ID:" + pi. getProcessDefinitionId (); System. out. println ("process instance ID" + pi. getProcessInstanceId ());}

Running result:

 

Process instance ID: 801

Process Definition ID: processVariables: 1: 704

Process instance ID801

 

Query a task

/*** Query the task by process instance id */@ Testpublic void findTask () {String processInstanceId = "801"; List <HistoricTaskInstance> list = processEngine. getHistoryService () // service related to historical data (History Table. createHistoricTaskInstanceQuery () // create a historical task instance query. 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 ("################################");}}}

Running result:

 

804 submit application 801 Fri Jun 26 10:55:02 CST2015 null

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

 

 

We have already introduced the deployment process definition, START process instances, and query ongoing tasks in the previous article, so we will not detail them. Next we will start to set process variables, the following two methods are provided to set process variables: using the basic data type and using the javabean method, and agreeing to get the process variables are also different:

 

Use Basic data types:

 

Set Process Variables

/*** Set the process variable */@ Testpublic void setVariables () {// The service related to the task, and the serviceTaskService taskService being executed = processEngine. getTaskService (); // task IDString taskId = "804"; // 1. set Process Variables and use the basic data type taskService. setVariable (taskId, "days for leave", 7); // bond taskService with the task ID. setVariable (taskId, "Leave Date", new Date (); taskService. setVariableLocal (taskId, "reason for asking for leave", "Go back to visit your friends and have a meal together for 123"); System. out. println ("process variable set successfully! ");}

Running result:

 

Process variable set!

 

Get Process Variables

/*** Get the process variable */@ Testpublic void getVariables () {// TaskService = processEngine with the task (the service being executed. getTaskService (); // task IdString taskId = "804"; // 1. obtain the process variable and use the basic data type Integer days = (Integer) taskService. getVariable (taskId, "days for leave"); Date date = (Date) taskService. getVariable (taskId, "Leave date"); String reason = (String) taskService. getVariable (taskId, "reason for asking for leave"); System. out. println ("leave days:" + days); System. out. println ("Leave date:" + date); System. out. println ("reason for leave:" + reason );}

Running result:

 

Leave days: 7

Leave Date: Fri Jun 2611: 07: 28 CST 2015

Reason for asking for leave: Go back and have a meal for 123

 

 

UseJavabean

 

Person class of JavaBean

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 of the number of days for leave; // private String name of the Person for leave; // private String note of the reason for leave; // leave time 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 ;}}


 

Set Process Variables

/*** Set the process variable */@ Testpublic void setVariables () {// The service related to the task, and the serviceTaskService taskService being executed = processEngine. getTaskService (); // task IDString taskId = "804"; // set the process variable. Use the javaBean method/*** to place a javaBean (Implementation serial number) in the process variable, if the attributes of the javabean object cannot be changed, an exception is thrown when it is obtained. ** solution: add the private static final long * serialVersionUID = "6757392135687480331l" to the person object "; serial number interface is also implemented **/Person p = new Person (); p. setName ("Cuihua ") ; P. setId (20); p. setDate (); p. setNote ("Go back to visit your friends and have a meal together for 123"); taskService. setVariable (taskId, "personnel information (add fixed version)", p); System. out. println ("process variable set successfully! ");}

Running result:

 

Process variable set!

 

Get Process Variables

/*** Get the process variable */@ Testpublic void getVariables () {// TaskService = processEngine with the task (the service being executed. getTaskService (); // task IdString taskId = "804"; // 2. obtain the process variable and use the Person p = (Person) taskService of the javaBean type. getVariable (taskId, "personnel information (add a fixed version)"); System. out. println ("requester:" + p. getName () + "days for leave:" + p. getId () + "leave time:" + p. getDate () + "reason for leave:" + p. getNote ());}

Running result:

 

Leave: Cui Hua leave days: 20 leave time: Fri Jun 26 11:13:44 CST 2015 leave reason: Go back to visit your friends and have a meal together for 123

 

 

Query Historical Process Variables

 

You can query all historical information of a variable based on its name.

You can query all the historical information of the Variable Based on the variable name/*** query the historical table of the process variable */@ Testpublic void findHistoryProcessVariables () {List <HistoricVariableInstance> list = processEngine. getHistoryService (). createHistoricVariableInstanceQuery () // create a historical process variable query object. variableName ("reason for leave "). 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 ("##################################### ###");}}}

 

Data Types supported by process variables:

 

Data Types supported by process variables include TypeName, string, integer, short, long, double, boolean, data, binary, and serializable, we can see that the Process Variables support most of the encapsulation types, Date, String, and class types that implement the Serializable interface.


 

Iii. Summary

 

This article introduces the knowledge of process variables, in addition to the definition of process variables, the code example also describes how to set and obtain the data types supported by Process Variables and Process Variables in different ways.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.