Activiti——管理流程定义(四)
Activiti——管理流程定義
1.設計流程定義文檔
1.1.流程圖
1.2.bpmn文件
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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"><process id="helloworld" name="HelloWorldProcess" isExecutable="true"><startEvent id="startevent1" name="Start"></startEvent><endEvent id="endevent1" name="End"></endEvent><userTask id="usertask1" name="提交申請" activiti:assignee="張三"></userTask><userTask id="usertask2" name="審批【部門經理】" activiti:assignee="李四"></userTask><userTask id="usertask3" name="審批【總經理】" activiti:assignee="王五"></userTask><sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow><sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow><sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow><sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_helloworld"><bpmndi:BPMNPlane bpmnElement="helloworld" id="BPMNPlane_helloworld"><bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"><omgdc:Bounds height="35.0" width="35.0" x="250.0" y="70.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="250.0" y="400.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"><omgdc:Bounds height="55.0" width="105.0" x="215.0" y="140.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"><omgdc:Bounds height="55.0" width="105.0" x="215.0" y="220.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3"><omgdc:Bounds height="55.0" width="105.0" x="215.0" y="296.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="267.0" y="105.0"></omgdi:waypoint><omgdi:waypoint x="267.0" y="140.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="267.0" y="195.0"></omgdi:waypoint><omgdi:waypoint x="267.0" y="220.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="267.0" y="275.0"></omgdi:waypoint><omgdi:waypoint x="267.0" y="296.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="267.0" y="351.0"></omgdi:waypoint><omgdi:waypoint x="267.0" y="400.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>說明:流程定義文檔有兩部分組成。
1)bpmn文件
? ? ?流程規則文件。在部署后,每次系統啟動時候都會被解析,把內容封裝成流程定義放入項目緩存中。
Activiti框架結合這個xml文件自動管理流程,流程的執行就是按照bpmn文件定義的規則執行的,
bpmn文件是給計算機執行用的。
2)展示流程圖的圖片
? ? ?在系統需要展示流程的進展圖片,圖片時給用戶看的。
2.部署流程定義(classpath路徑加載文件)
? ?【代碼片段】
/*** 部署流程定義*/@Testpublic void deploymentProcessDefinition() {Deployment deployment = processEngine.getRepositoryService()// 與流程定義和部署對象相關的Service.createDeployment()// 創建一個部署對象.name("HelloWorld入門程序")// 添加部署的名稱.addClasspathResource("diagrans/helloword.bpmn")// 從類路徑(classpath)下加載文件,一次只能加載一個文件.addClasspathResource("diagrans/helloword.png")// 從類路徑(classpath)下加載文件,一次只能加載一個文件.deploy();// 完成部署}說明:
1)先獲取流程引擎對象,在創建時會自動加載classpath下的activiti.cfg.xml
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();2)首先獲得默認的流程引擎,通過流程引擎獲取了一個RepositoryService對象(倉庫對象)。
3)由倉庫的服務對象產生一個部署對象配置對象,用來封裝部署操作的相關配置。
4)這是一個鏈式編程,在部署配置對象中設置顯示名,上傳流程定義規則文件。
5)向數據庫表中存放流程定義的規則信息。
6)這一步在數據庫中將操作三張表:
| act_re_deployment | 部署對象表 | 存放流程定義的顯示名和部署時間,每部署一次增加一條記錄。 |
| act_re_procdef | 流程定義表 | 存放流程定義的屬性信息,部署每個新的流程定義都會在這張表中增加一條記錄 注意:當流程定義的key相同的情況下,使用的是版本升級。 |
| act_ge_bytearray | 資源文件表 | 存儲流程定義相關的部署信息。即在流程定義文檔的存放地。每部署一次就會增加兩條記錄,一條是bpmn規則文件的,另一條是圖片的(如果部署時只指定了bpmn一個文件,activiti會在部署時解析bpmn文件內容自動生成流程圖),兩個文件不是很大,都是二進制形式存儲在數據庫中。 |
3.部署流程定義(zip格式文件)
/*** 部署流程定義(zip格式文件)*/@Testpublic void deploymentProcessDefinitionZip() {//加載zip資源文件InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrans/helloword.zip");ZipInputStream zipInputStream = new ZipInputStream(in);Deployment deployment = processEngine.getRepositoryService()// 與流程定義和部署對象相關的Service.createDeployment()// 創建一個部署對象.name("程序定義(ZIP)")// 添加部署的名稱.addZipInputStream(zipInputStream)// 從類路徑(classpath)下加載文件,一次只能加載一個文件 .deploy();// 完成部署System.out.println("=======打印部署對象信息========");System.out.println("流程ID:" + deployment.getId());// 流程ID:10001System.out.println("流程部署名稱:" + deployment.getName());// 流程部署名稱:程序定義(ZIP)System.out.println(deployment);// DeploymentEntity[id=10001, name=程序定義(ZIP)]}
/*** 查詢流程定義*/@Testpublic void findProcessDefinition(){List<ProcessDefinition> list =processEngine.getRepositoryService().createProcessDefinitionQuery()//創建流程定義查詢對象/**指定查詢條件 where條件*///.deploymentId(deploymentId)//使用部署對象的ID查詢//.processDefinitionId(processDefinitionId)//使用流程定義ID查詢//.processDefinitionKey(processDefinitionKey)//使用流程定義的Key查詢//.processDefinitionNameLike(processDefinitionNameLike)//使用流程定義名稱進行模糊查詢/**排序*///.orderByProcessDefinitionVersion().asc()//安照版本的升序排列.orderByProcessDefinitionName().desc()//按照流程定義名稱降序排列/**返回結果集*/.list();//返回List結果集//.listPage(firstResult, maxResults)//分頁查詢//.singleResult();//返回單個記錄結果集//.count();//返回記錄統計數if(list!=null && list.size()>0){for(ProcessDefinition processDef:list){System.out.println("流程定義ID:"+processDef.getId());//流程定義的key+版本+隨機數System.out.println("流程定義名稱:"+processDef.getName());//對應helloworld.bpmn文件中的name屬性值System.out.println("流程定義的Key:"+processDef.getKey());//對應helloworld.bpmn文件中的id屬性值System.out.println("流程定義的版本:"+processDef.getVersion());//當流程定義的key值相同的情況下,版本升級,默認1System.out.println("資源名稱bpmn文件:"+processDef.getResourceName());System.out.println("資源名稱png文件:"+processDef.getDiagramResourceName());System.out.println("部署對象ID:"+processDef.getDeploymentId());System.out.println("===========快樂分割線===============");}}}說明:
1)流程定義和部署對象相關的Service都是RepositoryService.
2)創建流程定義查詢對象,可以在ProcessDefinitionQuery上設置查詢的相關參數
3)調用ProccessDefinitionQuery對象的list方法,執行查詢,獲得符合條件的流程定義列表
4)由運行結果可以看出:
? ? Key和Name的值為:bpmn文件 process節點的id和name屬性值
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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"><process id="helloworld" name="HelloWorldProcess" isExecutable="true"><startEvent id="startevent1" name="Start"></startEvent><endEvent id="endevent1" name="End"></endEvent><userTask id="usertask1" name="提交申請" activiti:assignee="張三"></userTask><userTask id="usertask2" name="審批【部門經理】" activiti:assignee="李四"></userTask><userTask id="usertask3" name="審批【總經理】" activiti:assignee="王五"></userTask><sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow><sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow><sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow><sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow></process>
.........
5)key屬性被用來區分不同的流程定義。
6)帶有特定Key的流程定義第一次部署時,version為1,之后每次部署都會在當前最高版本號上加1
7)id的值的生成規則為(processDefinitionKey):(processDefinitionVersion):(generated-id),這里的generated-id是一個
? ? 自動生成的唯一的數字。
8)重復部署一次,deploymentid的值以一定的形式變化,規則act_ge_property表生成
5.刪除流程定義
/*** 刪除流程定義*/@Testpublic void deleteProcessDefinition(){String deploymentId = "30001";//部署流程定義IDprocessEngine.getRepositoryService().deleteDeployment(deploymentId);//不帶級聯的刪除只能刪除沒用啟動的流程,如果流程啟動就會拋出異常//.deleteDeployment(deploymentId, true);//級聯刪除(不管是有無啟動的流程都能刪除)System.out.println("刪除成功!");}
說明:
1)因為刪除的是流程定義,而流程定義的部署時屬于倉庫服務的,所以應該先得到RepositoryService
2)如果該流程定義下沒有正在運行的流程,則可以用普通刪除,如果是有關聯的信息(啟動的流程)可以使用級聯刪除。
項目開發中使用級聯刪除的情況比較多,刪除操作一般只開放給超級管理員使用。
6.獲取流程定義文檔的資源(查看流程圖附件)
/*** 獲取流程定義文檔資源* @throws IOException*/@Testpublic void viewProcessPic() throws IOException {//流程部署IDString deploymentId = "40001";//圖片資源名稱String resourceName = null;List<String> list =processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);if(list!=null&& list.size()>0){for(String val :list){System.out.println(val);if(val.endsWith(".png")){resourceName = val;}}}if(resourceName == null){throw new RuntimeException("resourceName is null");}InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, resourceName);FileUtils.copyInputStreamToFile(in, new File("F:/helloworld.png"));}
說明:
1)deploymentId為流程部署ID。
2)resourceName 為 act_ge_bytearry表中NAME列的值。
3)使用repositoryService的getDeploymentResourceNames 方法可以獲取指定部署下的所有文件名稱。
4)使用repositoryService的getResouceAsStream 方法傳入部署ID和資源圖片名稱可以獲取部署下指定名稱文件的輸入流。
5)最后的有關IO流的操作,使用FileUtils工具的 copyInputStreamToFile方法(apache common-io包)完成流程到文件的拷貝,
將資源文件以流的形式輸出到指定文件夾下。
7.附加功能:查詢最新版的流程定義
/*** 附加功能:查詢最新版的流程定義*/@Testpublic void findLastVersionProcessDefinition(){//方式①List<ProcessDefinition> list =processEngine.getRepositoryService().createProcessDefinitionQuery().orderByProcessDefinitionVersion().asc().list();//使用key合并,保留最新版本Map<String,ProcessDefinition> linkMap = new LinkedHashMap<String, ProcessDefinition>();if(list!=null && list.size()>0){for(ProcessDefinition processDef:list){linkMap.put(processDef.getKey(), processDef);}}List<ProcessDefinition> outList =new ArrayList<ProcessDefinition>(linkMap.values());if(outList!=null && outList.size()>0){for(ProcessDefinition processDef:outList){System.out.println("流程定義ID:"+processDef.getId());//流程定義的key+版本+隨機數System.out.println("流程定義名稱:"+processDef.getName());//對應helloworld.bpmn文件中的name屬性值System.out.println("流程定義的Key:"+processDef.getKey());//對應helloworld.bpmn文件中的id屬性值System.out.println("流程定義的版本:"+processDef.getVersion());//當流程定義的key值相同的情況下,版本升級,默認1System.out.println("資源名稱bpmn文件:"+processDef.getResourceName());System.out.println("資源名稱png文件:"+processDef.getDiagramResourceName());System.out.println("部署對象ID:"+processDef.getDeploymentId());System.out.println("===========快樂分割線===============");}}//方式② 更簡便的方式ProcessDefinition lastVersionProcessDefinition = processEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("helloworld").latestVersion()//設置獲取最后一個版本的標記.singleResult();if(lastVersionProcessDefinition!=null){System.out.println("流程定義ID:"+lastVersionProcessDefinition.getId());//流程定義的key+版本+隨機數System.out.println("流程定義名稱:"+lastVersionProcessDefinition.getName());//對應helloworld.bpmn文件中的name屬性值System.out.println("流程定義的Key:"+lastVersionProcessDefinition.getKey());//對應helloworld.bpmn文件中的id屬性值System.out.println("流程定義的版本:"+lastVersionProcessDefinition.getVersion());//當流程定義的key值相同的情況下,版本升級,默認1System.out.println("資源名稱bpmn文件:"+lastVersionProcessDefinition.getResourceName());System.out.println("資源名稱png文件:"+lastVersionProcessDefinition.getDiagramResourceName());System.out.println("部署對象ID:"+lastVersionProcessDefinition.getDeploymentId());}}
8.附加功能:刪除流程定義(刪除key相同的所有不同版本的流程定義)
/*** 附加功能:刪除流程定義(刪除key相同的所有不同版本的流程定義)*/@Testpublic void deleteProcessDefinitionByKey() {// 流程定義的KeyString processDefinitionKey = "helloworld";// 先使用流程定義的key查詢流程定義,查詢出 所有版本List<ProcessDefinition> list = processEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey(processDefinitionKey)// 使用流程定義的key查詢.list();// 遍歷,獲取每個流程定義的部署IDif (list != null && list.size() > 0) {for (ProcessDefinition processDefinition : list) {//根據迭代的每個部署ID進行級聯刪除processEngine.getRepositoryService().deleteDeployment(processDefinition.getDeploymentId(), true);System.out.println("刪除部署定義:" + processDefinition.getDeploymentId());}}System.out.println("刪除成功!");}
9.總結
Deployment 部署對象
1)一次部署的多個文件的信息,對于不需要的流程可以刪除和修改。
2)對應的表:
act_re_deployment? 部署對象表
act_re_procdef 流程定義表
act_ge_bytearray 資源文件表
act_ge_property 主鍵生成策略表
ProcessDefinition 流程定義
1)解析bpmn后得到的流程定義規則的信息,工作流系統就是按照流程定義的規則執行的。
總結
以上是生活随笔為你收集整理的Activiti——管理流程定义(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洱海包车一日游多少钱
- 下一篇: 有什么经济实惠的吉普车?