1 流程文檔部署生命週期
1.1 定義流程文檔
客戶端根據自己業務來定義流程文檔。
1.2 啓動流程引擎
流程引擎啓動後自動構建ProcessEngine實例對象,然後就可以通過該實例對象獲取服務類實例對象,比如TaskService。
1.3 部署流程文檔
調用流程文檔部署方法,會執行如下操作:
(1)將流程文檔中的元素解析爲Activiti的內部表示BaseElement實例。
(2)對BaseElement實例對象再次解析,進而將其轉成流程虛擬機中的ActivitiImpl實例對象或者TransitionImpl實例對象,該過程也是將BaseElement實例注入流程虛擬機的過程。
1.4 添加緩存
緩存流程定義實例。流程引擎默認開啓緩存。
2 DeploymentBuilder核心類
public interface DeploymentBuilder {
//根據文件流部署
DeploymentBuilder addInputStream(String var1, InputStream var2);
//根據文件位置部署
DeploymentBuilder addClasspathResource(String var1);
//根據字符串部署
DeploymentBuilder addString(String var1, String var2);
//根據字節數組部署
DeploymentBuilder addBytes(String var1, byte[] var2);
//根據壓縮包部署
DeploymentBuilder addZipInputStream(ZipInputStream var1);
//根據對象部署
DeploymentBuilder addBpmnModel(String var1, BpmnModel var2);
//如果調用,則不會針對BPMN 2.0 XSD進行XML模式驗證。
DeploymentBuilder disableSchemaValidation();
//如果被調用,將不會針對流程定義執行對流程定義在引擎上可執行的驗證(也就是說後續流程定義是否成功與否不保證)。
DeploymentBuilder disableBpmnValidation();
//部署名稱
DeploymentBuilder name(String var1);
//類別
DeploymentBuilder category(String var1);
//部署設置鍵屬性
DeploymentBuilder key(String var1);
//分佈式預留字段
DeploymentBuilder tenantId(String var1);
//禁止重複部署
DeploymentBuilder enableDuplicateFiltering();
DeploymentBuilder activateProcessDefinitionsOn(Date var1);
DeploymentBuilder deploymentProperty(String var1, Object var2);
//正式部署
Deployment deploy();
}
以上部署方式中最常用的幾種:
addInputStream()
一般用於部署單個bpmn文件。
addZipInputStream()
一般用於多文件部署,將其打成壓縮包。
addBpmnModel()
一般用於動態工作流引擎的自定義,這個在流程引擎的擴展上非常實用。
2.1 文件流部署
@Resource
private RepositoryService repositoryService;
/**
* 文件上傳 輸入流部署
* @param deploymentName
* @param file
* @throws Exception
*/
@PostMapping("/addProcess/{deploymentName}")
public ResponseEntity<Response> addProcess(@PathVariable String deploymentName, @RequestParam("file") MultipartFile file) throws Exception {
//enableDuplicateFiltering 防止重複部署 校驗資源名稱和資源內容
repositoryService.createDeployment().enableDuplicateFiltering().addInputStream(file.getOriginalFilename(), file.getInputStream())
.name(deploymentName).deploy();
return ResponseEntity.ok(buildSuccessResponse());
}
2.2 壓縮包部署
/**
* 壓縮包部署 會自動將其解壓並部署
* @param file
* @return
* @throws Exception
*/
@PostMapping("/addProcessByZip")
public ResponseEntity<Response> addProcessByZip(@RequestParam("file") MultipartFile file) throws Exception {
//解決壓縮包中文件名稱是中文的報錯問題,所以要設置GBK
ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(),Charset.forName("GBK"));
repositoryService.createDeployment().enableDuplicateFiltering().addZipInputStream(zipInputStream).enableDuplicateFiltering().deploy();
return ResponseEntity.ok(buildSuccessResponse());
}
注意:
壓縮包中文件名稱是中文的報錯問題,要設置編碼方式爲GBK。
2.3 BpmnModel方式部署
Model的創建可以用下圖描述
2.4 校驗BpmnModel實例對象
在我們組裝完BpmnModel後需要對該格式進行校驗,確保轉化的XML是正確的。Activiti提供了校驗的jar包
校驗代碼如下:
public boolean processValidator(BpmnModel bpmnModel){
ProcessValidatorFactory processValidatorFactory = new ProcessValidatorFactory();
ProcessValidator defaultProcessValidator = processValidatorFactory.createDefaultProcessValidator();
List<ValidationError> validate = defaultProcessValidator.validate(bpmnModel);
//如果錯誤集合中有數據,則證明校驗未通過
return validate.size() <= 0;
}
2.5 BpmnModel轉換流程文檔
public void convertToXML(BpmnModel bpmnModel) {
BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
String s = new String(bpmnXMLConverter.convertToXML(bpmnModel, "UTF-8"));
}
2.6 流程文檔轉BpmnModel
public void convertToBpmnModel() {
String resource = "/common.bpmn";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resource);
InputStreamSource inputStreamSource = new InputStreamSource(inputStream);
BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
BpmnModel bpmnModel = bpmnXMLConverter.convertToBpmnModel(inputStreamSource, true, false, "UTF-8");
}