Workflow learning-three steps of Activiti process definition management and three steps of activiti

Source: Internet
Author: User

Workflow learning-three steps of Activiti process definition management and three steps of activiti
I. Preface

 

In the previous article, we made a macro introduction to Activiti through a small demo, which gave you an overall understanding of Activiti. In this article, we will learn the CRUD of specific process definition management.

 

Ii. Text

 

What is the process definition?

 

ProcessDefinition (process definition) is a process step description. For example, in the process we will talk about next, the applicant Wang San initiates an application and Li Si acts as the department manager for approval. After the application is approved, this application is approved by Wang Wu, the next-level general manager. The token format exists.

 

 

In the previous article, we just mentioned a little bit about helloworld. bpmn is formed by dragging and dropping in the process designer. In fact, you can also configure it in the configuration file. I have saved the specific figure in the previous article, now I will configure helloworld. the bpmn configuration file is shown to you:


<? 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 = "HelloWorld" name = "HelloWorldProcess" isExecutable = "true"> <startEvent id = "startevent1" name = "Start"> </startEvent> <endEvent id = "endevent1" name = "End"> </endEvent> <userTask id = "usertask1" name = "Submit Application" activiti: assignee = "Zhang San"> </userTask> <userTask id = "usertask2" name = "approval [department manager]" activiti: assignee = ""> </userTask> <userTask id = "usertask3" name = "approval [General Manager]" activiti: assignee = ""> </userTask> <sequenceFlow id = "flow1" sourceRef = "startevent1" targetRef = "usertask1"> </sequenceFlow> <sequenceFlow id = "flow2" sourceRef = "usertask1" targetRef = "usertask2"> </sequenceFlow> <sequenceFlow id = "flow3" sourceRef = "usertask2" targetRef = "usertask3"> </sequenceFlow> <sequenceFlow id = "flow4" sourceRef = "usertask3" targetRef = "endevent1"> </sequenceFlow> </process> <bpmndi: bpmndistmid = "inherit"> <bpmndi: BPMNPlane bpmnElement = "HelloWorld" id = "inherit"> <bpmndi: BPMNShape bpmnElement = "startevent1" id = "inherit"> <omgdc: bounds height = "35.0" width = "35.0" x = "320.0" y = "50.0"> </omgdc: Bounds> </bpmndi: BPMNShape> <bpmndi: BPMNShape bpmnElement = "endevent1" id = "BPMNShape_endevent1"> <omgdc: bounds height = "35.0" width = "35.0" x = "320.0" y = "430.0"> </omgdc: Bounds> </bpmndi: BPMNShape> <bpmndi: BPMNShape bpmnElement = "usertask1" id = "BPMNShape_usertask1"> <omgdc: bounds height = "55.0" width = "105.0" x = "285.0" y = "120.0"> </omgdc: Bounds> </bpmndi: BPMNShape> <bpmndi: BPMNShape bpmnElement = "usertask2" id = "BPMNShape_usertask2"> <omgdc: bounds height = "55.0" width = "105.0" x = "285.0" y = "240.0"> </omgdc: Bounds> </bpmndi: BPMNShape> <bpmndi: BPMNShape bpmnElement = "usertask3" id = "BPMNShape_usertask3"> <omgdc: bounds height = "55.0" width = "105.0" x = "285.0" y = "350.0"> </omgdc: Bounds> </bpmndi: BPMNShape> <bpmndi: BPMNEdge bpmnElement = "flow1" id = "BPMNEdge_flow1"> <omgdi: waypoint x = "337.0" y = "85.0"> </omgdi: waypoint> <omgdi: waypoint x = "337.0" y = "120.0"> </omgdi: waypoint> </bpmndi: BPMNEdge> <bpmndi: BPMNEdge bpmnElement = "flow2" id = "BPMNEdge_flow2"> <omgdi: waypoint x = "337.0" y = "175.0"> </omgdi: waypoint> <omgdi: waypoint x = "337.0" y = "240.0"> </omgdi: waypoint> </bpmndi: BPMNEdge> <bpmndi: BPMNEdge bpmnElement = "flow3" id = "BPMNEdge_flow3"> <omgdi: waypoint x = "337.0" y = "295.0"> </omgdi: waypoint> <omgdi: waypoint x = "337.0" y = "350.0"> </omgdi: waypoint> </bpmndi: BPMNEdge> <bpmndi: BPMNEdge bpmnElement = "flow4" id = "BPMNEdge_flow4"> <omgdi: waypoint x = "337.0" y = "405.0"> </omgdi: waypoint> <omgdi: waypoint x = "337.0" y = "430.0"> </omgdi: waypoint> </bpmndi: BPMNEdge> </bpmndi: BPMNPlane> </bpmndi: bpmndions> </definitions>

