1.5.3 Implementing your first process in Activiti (用Activiti实现第一个流程)
In this chapter we referenced a book order process a couple of times. Now we have our BPM environment ready to be used, let’s try and implement a simplified version of this business process. We could use the Activiti Modeler to first model the process, and the Activiti Designer to implement and deploy the process, but it’s better to start off with a BPMN 2.0 XML document for learning purposes. So no drag and drop development, but get ready for some XML hacking.
本章我们多次提及订书流程。既然BPMN环境准备就绪,那么让我们尝试并实现这个业务流程的简单版本吧。尽管我们可以使用Activiti Modeler对流程进行建模,并用Activiti Designer 实现并部署这个流程,但是为了教学目的,最好采用BPMN 2.0 XML文件。所以,尽管没有拖拽的开发环境,准备对作XML的黑客。
We already introduced a lot of BPMN 2.0 constructs in section 1.4 and we also looked at a BPMN 2.0 XML document there. So let’s create a clean XML document for the book order process with a start and end event as shown in code listing 1.1.
在 1.4节介绍了许多BPMN 2.0构件,我们也查看了BPMN 2.0 XML文档。所以,让我们为订单流程创建一个简洁文档,它有开始和结束事件。代码如表1.1 所示。
Listing 1.1 bookorder.bpmn20.xml document with only a start and end event
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" #1
targetNamespace="http://www.bpmnwithactiviti.org"> #2
<process id="bookorder" name="bookorder">
<startEvent id="startevent1" name="Start"/>
<endEvent id="endevent1" name="End"/>
<sequenceFlow id="sequenceflow1" name="flow" #A
sourceRef="startevent1" targetRef="endevent1"/>
</process>
</definitions>
#1 Root element of BPMN 2.0 XML
#2 The process namespace
#A Connecting start to end event
A BPMN 2.0 process definition always starts with a definitions element #1 with a namespace of the OMG BPMN specification. Each process definition must also define a namespace, we have defined here a target Namespace #2 of the book’s website. Activiti also provides a namespace which enables us to use Activiti extensions to the BPMN 2.0 specification as we will see in chapter 3. We can now run this very simple process to test if we have the process defined correctly and the environment set-up in the right manner.
BPMN 2.0 流程定义总是从带有OMG BPMN规范命名空间的定义元素 #1开始。每个流程定义也必须定义一个命名空间,在此我们已经定义了书店的网址作为命名空间#2。Activiti也提供了命名空间让我们使用BPMN 2.0规范的Activiti扩展。第三章我们将讨论这些规范,我们现在可以运行这个简单的流程来测试定义的流程是否正确,设置的环境是否正常。
To test the process, you have to create a Java project in your favorite editor. In this book we’ll use Eclipse for the example description, because the Eclipse Designer is only available as an Eclipse plug-in. You can also download the source code from the book’s website at Manning and import the examples from there. After your Java project is created, the Activiti libraries have to be added to the Java build path. The source code of the book uses Maven to retrieve all the necessary dependencies. The project structure of the example code is explained in detail in chapter 3. If you are already familiar with using Maven you can use the mvn eclipse:eclipse command to get the dependencies referenced from your Eclipse project, otherwise you can read the first section in chapter 3 how to setup the examples project.
为了测试流程,得在编辑器里建立Java项目。本书为了示例描述,我们将使用Eclipse,因为Eclipse Designer只有Eclipse插件可用。你也可以从本书下载源代码。并从那里输入示例。在建立Java项目之后,得把Activiti类库加入到Java构建路径里。本书源代码使用Maven检索所有必须的依赖。示例代码的项目结构在第三章讨论。如果已经熟悉Maven,可以使用mvn eclipse:eclipse命令得到Eclipse项目的所有依赖包。否则,否则您可以阅读第3章第一节,如何建立示例项目。
With the dependencies in place you can add a class file with the name BookOrderTest to your Java project. The BookOrderTest class should contain one test method which is shown in code listing 1.2.
如果依赖包就位,可以把BookOrderTest类文件加入到Eclipse项目。BookOrderTest类应该包括一个测试方法,代码如表1.2所示。
Listing 1.2 First example of a JUnit test for a Activiti process deployment
public class BookOrderTest {
@Test
public void startBookOrder() {
ProcessEngine processEngine = new ProcessEngineBuilder() #1
.configureFromResource("activiti.cfg.xml") #1
.buildProcessEngine(); #1
RuntimeService runtimeService =
processEngine.getRuntimeService();
RepositoryService repositoryService =
processEngine.getRepositoryService();
repositoryService.createDeployment() #2
.addClasspathResource("bookorder.bpmn20.xml") #2
.deploy(); #2
ProcessInstance processInstance = #3
runtimeService.startProcessInstanceByKey("bookorder"); #3
assertNotNull(processInstance.getId());
System.out.println("id " + processInstance.getId() + " " +
processInstance.getProcessDefinitionId());
}
}
#1 Create the Activiti engine
#2 Deploy the bookorder process definition
#3 Start the bookorder process instance
In just a few lines of code we are able to start-up the Activiti process engine, deploy the book order process XML file from code listing 1.1 to it, and start a process instance for the deployed process definition. This is good stuff right?
寥寥数行代码,就能启动Activiti流程引擎,部署如表1.1订单流程实例,这真是一个好东东,对吗?
The process engine can be created with the ProcessEngineBuilder #1, which can accept a configuration file which contains the database location, username and password. We have to add this activiti.cfg.xml file to the source path of the Java project to be able to run the unit test. The default activiti.cfg.xml contains a H2 database configuration, which was created and started as part of the installation procedure. The contents of the configuration file are shown below.
流程引擎可以用ProcessEngineBuilder #1来建立,它可以接受包括数据库位置,用户名和密码的配置文件。我们不得不把activiti.cfg.xml加入到Java项目的源代码路径,并能够运行单元测试。缺省的activiti.cfg.xml包括H2数据库配置, 它并作为安装过程的一部分可建立和开始。配置文件的内容如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<activiti-cfg>
<database type="h2" schema-strategy="check-version">
<jdbc url="jdbc:h2:tcp://localhost/activiti"
driver="org.h2.Driver"
username="sa"
password="" />
</database>
<job-executor activate="off" />
</activiti-cfg>
The details of this configuration file are explained in chapter 3, so for now just know that it contains the database configuration so the process engine can connect to the H2 database. By the way, Activiti can also run on other database platforms than H2 as we’ll see in chapter 5.
第3章将解释配置文件的细节,所以现在我只要知道它包括数据库配置,就可让流程引擎可以连接H2数据即可。顺便说一句,Activiti也可运行其它数据库平台,正如第5章所示,为了部署Java代码里的流程。
The next important step in code listing 1.2 is the deployment of the bookorder.bpmn20.xml file we showed in code listing 1.1. To deploy a process from Java code we need to access the RepositoryService from the ProcessEngine instance. Via the RepositoryService instance we can add the book order XML file to the classpath and deploy it to the process engine #2. The process engine will validate the book order process file and create a new process definition in the H2 database.
我们需要从ProcessEngine实例访问RepositoryService。通过RepositoryService实例,可以将订书流程XML文件加入到classpath,并部署到流程引擎#2中。流程引擎将验证订书流程文件,并在H2数据库建立一个新的流程定义。
Now it’s easy to start a process instance based on the newly deployed process definition by invoking the startProcessInstanceByKey method #3 on the RuntimeService instance, which is also retrieved from the ProcessEngine instance. The key bookorder which is passed as process key parameter should be equal to the process name attribute from the book order process of code listing 1.1. A process instance is stored to the H2 database and a process instance id is created that can be used to as a reference to this specific process instance. So this identifier is very important and should not be lost when you develop your own BPM application with Activiti.
通过调用RuntimeService实例的startProcessByKey方法 #3,很容易基于流程实例的新的部署流程定义。可以从ProcessEngine实例检索RuntiimeService实例。作为流程Key参数的Key bookOrder应该和代码列表1.1订书流程名属性一致。流程实例存储在H2数据库。当使用Activiti开发您自己的BPM应用时,建立的流程实例id可以用来作为指定流程实例的引用。所以标识符非常重要,不要丢失之。
Before you run the startBookOrder unit test, make sure that the bookorder.bpmn20.xml file is available in the source folder of the Java project. If that’s the case you can run the unit test and the result should be green. In the console you should see a message like:
在运行startBookOrder单元测试之前,确保Java项目源文件夹的bookorder.bpmn20.xml文件可用。如果你能运行单元测试,那么结果应该为绿。在控制台能看见如下信息:
id 112 bookorder:1
This message means that the process instance id is 112 and the process definition that was used to create the instance was the bookorder definition with version 1. Notice that if you run the same unit test again the process instance id will change and the version of the process definition will be equal to 2.
这个消息意味着流程id为112.用来建立实例的流程定义是版本为1的订书流程定义。注意如果再次运行相同的单元测试,流程实例的id发生变化,流程定义的版本将变为2.
Now we have the basics covered, let’s implement a bit more complex book order process, so we can use the Activiti Explorer to claim and finish a user task for our process.
既然我们已经了解了基础,那么让我们实现一个更为复杂的订书流程。所以我们可以使用Activiti Explorer来领取和完成流程中的用户任务。