Chapter 7. BPMN
第七章 BPMN
Table of Contents
Getting started: 10 minute tutorial
什么是BPMN?
See our FAQ entry on BPMN 2.0
示例
Examples for the BPMN 2.0 constructs described in the following sections can be found in the docs/examples folder.
本章所讨论的的BPMN2.0可以在docs/examples 目录下找到相应的示例。
See the specific section on examples for more information.
更多的可以参考示例 章节。
流程定义
To create a new BPMN 2.0 process definition, it's best to have your Eclipse properly set up.
为了创建BPMN2.0的流程定义文件,最好你的Eclipse中已经设置好相应的属性。
Create a new XML file (New->Other->XML-XML) and give it a name. Make sure that the file ends with .bpmn20.xml, since otherwise the engine won't pick up this file for deployment.
创建一个XML文件(New->Other->XML-XML))并赋一个指定的名称。XML文件名称必须以.bpmn20.xml作为结尾,否则流程引擎将不会部署此文件。
The root element of the BPMN 2.0 schema is the definitions element. Within this element, multiple process definitions can be defined (although we advise to have only one process definition in each file, since this simplifies maintenance later in the development process). An empty process definition looks as follows. Note that the minimal definitions element only needs the xmlns and targetNamespacedeclaration.
BPMN2.0定义文件中的根元素是definitions。使用该元素可以在一个定义文件中定义多个流程(尽管如此,我们还是建议一个文件中最好只定义一个流程,因为这样以后维护起来更简单)。一个空的流程定义文件大概如下面所示。注意此时定义元素仅仅需要声明 xmlns 和 targetNamespace。
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn-extensions" targetNamespace="http://www.activiti.org/bpmn2.0"> <process id="myProcess" name="My First Process"> </process> </definitions>
The process element has two attributes:
流程元素有两个属性:
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
Important to note here is that this is not the same as calling the startProcessInstanceById method. This method expects the String id that was generated at deploy time by the Activiti engine, and can be retrieved by calling the processDefinition.getId() method. The format of the generated id is 'key:version', and the length is constrained to 64 characters. If you get an ActivitiException stating that the generated id is too long, limit the text in the key field of the process.
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
这里需要注意的是与startProcessInstanceById 方法的区别。startProcessInstanceById 方法传入的参数是id字符串,该id是在Activiti引擎部署一个流程定义自动生成的,该id的值可以通过调用 processDefinition.getId() 方法获取。 Id格式为 'key:version', 长度为64位。如果你在部署时得到一个id长度太长的ActivitiException异常,那么最好修改key字段的长度。
Getting started: 10 minute tutorial
启航:10分钟教程
In this section we will cover a (simple) business process that we will use to introduce some basic Activiti concepts and the Activiti API.
在本小节中,我们将通过一个(简单)的业务流程用来介绍Activiti的基本概念以及Activiti API。
用例
The use case is very simple: we have a company, let's call it BPMCorp. In BPMCorp, a financial report needs to be written every month for the company shareholders. This is the responsibility of the accountancy department. When the report is finished, one of the members of the upper management needs to approve the document before it is sent to all the shareholders.
这个用例非常简单:假如我们有一家名叫BPM的公司。在BPM公司,每个月财务部门需要将新的财务报表发送给公司的股东。当财务报表编写完毕后,需要经过上级管理部门进行审批,审批通过后才能发送给公司股东。
All files and code snippets used through the next sections can be found in the examples shipped with the Activiti distribution. Look for the package org.activiti.examples.bpmn.usertask.
接下来我们所使用的所有的文件以及代码都在Activiti发行包的examples目录中可以找到。具体可以参看 org.activiti.examples.bpmn.usertask package。
流程图
The business process as described above, can be graphically visualized using the Activiti Modeler. The end result, in BPMN 2.0 notation, looks like this:
上面所说的业务流程可以使用Activiti Modeler建模工具进行绘制。下图就是我们所绘制出的流程图:
There is nothing spectacular here. What we see is a none start event (circle on the left), followed by two user tasks: 'Write monthly financial report' and 'Verify monthly financial report', ending in a none end event (circle with thick border on the right).
这里没有什么好解释的。正如我们所看到的一个 none start event(左面那个圆圈),接下来是两个 user task:“Write monthly financial report”和“Verify monthly financial report”,最后是一个 none end event(右面那个带厚边框的圆)。
XML 表示
The XML version of this business process (FinancialReportProcess.bpmn20.xml) looks as shown below. It's easy to recognize the main elements of our process (click on the links for going to the detailed section of that BPMN 2.0 construct):
(FinancialReportProcess.bpmn20.xml)业务流程的XML表示如下面所示。我们可以清晰的辨识出主要的元素(点击链接查看BPMN2.0具体元素章节):
<process id="financialReport" name="Monthly financial report reminder process"> <startEvent id="theStart" /> <sequenceFlow id='flow1' sourceRef='theStart' targetRef='writeReportTask' /> <userTask id="writeReportTask" name="Write monthly financial report" > <documentation> Write monthly financial report for publication to shareholders. </documentation> <potentialOwner> <resourceAssignmentExpression> <formalExpression>accountancy</formalExpression> </resourceAssignmentExpression> </potentialOwner> </userTask> <sequenceFlow id='flow2' sourceRef='writeReportTask' targetRef='verifyReportTask' /> <userTask id="verifyReportTask" name="Verify monthly financial report" > <documentation> Verify monthly financial report composed by the accountancy department. This financial report is going to be sent to all the company shareholders. </documentation> <potentialOwner> <resourceAssignmentExpression> <formalExpression>management</formalExpression> </resourceAssignmentExpression> </potentialOwner> </userTask> <sequenceFlow id='flow3' sourceRef='verifyReportTask' targetRef='theEnd' /> <endEvent id="theEnd" /> </process>
Starting a process instance
启动流程实例
We now have defined the process definition of our business process. From such a process definition, we can create at runtime process instances. In this case, one process instance would match with the creation and verification of the financial report every month.
现在,我们已经为我们的业务流程定义了一个流程定义。通过该流程定义,我们就可以创建一个流程实例。在本示例中,一个流程实例也就是每个月创建并验证财务报表。
To be able to create process instances from a given process definition, we must first deploy this process definition. Deploying a process definition means two things:
为了能够从一个给定的流程定义中创建流程实例,我们必须首先部署该流程定义。部署流程定义也就意味着需要做下面两件事:
More information on deployment can be found in the dedicated deployment section.
更多关于如何进行部署的内容可以在 dedicated deployment section小节中找到。
As described in that section, deployment can happen in several ways. One way is through the API as follows:
正如该章节所描述的,部署一个流程可以通过多种方式。下面通过API进行部署就是其中一种:
Deployment deployment = repositoryService.createDeployment() .addClasspathResource("org/activiti/examples/bpmn/usertask/FinancialReportProcess.bpmn20.xml") .deploy();
al