You can use the process designer or the configuration file to directly write data.

 

CRUD defined in the process

 

 

Deployment process definition

 

Before defining a process, deploy the process definition. There are two ways to deploy the process definition:

 

1.deploy two flow-defined helloworld.bpmnand helloworld.png files

/*** DeoploymentProcessDefinition_classpath () {Deployment deployment = processEngine. getRepositoryService () // The service related to the process definition and deployment object. createDeployment () // create a deployment object. name ("Process Definition") // Add the deployment name. addClasspathResource ("diagrams/helloworld. bpmn ") // load from the classpath resource. Only one file can be loaded at a time. addClasspathResource ("diagrams/helloworld.png") // loads data from classpath resources. Only one file can be loaded at a time. deploy (); // complete System deployment. out. println ("deployment ID:" + deployment. getId (); System. out. println ("deployment name:" + deployment. getName ());}

Running result:


Deployment ID: 301

Deployment name: Process Definition

 

 

2.compress helloworld.bpmnand helloworld.png to zip for deployment

/*** Deployment process definition zip */@ Testpublic void deploymentProcessDefinition_zip () {InputStream in = this. getClass (). getClassLoader (). getResourceAsStream ("diagrams/helloworld.zip"); ZipInputStream zipInputStream = new ZipInputStream (in); Deployment deployment = processEngine. getRepositoryService () // The service related to the process definition and deployment object. createDeployment () // create a deployment object. name ("Process Definition") // Add deployment. addZipInputStream (zipInputStream) // deploy a file in the specified zip format. deploy (); // complete System deployment. out. println ("deployment ID:" + deployment. getId (); System. out. println ("deployment name:" + deployment. getName ());}

Running result:


Deployment ID: 401

Deployment name: Process Definition

 

We will explain the above deployment process as follows:


1) obtain the process engine object first: activiti. cfg. xml under classpath is automatically loaded at creation.

2) Get a RepositoryService object (repository object) through the obtained process engine object and the process engine object)

3) A deployment object configuration object is generated by the service objects in the warehouse to encapsulate the configurations related to deployment operations.

4) This is a chained programming. Set the display name in the deployment configuration object and upload the process definition rule file.

5) store the rule information defined in the process to the database table

These tables are related to deployment objects and process definitions:

Act_re_deployment stores the display name and deployment time of the process definition, and adds a record for each deployment;

