Flowable實戰(三)流程部署管理
一、流程定義的版本
當部署流程定義時,資料庫中的流程定義會是這個樣子:
id | key | name | version |
---|---|---|---|
myProcess:1:676 | myProcess | My important process | 1 |
如果我們現在部署同一個流程的更新版本(例如修改部分用戶任務),且保持流程定義的id不變,那麼流程定義表中會包含下面的記錄:
id | key | name | version |
---|---|---|---|
myProcess:1:676 | myProcess | My important process | 1 |
myProcess:2:870 | myProcess | My important process | 2 |
當調用執行流程時,會使用版本2的流程定義,因為這是這個流程定義的最新版本。
二、流程部署方式
2.1 指定項目內資源文件部署
比如在resources/下新建資源文件single-task.bpmn20.xml
部署示例程式碼:
// 資源路徑
String path = "single-task.bpmn20.xml";
// 創建部署構建器
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
// 添加資源
deploymentBuilder.addClasspathResource(path);
// 執行部署
deploymentBuilder.deploy();
// 驗證部署
long count = repositoryService.createProcessDefinitionQuery().processDefinitionKey("singleTask").count();
// count等於1,則說明部署成功
只要是在項目class目錄下的資源,都可以用這種方式部署。
這種方式一般用於開發測試階段,真正的生產環境,是通過與web管理頁面交互部署。
2.2 springboot自動部署
在springboot環境下,resources/processes目錄下的任何BPMN 2.0流程定義都會被自動部署。
2.3 介面方式
實際生產中,我們需要接收前端傳回來的流程定義數據,然後更新部署。
下面我們省去前端訪問介面參數的展示,僅演示後端處理程式碼。
// 從前端接收到的XML字元串
// 此處省去xml具體內容,可參考2.1的single-task.bpmn20.xml示例
// 注意將id定義為singleTask2,以便跟2.1的內容作出區別
String text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><definitions...</definitions>";
// 創建部署構建器
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
// 執行部署
deploymentBuilder.addString("single-task2.bpmn20.xml", text).deploy();
// 驗證部署
long count = repositoryService.createProcessDefinitionQuery().processDefinitionKey("singleTask2").count();
// count等於1,則說明部署成功
2.4 zip壓縮包
當我們需要一次同時部署多個資源時,可以通過zip壓縮包的部署方式。
String fileName = "path/multi-task.zip";
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(fileName));
repositoryService.createDeployment()
.name("multi-task.zip")
.addZipInputStream(inputStream)
.deploy();
三、流程管理
業務系統的第一步,就是需要一個列表可以瀏覽和管理流程定義。
3.1 獲取已部署流程列表
3.2 讀取流程圖片
顯然,管理頁面需要顯示流程圖片的功能。
Flowable引擎會在流程部署時,自動生成流程圖片。
獲取流程圖片方法:
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey("singleTask")
.singleResult();
String diagramResourceName = processDefinition.getDiagramResourceName();
InputStream imageStream = repositoryService.getResourceAsStream(
processDefinition.getDeploymentId(), diagramResourceName);
注意:如果不需要或不希望在部署時生成流程圖,可以在流程引擎配置中設置isCreateDiagramOnDeploy參數:
<property name="createDiagramOnDeploy" value="false" />
3.3 讀取流程定義的XML
// 根據processDefinitionId查詢
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
// 讀取資源流,resourceName為前端傳回的資源名稱
InputStream stream = repositoryService.getResourceAsStream(pd.getDeploymentId(), resourceName);
//讀取到的資源流再返回到前端
3.4 刪除部署
一個流程定義不是通過流程定義ID刪除,而是通過流程定義的部署ID刪除。在執行刪除時,會將和本次部署有關的資源一起刪除。
repositoryService.deleteDeployment(deploymentId, true);
四、小結