Activiti in Action(實(shí)戰(zhàn)Activiti)-第一章 BPMN 2.0: what’s in it for developers?(10)
Posted on 2011-01-06 21:26 網(wǎng)路冷眼@BlogJava 閱讀(4872) 評(píng)論(0) 編輯 收藏 所屬分類: Java 、Java EE 、BPM1.5.4 Implementing a simple book order process (實(shí)現(xiàn)一個(gè)簡(jiǎn)單的訂書流程)(上)
It would be a bit of a shame to stop chapter 1 with an example that contains just a start and an end event. So let’s enhance our simple book order process with a script task and a user task, so we can see a bit of action on the Activiti engine. First the script task will print an ISBN number that will be been given as input to the book order process when started. Then a user task will be used to handle the book ordering manually. So this is not the most efficient process, but it’s good for learning about BPMN 2.0 and Activiti.
用一個(gè)只包含開(kāi)始和結(jié)束事件的示例草草了結(jié)本章,那會(huì)讓人感到羞恥。所以讓我們用腳本任務(wù)和用戶任務(wù)來(lái)加強(qiáng)我們簡(jiǎn)單的訂書流程,這樣我們能看見(jiàn)Activiti 引擎的一些動(dòng)作。首先腳本任務(wù)將打印ISBN好,當(dāng)啟動(dòng)時(shí)將ISBN數(shù)字作為訂書流程的輸入。然后采用用戶任務(wù)手動(dòng)處理訂書流程。所以這并不是最有效的流程,但是對(duì)于學(xué)習(xí)BPMN 2.0和Activiti大有裨益。
Activiti provides the possibility to use the scripting language you want, but Groovy is supported by default. So we’ll use a line of Groovy to print the ISBN process variable. In code listing 1.3 a revised version of the book order process of code listing 1.1 is shown.
盡管Activiti為使用腳本語(yǔ)言提供了可能性,但是缺省,只提供對(duì)Groovy的支持。所以,我們將使用一行Groovy打印ISBN變量。代碼列表1.1的一個(gè)修訂的訂書流程如代碼列表1.3所示。
Listing 1.3 A book order process with a script and user task
targetNamespace="http://www.bpmnwithactiviti.org">
<process id="bookorder" name="bookorder">
<startEvent id="startevent1" name="Start"/>
<scriptTask id="scripttask1" #A
name="Validate order"
scriptFormat="groovy">
<script>
out:print "validating order for isbn " + isbn + "\n"; #1
</script>
</scriptTask>
<sequenceFlow id="sequenceflow1" name="Validate order"
sourceRef="startevent1" targetRef="scripttask1"/>
<userTask id="usertask1" name="Complete order"> #2
<documentation>book order user task</documentation>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>sales</formalExpression> #B
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
<sequenceFlow id="sequenceflow2" name="Sending to management"
sourceRef="scripttask1" targetRef="usertask1"/>
<endEvent id="endevent1" name="End"/>
<sequenceFlow id="sequenceflow3" name="Ending process"
sourceRef="usertask1" targetRef="endevent1"/>
</process>
</definitions>
#A Definition of a script task
#B Assign task to sales group
#1 Print the isbn
#2 Definition of a user task
With the two additional tasks added to the process definition, the number of lines of the XML file grows quite a bit. In the next chapter we’ll show the Activiti designer, which abstract you from the XML file when designing the process.
隨著兩項(xiàng)額外的任務(wù)添加到進(jìn)程的定義,該行數(shù)XML的文件增長(zhǎng)了不少。下一章我們將討論Activiti設(shè)計(jì)器,當(dāng)設(shè)計(jì)流程時(shí)時(shí),讓您從XML文件脫身。
The script task contains a out:print variable #1, which is a reserved word within the Activiti script task for printing text to the system console. Also notice that the isbn variable can be used directly in the script code without any additional programming.
腳本任務(wù)包含了out:print 變量 #1,它是Activiti腳本任務(wù)的保留字,它將文本打印到任務(wù)控制臺(tái)。也要注意在腳本里可以直接使用isbn變量,而不需任何額外的編程。
The user task #2 contains a potential owner definition, which means that the task can be claimed and completed by users that are part of the group sales. When we run this process in a minute, we can see in the Activiti Explorer that this user task is available in the task list for the user kermit, who is part of the sales group.
用戶任務(wù)#2包括一個(gè)潛在的用戶定義,它意味著Sales組的用戶可以領(lǐng)取和完成任務(wù)。當(dāng)我們運(yùn)行這個(gè)流程一會(huì)兒,在Activiti Explorer里,對(duì)于用戶kermit的任務(wù)列表,有任務(wù)可用,因?yàn)閗ermit是sales組的人。
Now that we added more logic to the process, we also need to change our unit test. One thing we need to add is a process variable isbn when starting the process. And to test if the user task is created, we need to query the Activiti engine database for user tasks that can be claimed by the user kermit. Let’s take a look at the changed unit test in code listing 1.4.
既然我們把更多的邏輯加入到流程,那么也需要改變我們的單元測(cè)試。。當(dāng)啟動(dòng)流程時(shí),需要增加的一件事是增加流程變量isbn。為了測(cè)試是否建立了用戶任務(wù),我們需要查詢Activiti引擎數(shù)據(jù)庫(kù)能夠由用戶kermit領(lǐng)取的任務(wù)。讓來(lái)看看代碼列表1.2里的變化的單元測(cè)試。
Listing 1.4 A unit test with a process variable and user task query
@Test
public void startBookOrder() {
ProcessEngine processEngine = new ProcessEngineBuilder()
.configureFromResource("activiti.cfg.xml")
.buildProcessEngine();
RuntimeService runtimeService =
processEngine.getRuntimeService();
RepositoryService repositoryService =
processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService(); #1
repositoryService.createDeployment()
.addClasspathResource("bookorder.bpmn20.xml")
.deploy();
Map<String, Object> variableMap = new HashMap<String, Object>();
variableMap.put("isbn", "123456");
ProcessInstance processInstance = #2
runtimeService.startProcessInstanceByKey( #2
"bookorder", variableMap); #2
assertNotNull(processInstance.getId());
System.out.println("id " + processInstance.getId() + " "
+ processInstance.getProcessDefinitionId());
List<Task> taskList = taskService.createTaskQuery() #A
.taskCandidateUser("kermit") #A
.list() #A
assertEquals(taskList.size(), 1);
for(Task task : taskList) {
System.out.println("found task " + task.getName());
}
}
}
#1 Get a TaskService instance
#2 Start a process with a variable
#A Find tasks available for kermit
The BookOrderTest unit test has been changed to start a process instance with a map of variables #2 that contains one variable with a name of isbn and a value of 123456. In addition, when the process instance has been started a TaskService instance #1 is used to retrieve the tasks available to claim by the user kermit. Because there is only one process instance running with one user task we test that the number of tasks retrieved is 1.
BookOrderTest單元測(cè)試已經(jīng)用一個(gè)變量#2的map而變化之后啟動(dòng)了一個(gè)流程實(shí)例。這個(gè)馬屁變量包含名為isbn,值為123456。另外,當(dāng)流程變量已經(jīng)啟動(dòng)TaskService實(shí)例#1時(shí),用來(lái)檢索用戶kermit所領(lǐng)取的任務(wù)。因?yàn)橹挥幸粋€(gè)流程變量運(yùn)行一個(gè)用戶任務(wù),所以檢索任務(wù)為1.
With the book order process in place and a new unit test we can run the unit test to see if our changes work. In the console you should see a similar output like:
因?yàn)橛啎鞒虦?zhǔn)備就緒,運(yùn)行一個(gè)新的單元測(cè)試來(lái)檢查我們的改變是否發(fā)生作用。在控制臺(tái),您應(yīng)該看到如下的輸出:
validating order for isbn 123456
id 112 bookorder:1
found task Complete order
The first line is printed by the Groovy script task in the running process instance. The last line confirms that one user task is available for claim for the user kermit. Because a user task is created we should be able to see this task in the Activiti Explorer. Confirm that Tomcat has been started as part of the installation procedure. If this is not the case you canstart Tomcat with the Ant build script in the setup directory with the following command:
由運(yùn)行的流程實(shí)例的Groovy腳本任務(wù)打印第一行。最后確定用戶Kermit可領(lǐng)取的用戶任務(wù)。因?yàn)橐呀⒁粋€(gè)用戶任務(wù),所以我們應(yīng)該在Activiti Explorer里看見(jiàn)這個(gè)任務(wù)。確認(rèn)Tomcat已經(jīng)作為安裝過(guò)程的一部分已經(jīng)啟動(dòng)。如果不是這種情況,你能夠用setup目錄下的Ant 構(gòu)建以下面的命令啟動(dòng)Tomcat:
ant tomcat.start