2017.2.20 activiti實戰--第二章--搭建Activiti開發環境及簡單示例(二)簡單示例


學習資料:《Activiti實戰》

 

第一章 搭建Activiti開發環境及簡單示例

2.5 簡單流程圖及其執行過程

(1)leave.bpmn

后綴名必須是bpmn。安裝了activiti的插件(eclipse:activiti designer,intellij:actiBPM)后,就可以用設計器的形式進行流程的繪制。

 

注意:在activiti部署的時候,必須同時存在leave.bpmn及leave.png。因此在eclipse設置:window—>preferences—>activiti->save->選中create process definition image when saving the diagram

繪制完leave.bpmn后,就會自動生成對應的png文件。(intellij的插件actiBPM還沒有找到設置的地方)

 

(2)leave.xml

 eclipse中還可以以xml形式查看(目前還沒有找到intellij中如何以xml格式查看,而且intellij中activiti插件的使用明顯比eclipse中使用的用戶體驗差)。對應的xml文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
 3   <process id="leave" name="Leave">
 4     <documentation>請假流程</documentation>
 5     <startEvent id="startevent1" name="Start"></startEvent>
 6     <userTask id="deptLeaveAudit" name="領導審批" activiti:assignee="leader"></userTask>
 7     <userTask id="hrAudit" name="人事審批" activiti:assignee="hr"></userTask>
 8     <userTask id="reportBack" name="銷假" activiti:assignee="${proposer}"></userTask>
 9     <endEvent id="endevent1" name="End"></endEvent>
10     <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="deptLeaveAudit"></sequenceFlow>
11     <sequenceFlow id="flow2" name="" sourceRef="deptLeaveAudit" targetRef="hrAudit"></sequenceFlow>
12     <sequenceFlow id="flow3" name="" sourceRef="hrAudit" targetRef="reportBack"></sequenceFlow>
13     <sequenceFlow id="flow4" name="" sourceRef="reportBack" targetRef="endevent1"></sequenceFlow>
14   </process>
15 </definitions>

 

(3)測試代碼

獲取processEngine --》 獲取各種Service --》部署bpmn文件 --》 創建流程定義 --》 執行流程定義 --》 獲取某個用戶擁有的task --》用戶簽收該task --》用戶完成該task --》 查看歷史信息

 1 public class SayHelloToLeaveTest {
 2 
 3     @Test
 4     public void testStartProcess() throws Exception {
 5         ProcessEngine processEngine = ProcessEngineConfiguration
 6                 .createStandaloneInMemProcessEngineConfiguration()
 7                 .buildProcessEngine();
 8 
 9         RepositoryService repositoryService = processEngine.getRepositoryService();
10         String bpmnFileName = "me/kafeitu/activiti/helloworld/SayHelloToLeave.bpmn";
11         repositoryServic.createDeployment()
.addInputStream(
"SayHelloToLeave.bpmn",this.getClass().getClassLoader().getResourceAsStream(bpmnFileName))
.deploy();
17 18 ProcessDefinition processDefinition = repositoryService 19 .createProcessDefinitionQuery().singleResult(); 20 assertEquals("SayHelloToLeave", processDefinition.getKey()); 21 22 RuntimeService runtimeService = processEngine.getRuntimeService(); 23 24 Map<String, Object> variables = new HashMap<String, Object>(); 25 variables.put("applyUser", "employee1"); 26 variables.put("days", 3); 27 28 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("SayHelloToLeave", variables); 30 assertNotNull(processInstance); 31 System.out.println("pid=" + processInstance.getId() + ", pdid=" 32 + processInstance.getProcessDefinitionId()); 33 34 TaskService taskService = processEngine.getTaskService(); 35 Task taskOfDeptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult(); 37 assertNotNull(taskOfDeptLeader); 38 assertEquals("領導審批", taskOfDeptLeader.getName()); 39 40 taskService.claim(taskOfDeptLeader.getId(), "leaderUser"); 41 variables = new HashMap<String, Object>(); 42 variables.put("approved", true); 43 taskService.complete(taskOfDeptLeader.getId(), variables); 44 45 taskOfDeptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult(); 47 assertNull(taskOfDeptLeader); 48 49 HistoryService historyService = processEngine.getHistoryService(); 50 long count = historyService.createHistoricProcessInstanceQuery().finished().count(); 52 assertEquals(1, count); 53 } 54 }

 

2.6 Activiti Explorer

Activiti Explorer是activiti為了讓開發人員快速入門所設計的示例程序。

查看之前下載的activiti-5.22.0.zip,可以看到:

 

(1)運行explorer

將activiti-explorer.war包部署到tomcat下。訪問地址:http://localhost:8080/activiti-explorer

默認有一個賬號:kermit/kermit

 

登錄進去后的頁面:(詳細的使用參看用戶手冊)

 

(2)修改explorer的參數

沒做任何修改的explorer,db.properties默認的h2數據庫,如果要用自己的數據庫,還要修改db.properties。

然后把databse里的create文件夾里的sql文件,在自己的數據庫里執行一遍。執行完后,表結構如圖。

 

(3)使用explorer(存在中文亂碼問題)

進入explorer的頁面,選擇流程。點擊流程設計工作區。
 
 
然后點擊新建model。這個model就是我們創建的一個流程。
 
 
 
在設計區畫好,並設置好屬性之后,點擊保存並關閉editor。就會回到這里的流程設計工作區。
 
右邊的處理模型,選項選擇部署。就會跳出如下框:
 
然后回到流程tab頁,就可以看到自己部署的流程定義了。
 
點擊右上角的啟動流程。可以看到有一個任務被標紅了,表示目前進展到這里。
 
如果在畫流程圖的時候第一個框設置了assignee,比如我設置成了我自己,那么啟動流程之后,我的任務欄里會多出來一個任務。
點擊下方的完成任務。
 
回到流程tab頁里。然后可以看到流程里的紅色已經轉移,說明已經傳遞到下一個人去完成任務了。
 
 
這是explorer的基本使用,其他一些詳細的使用不再贅述。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM