網(wǎng)路冷眼@BlogJava

          熙熙攘攘一閑人 以冷靜的眼光觀察技術(shù)
          posts - 88, comments - 193, trackbacks - 0, articles - 28
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          1.5.4 Implementing a simple book order process (實(shí)現(xià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.

          用一個只包含開始和結(jié)束事件的示例草草了結(jié)本章,那會讓人感到羞恥。所以讓我們用腳本任務(wù)和用戶任務(wù)來加強(qiáng)我們簡單的訂書流程,這樣我們能看見Activiti 引擎的一些動作。首先腳本任務(wù)將打印ISBN好,當(dāng)啟動時將ISBN數(shù)字作為訂書流程的輸入。然后采用用戶任務(wù)手動處理訂書流程。所以這并不是最有效的流程,但是對于學(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為使用腳本語言提供了可能性,但是缺省,只提供對Groovy的支持。所以,我們將使用一行Groovy打印ISBN變量。代碼列表1.1的一個修訂的訂書流程如代碼列表1.3所示。

          Listing 1.3 A book order process with a script and user task

          <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"  
              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的文件增長了不少。下一章我們將討論Activiti設(shè)計(jì)器,當(dāng)設(shè)計(jì)流程時時,讓您從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ù)控制臺。也要注意在腳本里可以直接使用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包括一個潛在的用戶定義,它意味著Sales組的用戶可以領(lǐng)取和完成任務(wù)。當(dāng)我們運(yùn)行這個流程一會兒,在Activiti Explorer里,對于用戶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.

          既然我們把更多的邏輯加入到流程,那么也需要改變我們的單元測試。。當(dāng)啟動流程時,需要增加的一件事是增加流程變量isbn。為了測試是否建立了用戶任務(wù),我們需要查詢Activiti引擎數(shù)據(jù)庫能夠由用戶kermit領(lǐng)取的任務(wù)。讓來看看代碼列表1.2里的變化的單元測試。

          Listing 1.4 A unit test with a process variable and user task query

          public class BookOrderTest { 
            @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單元測試已經(jīng)用一個變量#2的map而變化之后啟動了一個流程實(shí)例。這個馬屁變量包含名為isbn,值為123456。另外,當(dāng)流程變量已經(jīng)啟動TaskService實(shí)例#1時,用來檢索用戶kermit所領(lǐng)取的任務(wù)。因?yàn)橹挥幸粋€流程變量運(yùn)行一個用戶任務(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)行一個新的單元測試來檢查我們的改變是否發(fā)生作用。在控制臺,您應(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)橐呀⒁粋€用戶任務(wù),所以我們應(yīng)該在Activiti Explorer里看見這個任務(wù)。確認(rèn)Tomcat已經(jīng)作為安裝過程的一部分已經(jīng)啟動。如果不是這種情況,你能夠用setup目錄下的Ant 構(gòu)建以下面的命令啟動Tomcat:

          ant tomcat.start

          主站蜘蛛池模板: 大安市| 阿拉善右旗| 扎兰屯市| 个旧市| 灵川县| 苍梧县| 昌乐县| 麻栗坡县| 太湖县| 合山市| 沙坪坝区| 涞水县| 黑河市| 奉贤区| 河源市| 新宁县| 丘北县| 平远县| 始兴县| 门源| 临潭县| 阿坝县| 常德市| 桐庐县| 苗栗市| 改则县| 太康县| 临海市| 塔城市| 孟州市| 手游| 十堰市| 龙州县| 双桥区| 许昌县| 九龙城区| 九江县| 吴江市| 革吉县| 海南省| 海安县|