Act_re_procdef (stores the attributes of the process definition. To deploy each new process definition, a record is added to this table. Note that when the key of the process definition is the same, version Upgrade is used;

Act_ge_bytearray stores deployment information related to process definition. That is, the place where the process definition document is stored. Each deployment adds two records, one for the bpmn rule file and the other for the image (if only one bpmn file is specified during deployment, activiti will parse the bpmn file content during deployment to automatically generate a flowchart ). The two files are not very large and are both stored in the database in binary format.

 

Process Definition Query

 

We have deployed the process definition above. Here we will query the process definition. There are two queries: one is to query all the process definitions and the other is to query the latest version of the process definition.

 

View All Process Definitions


/*** Query all Process Definitions */@ Testpublic void findProcessDefinition () {List <ProcessDefinition> list = processEngine. getRepositoryService () // The service first related to the process definition and deployment object. createProcessDefinitionQuery () // create a process-defined query/** specify the query conditions and where conditions *///. deploymentId (deploymentId) // query by deployment Object ID //. processDefinitionId (processDefinitionId) // use the process definition ID to query //. processDefinitionNameLike (processDefinitionNameLike) // use the name of the Process Definition to perform fuzzy query/* sort */. order ByProcessDefinitionVersion (). asc ()//. orderByProcessDefinitionVersion (). desc ()/* returned result set */. list (); // returns a list of sets to encapsulate the process definition //. singleResult (); // returns the unique result set //. count (); // number of returned result sets //. listPage (firstResult, maxResults); // if (list! = Null & list. size ()> 0) {for (ProcessDefinition pd: list) {System. out. println ("process definition ID:" + pd. getId (); // key + version defined by the process + number of randomly generated System. out. println ("process definition name:" + pd. getName (); // corresponding to helloworld. the name attribute value System in the bpmn file. out. println ("key defined by the process:" + pd. getKey (); // corresponding to helloworld. the id attribute value System in the bpmn file. out. println ("version defined by the process:" + pd. getVersion (); // when the key value of the process definition is the same, the version is upgraded. The default value is 1System. out. println ("Resource Name bpmn file:" + pd. getResourceName (); System. out. println ("Resource Name png file:" + pd. getDiagramResourceName (); System. out. println ("deployment Object ID:" + pd. getDeploymentId (); System. out. println ("##################################### ####################");}}}

Running result:


Process Definition ID: HelloWorld: 1: 304

Name of the Process Definition: HelloWorldProcess

Key of the Process Definition: HelloWorld

Version of the Process Definition: 1

Resource Name bpmn file: diagrams/helloworld. bpmn

Resource Name png file: diagrams/helloworld.png

Deployment Object ID: 301

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

Process Definition ID: HelloWorld: 2: 404

Name of the Process Definition: HelloWorldProcess

Key of the Process Definition: HelloWorld

Version of the Process Definition: 2

Resource Name bpmn file: helloworld. bpmn

Resource Name png file: helloworld.png

Deployment Object ID: 401

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

 

We can see from the above that, when the process definition key value is the same, the version is upgraded from 1, and the process definition id is [key: Version: Generate ID ];

 

The above code is described as follows:

 

1) The Service related to the process definition and deployment object is a RepositoryService.

2) create a process definition query object. You can set query parameters on ProcessDefinitionQuery.

3) Call the list method of the ProcessDefinitionQuery object to execute the query and obtain the list of qualified process definitions.

4) The running results show that the Key and Name values are: bpmn configuration file process node id and name attribute values.

5) The key attribute is used to differentiate different process definitions.

6) when a process with a specific key is defined for the first deployment, the version is 1. After that, each deployment will add 1 to the current maximum version number.

7) the generation rule of Id values is: {processDefinitionKey }:{ processDefinitionVersion }:{ generated-id}, where generated-id is a unique automatically generated number.

(8) If the deployment is repeated once, the value of deploymentId changes to the act_ge_property table in a certain form.

 

 

View the process definition in the latest version:

