在activiti入門一(簡單介紹中),簡單介紹了repositoryService的主要作用是管理流程倉庫,例如部署,刪除,讀取流程資源等。本文就repositoryService再做下詳細(xì)的說明
常用的流程定義文件擴(kuò)展名有:bpmn20.xml和bpmn。流程定義的圖片一般用png格式。
部署流程資源的實(shí)現(xiàn)方式有很多種,如classPath,InputStream,字符串,zip壓縮包格式等。下面簡單介紹下各種方式的使用。
classPath方式,就是以class目錄為根路徑來讀取相應(yīng)的資源文件并進(jìn)行部署。路徑的命名方式是用斜杠”/”來分割包名。
具體實(shí)現(xiàn)看代碼
@Testpublic void classPathDeploymentTest(){ //定義資源文件的classPath String bpmnClassPath = "/bpmn/leave-dynamic-form.bpmn20.xml"; String pngClassPath = "/bpmn/leave-dynamic-form.png"; //創(chuàng)建部署構(gòu)建器 DeploymentBuilder deploymentBuilder = repositoryService.createDeployment(); //添加資源 deploymentBuilder.addClasspathResource(bpmnClassPath); deploymentBuilder.addClasspathResource(pngClassPath); //執(zhí)行部署 deploymentBuilder.deploy(); //驗(yàn)證部署是否成功 ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); long count = processDefinitionQuery.processDefinitionKey("leave-dynamic-from").count(); Assert.isTrue(count == 1); //讀取圖片文件 ProcessDefinition processDefinition = processDefinitionQuery.singleResult(); String diagramResourceName = processDefinition.getDiagramResourceName(); System.out.println(diagramResourceName);}
該方式的每行的代碼作用參看注釋。classPath一般用于開發(fā)階段,在真正的產(chǎn)品使用中,很少會用到這種方式。
InputStream方式,簡單點(diǎn)說就是把文件轉(zhuǎn)換為輸入流,來進(jìn)行部署。輸入流的來源,可以從classPath讀取,也可以從一個絕對路徑讀取,也可以根據(jù)上傳下載的文件來。
@Testpublic void inputStreamFromAbsolutePathTest() throws Exception{ String filePath = "/Users/whatlookingfor/code/workfocus/src/test/resources/bpmn/leave-dynamic-form.bpmn20.xml"; //讀取filePath文件作為一個輸入流 FileInputStream fileInputStream = new FileInputStream(filePath); repositoryService.createDeployment().addInputStream("leave-dynamic-form.bpmn20.xml",fileInputStream).deploy(); //驗(yàn)證部署是否成功 ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); long count = processDefinitionQuery.processDefinitionKey("leave-dynamic-form").count(); Assert.isTrue(count == 1);}
使用inputStream部署時,需要額外的參數(shù)是資源的名稱。以上的代碼是從一個絕對路徑去讀取文件,生成inputStream輸入流。
這種方式可能是在項(xiàng)目中最廣泛使用的,比如通過上傳bpmn文件來部署,或者給出文件URL地址來部署。
字符串的方式部署,就是把bpmn或者xml中的內(nèi)容轉(zhuǎn)換為純文本來作為資源部署的。
@Testpublic void stringDeploymentTest(){ String text = "xml內(nèi)容"; repositoryService.createDeployment().addString("leave-dynamic-form.bpmn20.xml",text); //驗(yàn)證是否部署成功 ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); long count = processDefinitionQuery.processDefinitionKey("leave-dynamic-form").count(); Assert.isTrue(count == 1);}
無論是classPath,InputStream,還是字符串方式,每次部署,都只能部署一個流程資源。而壓縮包方式的部署,則可以同時部署多個流程資源。壓縮包中有多少個資源文件,則就可以部署多少個,其實(shí)類似于classPath的批量
@Test public void zipStreamDeploymentTest(){ //讀取資源 InputStream zipStream = getClass().getClassLoader().getResourceAsStream("bpmn/leave.zip"); repositoryService.createDeployment().addZipInputStream(new ZipInputStream(zipStream)).deploy(); //統(tǒng)計已部署流程定義的數(shù)量 long count = repositoryService.createProcessDefinitionQuery().count(); Assert.isTrue(count == 1); //查詢部署記錄 Deployment deployment = repositoryService.createDeploymentQuery().singleResult(); Assert.notNull(deployment); String deploymentId = deployment.getId(); //驗(yàn)證資源文件是否都部署成功 Assert.notNull(repositoryService.getResourceAsStream(deploymentId,"leave-dynamic-form.bpmn")); Assert.notNull(repositoryService.getResourceAsStream(deploymentId,"leave-dynamic-form.png")); }
看代碼可以發(fā)現(xiàn),壓縮包方式,也是轉(zhuǎn)換為輸入流的。所以,在項(xiàng)目中,最常用的就是inputStream方式和壓縮包方式的整合。
通過上傳功能(格式包含bpmn,bpmn20.xml,zip,bar),然后根據(jù)文件格式,使用不同的部署方式。
其實(shí)在上面的代碼中,驗(yàn)證是否部署成功,已經(jīng)實(shí)現(xiàn)了讀取已部署的流程定義
@Testpublic void findDefinitionTest(){ ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); List<ProcessDefinition> processDefinitionList = processDefinitionQuery.listPage(0,10);}
InputStream getResourceAsStream(String deploymentId, String resourceName);
通過此方式,可以讀取資源流,然后根據(jù)各種不同的情況,輸入資源流即可。
List<String> getDeploymentResourceNames(String deploymentId);
讀取某個部署ID對應(yīng)的文件資源名稱
具體參看代碼
public void deleteDeployment(){ String deploymentId = "leave-dynamic-form:1:5004"; repositoryService.deleteDeployment(deploymentId,true);}
deleteDeployment方法有兩個參數(shù),第一個是部署ID,第二個參數(shù)true表示刪除時,會同時把流程相關(guān)的流程數(shù)據(jù)(包括運(yùn)行中的和已經(jīng)結(jié)束的流程實(shí)例)一并刪除掉。
流程模型的激活 repositoryService.activateProcessDefinitionById(processDefinitionId, true, null);
流程模型的掛起 repositoryService.suspendProcessDefinitionById(processDefinitionId, true, null);
第二個參數(shù)就是用來控制在掛起流程定義時,是否同時掛起對應(yīng)的流程實(shí)例。同樣的,流程實(shí)例也有激活和掛起,這里不做詳細(xì)說明,參看runtimeService介紹。
其實(shí)repositoryService的常用方法并不止上面那些。比如流程模型的創(chuàng)建,設(shè)置,刪除等等。這方面沒有使用過,因?yàn)槟P驮O(shè)計主要通過的是Activiti Modeler。當(dāng)然如果想要自己設(shè)計一套流程模型界面的話,repositoryService中的很多方法,你需要詳細(xì)研究下。