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,很容易基于流程實例的新的部署流程定義??梢詮腜rocessEngine實例檢索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來領取和完成流程中的用戶任務。