View the process definition in the latest version:/*** additional function to query the process definition in the latest version */@ Testpublic void findLastVersionProcessDefinition () {List <ProcessDefinition> list = processEngine. getRepositoryService (). createProcessDefinitionQuery (). orderByProcessDefinitionVersion (). asc () // sort the versions defined by the process in ascending order. list ();/*** Map <String, ProcessDefinition> key of the map set: value of the key map set of the Process Definition: the object of the process definition * features of the map set: when the key values of the map set are the same, the last value will replace the previous value */Map <String, ProcessDefinition> Map = new LinkedHashMap <String, ProcessDefinition> (); if (list! = Null & list. size ()> 0) {for (ProcessDefinition pd: list) {map. put (pd. getKey (), pd) ;}list <ProcessDefinition> pdList = new ArrayList <ProcessDefinition> (map. values (); if (pdList! = Null & pdList. size ()> 0) {for (ProcessDefinition pd: pdList) {System. out. println ("process definition ID:" + pd. getId (); // key + version defined by the process + number of randomly generated System. out. println ("process definition name:" + pd. getName (); // corresponding to helloworld. the name attribute value System in the bpmn file. out. println ("key defined by the process:" + pd. getKey (); // corresponding to helloworld. the id attribute value System in the bpmn file. out. println ("version defined by the process:" + pd. getVersion (); // when the key value of the process definition is the same, the version is upgraded. The default value is 1System. out. println ("Resource Name bpmn file:" + pd. getResourceName (); System. out. println ("Resource Name png file:" + pd. getDiagramResourceName (); System. out. println ("deployment Object ID:" + pd. getDeploymentId (); System. out. println ("##################################### ####################");}}}

Running result:

Process Definition ID: HelloWorld: 2: 404

Name of the Process Definition: HelloWorldProcess

Key of the Process Definition: HelloWorld

Version of the Process Definition: 2

Resource Name bpmn file: helloworld. bpmn

Resource Name png file: helloworld.png

Deployment Object ID: 401

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

 

The running results show that we can find out the process definition of the latest version. The query is the same as all the above queries, except that the filter version function is added, which is easy to understand by using map for code.

 

 

Obtain the file resources defined by the Process

 

After the process definition is deployed, you can view the process definition image.

/*** View the flowchart */@ Testpublic void viewPic () throws IOException {// put the produced image in the folder String deploymentId = "401 "; // TODO // obtain the image Resource Name List <String> list = processEngine. getRepositoryService (). getDeploymentResourceNames (deploymentId); // define the image Resource Name String resourceName = ""; if (list! = Null & list. size ()> 0) {for (String name: list) {if (name. indexOf (". png ")> = 0) {resourceName = name ;}}// retrieves the image input stream InputStream in = processEngine. getRepositoryService (). getResourceAsStream (deploymentId, resourceName); File file = new File ("D:/" + resourceName); // write the image of the input stream to FileUtils under drive D. copyInputStreamToFile (in, file );}

Note:


1) deploymentId is the process deployment ID

2) resourceName is the value of the NAME _ column in The act_ge_bytearray table.

3) use the getDeploymentResourceNames method of repositoryService to obtain the names of all files under the specified deployment.

4) use the getResourceAsStream method of the repositoryService to pass in the deployment ID and resource image name to obtain the input stream of the specified name file under the deployment.

5) for the last IO stream operation, use the copyInputStreamToFile method of the FileUtils tool to copy the process to the file and output the resource file as a stream to the specified folder.

 

 

Delete Process Definition

 

The process definition can be deleted because the process definition can be started. If there is no running process under the process definition, the process definition can be deleted normally. For associated information, delete it in cascade mode. You can delete a deployment object by using its id or the key defined in the process. The difference is that only one record is deleted by using the id, deleting a key deletes all the process definitions of all versions with the same key.

/*** Delete a process definition (delete all process definitions of different versions with the same key) */@ Testpublic void delteProcessDefinitionByKey () {// process-defined KeyString processDefinitionKey = "HelloWorld "; // use the key of the Process Definition to query the process definition, and query all versions of List <ProcessDefinition> list = processEngine. getRepositoryService (). createProcessDefinitionQuery (). processDefinitionKey (processDefinitionKey) // use the key defined in the process to query. list (); // traverse to obtain the deployment IDif (list! = Null & list. size ()> 0) {for (ProcessDefinition pd: list) {// gets the deployment IDString deploymentId = pd. getDeploymentId (); // * deletes a process without cascade operations. If the process is started, an exception is thrown. // * // processEngine. getRepositoryService (). deleteDeployment (deploymentId);/*** cascading deletion can be deleted regardless of whether the process is started or not */processEngine. getRepositoryService (). deleteDeployment (deploymentId, true );}}}

Note:


1) because the process definition is deleted and the deployment of the Process Definition belongs to the repository service, you should first obtain the RepositoryService

2) According to the key defined in the process, the process definition of all versions with the same key value is first queried, and then the deployment Object id of each process definition is obtained.

3) cascade deletion using the deployment Object id

 

 

Now we have finished introducing the deployment, query, and deletion of the process definition. The modification to the process definition is to re-deploy the process with the same key value, upgrade the version of the process definition without affecting the previous version process. The latest version of the process definition will be used by default for the new process.

 

Iii. Summary

 

This article mainly explains the concept of process definition, and then detailed the process definition deployment in different ways, it also describes the process definition query, process definition document resource acquisition, and process definition deletion.

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.