1 流程圖
對於activiti的基本使用,推薦大家先看下官方文檔:https://www.activiti.org/userguide/index.html#bpmnParallelGateway
- 流程圖編輯
activiti 官方提供流程圖的應用:鏈接:https://github.com/Activiti/Activiti/releases/download/activiti-6.0.0/activiti-6.0.0.zip
當然,對於開發工具來說,也有相應的插件可以使用,此處不展開說明。
解壓後在wars 目錄下有如下三個應用,可以直接放在tomcat中運行。
其中activiti-app就提供了在線編輯流程圖的功能,默認的用戶名密碼:admin/test,可以看到如下界面:左邊第一個點擊進入就可以在線編輯,具備導入導出的能力。詳細使用可以參見:https://blog.csdn.net/yongboyhood/article/details/70833021
隨手畫的一個簡單流程圖,效果圖如下:
以上圖片對應的xml:
<?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/processdef">
<process id="privilegeApprove" name="privilegeApprove" isExecutable="true">
<documentation>權限申請審批流程</documentation>
<startEvent id="startEvent1" name="開始"></startEvent>
<exclusiveGateway id="exclusiveGateWay1"></exclusiveGateway>
<endEvent id="sid-D29B6096-0F99-4B9F-94D1-792C2A2337C0" name="審批結束"></endEvent>
<userTask id="approve-dot_delete" name="一級審批"></userTask>
<endEvent id="sid-FC011D0D-D5A6-41BA-A96D-84F699CFB126_delete" name="審批結束"></endEvent>
<sequenceFlow id="sid-17F421C4-3923-4A93-8100-DF2B1E48ED93_delete" sourceRef="approve-dot_delete" targetRef="sid-FC011D0D-D5A6-41BA-A96D-84F699CFB126_delete"></sequenceFlow>
<sequenceFlow id="noNeedApprove" name="N" sourceRef="exclusiveGateWay1" targetRef="sid-D29B6096-0F99-4B9F-94D1-792C2A2337C0">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${needApprove==false}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="needApprove" name="Y" sourceRef="exclusiveGateWay1" targetRef="approve-dot_delete">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${needApprove==true}]]></conditionExpression>
</sequenceFlow>
<userTask id="apply" name="提交申請"></userTask>
<sequenceFlow id="sid-3D265F3E-6C63-41D3-9601-50B1CF0A3C86" sourceRef="startEvent1" targetRef="apply"></sequenceFlow>
<sequenceFlow id="sid-AA8AEF9C-FBD8-41E6-87A8-1A12E48D9C5E" name="需要審批?" sourceRef="apply" targetRef="exclusiveGateWay1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_privilegeApprove">
<bpmndi:BPMNPlane bpmnElement="privilegeApprove" id="BPMNPlane_privilegeApprove">
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
注意,以上的xml文件,我將最後的圖片座標信息刪除了,但是不影響流程的正常使用。
xml文件如果更改過,會在表中重新部署,並且生成一個最新的版本。
根據act_re_procdef 可以看到,即使stringprocessTest.bpmn這個文件有過9次更改,每次都會重新記錄下來。
2 採坑記錄
-
採坑1:流程啓動沒有生成歷史表
正常來說,會生成帶 hi , re ,ru三大類的表,hi 代表的歷史表保存了我們完成及未完成流程的詳細記錄。採用最新activti 7版本啓動的時候,默認是不會生成歷史表的,需要在配置文件中配置:
spring.activiti.db-history-used=true
-
採坑2 : 動態生成的xml,導入編輯頁面 無法顯示
以下代碼是生成xml內容的關鍵代碼
BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
byte[] convertToXML = bpmnXMLConverter.convertToXML(model);
String bytes = new String(convertToXML);
動態生成的xml需要將
<bpmndi:BPMNShape>
標籤去掉,才能在Activiti編輯器中顯示
-
採坑3: 動態生成的流程沒有圖片信息
動態生成流程,需要調用代碼生成圖像信息,沒有下面代碼,將無法獲取流程圖片。如果是自動部署的xml文件,則會自動生成圖像信息,無需關心。
// 2. Generate graphical information
new BpmnAutoLayout(model).execute();
注意以上代碼可能會報錯
添加如下依賴就可以解決問題
<!--生成圖像信息-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>6.0.0.RC1</version>
</dependency>
關於如何動態生成流程圖,可以參見:
https://github.com/altraman00/activiti-dynamic-process
https://github.com/frederikheremans/activiti-dynamic-process
-
採坑4: An Authentication object not found
如果使用activiti 7 你可能會遇見如下錯誤:
大致意思就是SecurityContext 沒有認證對象,其實就是說當前沒有登錄用戶。因爲activiti 7 與身份相關的已經交給Spring Security,需要在調用 ProcessRuntime,TaskRuntime 的相關接口之前,在SecurityContext 中設置用戶。解決方案,調用如下代碼:
securityUtil.logInAs(userName);
而securityUtil 是一個簡化操作用戶登錄的工具類,可以參見:https://github.com/Activiti/activiti-examples/blob/master/activiti-api-basic-task-example/src/main/java/org/activiti/examples/SecurityUtil.java#L26
-
採坑5 :不允許訪問
如果使用activiti 7 你可能會遇見如下錯誤:不允許訪問
這個問題和採坑3 的有一定的關係,從異常來看也是Spring Security 拋出的錯誤,也是和身份管理相關,究其原因是因爲 當前登錄的用戶沒有 “ROLE_ACTIVITI_ADMIN” 這個角色,此角色會用來做權限判定,所以在查詢用戶時必須設置。具體可以參見官方案例:
如何接入自己系統的身份系統,可以參見這個鏈接:https://shimo.im/docs/DVhxGGPwwRwtJVp8