javascript
Activiti 流程配置可视化:SpringBoot 集成 Activiti6 + Activiti Modeler
Activiti 快速入門教程:SpringBoot 集成 Activiti6 + Activiti Modeler 流程配置可視化
- 7大服務與核心表
- 23張表概覽
- 7大核心服務(重要)
- 加依賴
- 內部日志
- 初始化 ProcessEngine
- 代碼初始化
- activiti.cfg.xml 初始化
- 我的初始化示例
- 集成 Activiti Modeler
- 下載源碼
- 新增依賴
- 代碼集成
- 前端代碼集成
- 后端代碼集成
- 結構
- 代碼修改
- RepositoryService 注入問題
- 啟動器注解修改
- 請求路徑調整
- 修改 ModelSaveRestResource.java
- 新增 createModel 接口
- 訪問頁面
- 漢化
- 參考
7大服務與核心表
轉載:Activiti 23張表及7大服務詳解
23張表概覽
Activiti使用到的表都是ACT_開頭的。
PS:表中參數具體意思,請直接看轉載
ACT_RE_*:
RE 表示repository(存儲),RepositoryService接口所操作的表。帶此前綴的表包含的是靜態信息,如,流程定義,流程的資源(圖片,規則等)。
ACT_RU_*:
RU 表示runtime,運行時表-RuntimeService。這是運行時的表存儲著流程變量,用戶任務,變量,職責(job)等運行時的數據。Activiti只存儲實例執行期間的運行時數據,當流程實例結束時,將刪除這些記錄。這就保證了這些運行時的表小且快。
ACT_ID_*:
ID 表示identity (組織機構),IdentityService接口所操作的表。用戶記錄,流程中使用到的用戶和組。這些表包含標識的信息,如用戶,用戶組,等等。
ACT_HI_*:
HI 表示history,歷史數據表,HistoryService。就是這些表包含著流程執行的歷史相關數據,如結束的流程實例,變量,任務,等等
ACT_GE_*:
全局通用數據及設置(general),各種情況都使用的數據。
| 1 | act_ge_bytearray | 二進制數據表 |
| 2 | act_ge_property | 二屬性數據表存儲整個流程引擎級別的數據,初始化表結構時,會默認插入三條記錄數據表 |
| 3 | act_hi_actinst | 歷史節點表 |
| 4 | act_hi_attachment | 歷史附件表 |
| 5 | act_hi_comment | 歷史意見表 |
| 6 | act_hi_identitylink | 歷史流程人員表 |
| 7 | act_hi_detail | 歷史詳情表,提供歷史變量的查詢 |
| 8 | act_hi_procinst | 歷史流程實例表 |
| 9 | act_hi_taskinst | 歷史任務實例表 |
| 10 | act_hi_varinst | 歷史變量表 |
| 11 | act_id_group | 用戶組信息表 |
| 12 | act_id_info | 用戶擴展信息表 |
| 13 | act_id_membership | 用戶與用戶組對應信息表 |
| 14 | act_id_user | 用戶信息表 |
| 15 | act_re_deployment | 部署信息表 |
| 16 | act_re_model | 流程設計模型部署表 |
| 17 | act_re_procdef | 流程定義數據表 |
| 18 | act_ru_event_subscr | throwEvent、catchEvent時間監聽信息表 |
| 19 | act_ru_execution | 運行時流程執行實例表 |
| 20 | act_ru_identitylink | 運行時流程人員表,主要存儲任務節點與參與者的相關信息 |
| 21 | act_ru_job | 運行時定時任務數據表 |
| 22 | act_ru_task | 運行時任務節點表 |
| 23 | act_ru_variable | 運行時流程變量數據表 |
7大核心服務(重要)
所謂核心服務,就是通過調用服務中開放的接口,去獲取自己所需要的數據,甚至改變流程
| RepositoryService | Activiti 中每一個不同版本的業務流程的定義都需要使用一些定義文件,部署文件和支持數據 ( 例如 BPMN2.0 XML 文件,表單定義文件,流程定義圖像文件等 ),這些文件都存儲在 Activiti 內建的 Repository 中。Repository Service 提供了對 repository 的存取服務。 |
| RuntimeService | 在 Activiti 中,每當一個流程定義被啟動一次之后,都會生成一個相應的流程對象實例。Runtime Service 提供了啟動流程、查詢流程實例、設置獲取流程實例變量等功能。此外它還提供了對流程部署,流程定義和流程實例的存取服務。 |
| TaskService | 在 Activiti 中業務流程定義中的每一個執行節點被稱為一個 Task,對流程中的數據存取,狀態變更等操作均需要在 Task 中完成。Task Service 提供了對用戶 Task 和 Form 相關的操作。它提供了運行時任務查詢、領取、完成、刪除以及變量設置等功能。 |
| HistoryService | History Service 用于獲取正在運行或已經完成的流程實例的信息,與 Runtime Service 中獲取的流程信息不同,歷史信息包含已經持久化存儲的永久信息,并已經被針對查詢優化。 |
| IdentityService | Activiti 中內置了用戶以及組管理的功能,必須使用這些用戶和組的信息才能獲取到相應的 Task。Identity Service 提供了對 Activiti 系統中的用戶和組的管理功能。 |
| FormService | Activiti 中的流程和狀態 Task 均可以關聯業務相關的數據。通過使用 Form Service 可以存取啟動和完成任務所需的表單數據并且根據需要來渲染表單。 |
| ManagementService | Management Service 提供了對 Activiti 流程引擎的管理和維護功能,這些功能不在工作流驅動的應用程序中使用,主要用于 Activiti 系統的日常維護。 |
加依賴
<!-- Activity --> <dependency><groupId>org.activiti</groupId><artifactId>activiti-engine</artifactId><version>6.0.0</version> </dependency>內部日志
Activiti 使用 SLF4J 作為內部日志框架。在這個例子中,我們使用 log4j 作為 SLF4J 的實現。
加依賴
<!-- 日志 --> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version> </dependency> <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.21</version> </dependency>resource 目錄下新建文件 log4j.properties
log4j.rootLogger=DEBUG, ACT log4j.appender.ACT=org.apache.log4j.ConsoleAppender log4j.appender.ACT.layout=org.apache.log4j.PatternLayout log4j.appender.ACT.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n初始化 ProcessEngine
代碼初始化
// 流程引擎配置 ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration().setJdbcUrl(url).setJdbcUsername(username).setJdbcPassword(password).setJdbcDriver(driverClassName)// 初始化基礎表,不需要的可以改為 DB_SCHEMA_UPDATE_FALSE.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); // 初始化流程引擎對象 ProcessEngine processEngine = cfg.buildProcessEngine();activiti.cfg.xml 初始化
代碼部分
// 流程引擎配置 ProcessEngineConfiguration cfg = ProcessEngineConfiguration// 根據文件名獲取配置文件//.createProcessEngineConfigurationFromResource("activiti.cfg.xml");// 獲取默認配置文件,默認的就是 activiti.cfg.xml.createProcessEngineConfigurationFromResourceDefault()// 初始化基礎表,不需要的可以改為 DB_SCHEMA_UPDATE_FALSE.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); // 初始化流程引擎對象 ProcessEngine processEngine = cfg.buildProcessEngine();新建 activiti.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"><property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"/><property name="jdbcDriver" value="com.mysql.jdbc.Driver"/><property name="jdbcUsername" value="root"/><property name="jdbcPassword" value="123456"/><property name="databaseSchemaUpdate" value="true"/></bean> </beans>我的初始化示例
我的配置文件 ProcessEngineConfig.java
依賴
- spring-boot-configuration-processor 加載配置文件
- lomok 簡化 java 代碼
核心的 7 大服務在這里注冊成 bean 后,之后就可以直接使用了
/*** 流程引擎配置文件* @author: linjinp* @create: 2019-10-21 16:49**/ @Configuration @ConfigurationProperties(prefix = "spring.datasource") @Data public class ProcessEngineConfig {private Logger logger = LoggerFactory.getLogger(DruidDatsSourceConfig.class);// 從 yml 配置文件中獲取 mysql 配置信息@Value("${spring.datasource.url}")private String url;@Value("${spring.datasource.driver-class-name}")private String driverClassName;@Value("${spring.datasource.username}")private String username;@Value("${spring.datasource.password}")private String password;@Value("${spring.datasource.publicKey}")private String publicKey;/*** 初始化流程引擎* @return*/@Primary@Beanpublic ProcessEngine initProcessEngine() {logger.info("=============================ProcessEngineBegin=============================");// 流程引擎配置ProcessEngineConfiguration cfg = null;try {cfg = new StandaloneProcessEngineConfiguration().setJdbcUrl(url).setJdbcUsername(username)// 對密碼進行解密.setJdbcPassword(ConfigTools.decrypt(publicKey, password)).setJdbcDriver(driverClassName)// 初始化基礎表,不需要的可以改為 DB_SCHEMA_UPDATE_FALSE.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)// 默認郵箱配置// 發郵件的主機地址,先用 QQ 郵箱.setMailServerHost("smtp.qq.com")// POP3/SMTP服務的授權碼.setMailServerPassword("xxxxxxx")// 默認發件人.setMailServerDefaultFrom("836369078@qq.com")// 設置發件人用戶名.setMailServerUsername("管理員");} catch (Exception e) {e.printStackTrace();}// 初始化流程引擎對象ProcessEngine processEngine = cfg.buildProcessEngine();logger.info("=============================ProcessEngineEnd=============================");return processEngine;}//八大接口// 業務流程的定義相關服務@Beanpublic RepositoryService repositoryService(ProcessEngine processEngine){return processEngine.getRepositoryService();}// 流程對象實例相關服務@Beanpublic RuntimeService runtimeService(ProcessEngine processEngine){return processEngine.getRuntimeService();}// 流程任務節點相關服務@Beanpublic TaskService taskService(ProcessEngine processEngine){return processEngine.getTaskService();}// 流程歷史信息相關服務@Beanpublic HistoryService historyService(ProcessEngine processEngine){return processEngine.getHistoryService();}// 表單引擎相關服務@Beanpublic FormService formService(ProcessEngine processEngine){return processEngine.getFormService();}// 用戶以及組管理相關服務@Beanpublic IdentityService identityService(ProcessEngine processEngine){return processEngine.getIdentityService();}// 管理和維護相關服務@Beanpublic ManagementService managementService(ProcessEngine processEngine){return processEngine.getManagementService();}// 動態流程服務@Beanpublic DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine){return processEngine.getDynamicBpmnService();}//八大接口 end }成功生成表
這個時候,就已經可以通過 7 大服務提供的接口來操作 Activiti 了
集成 Activiti Modeler
下載源碼
我這里選用的是 Activiti 5.23.0 版本的頁面,下載 zip,解壓
Activiti 5.23.0 源碼
新增依賴
下面 3 個是為了兼容 Activiti6 新增的 jar 包,如果是 Activiti5 的版本就不需要了
<!-- Activiti modeler 可視化依賴 --> <dependency><groupId>org.activiti</groupId><artifactId>activiti-modeler</artifactId><version>5.23.0</version> </dependency><!-- Activity6 集成 Modeler 需要 jar 包 --> <dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-transcoder</artifactId><version>1.8</version> </dependency> <dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-codec</artifactId><version>1.8</version> </dependency> <dependency><groupId>org.activiti</groupId><artifactId>activiti-json-converter</artifactId><version>6.0.0</version> </dependency>代碼集成
前端代碼集成
在項目中的 resource 文件夾下新建一個 static 文件夾
SpringBoot 能自動讀取 static 目錄下的靜態文件,因此文件夾名稱不可隨意更改
找到 activiti-webapp-explorer2 包
將 webapp 下的 diagram-viewer 文件夾,editor-app 文件夾,modeler.html 文件復制到 static 下
- diagram-viewer:流程圖跟蹤組件
- editor-app:目錄中包含設計器里面所有的資源:angular.js、oryx.js以及配套的插件及css
- modeler.html:流程設計器的主頁面,用來引入各種web資源
路徑:Activiti-5.23.0\modules\activiti-webapp-explorer2\src\main\webapp
后端代碼集成
將 Activit 5.23.0 項目中 resource 文件夾下的 stencilset.json 放到自己項目的 resource 目錄下
這個文件的作用就是國際化翻譯,但是只有英文版的
路徑:Activiti-5.23.0\modules\activiti-webapp-explorer2\src\main\resources
找到 activiti-modeler 包
將里面的 StencilsetRestResource.java,ModelEditorJsonRestResource.java,ModelSaveRestResource.java 文件放到自己的項目里
- StencilsetRestResource.java:用于讀取 stencilset.json 文件
- ModelEditorJsonRestResource.java:用戶獲取流程數據
- ModelSaveRestResource.java:用于保存流程數據
路徑:Activiti-5.23.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor
結構
代碼修改
RepositoryService 注入問題
ModelEditorJsonRestResource.java 與 ModelSaveRestResource.java 都是通過 @Autowired 注解注入的 RepositoryService 服務
配置文件中直接對7大服務進行 @Bean 注冊,一個是為了節省后續開發
同時也是為了解決 @Autowired 無法注入,找不到 bean 問題
啟動器注解修改
@SpringBootApplication 注解修改,不加載身份認證配置
// 去除身份驗證 @SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) public class ActivitiApplication {public static void main(String[] args) {SpringApplication.run(ActivitiApplication.class, args);} }請求路徑調整
看 editor-app/app-cfg.js 文件
這個是接口的請求前綴配置
因此在 StencilsetRestResource.java,ModelEditorJsonRestResource.java,ModelSaveRestResource.java 文件上加上 @RequestMapping(value = "/activiti-explorer/service")
以 ModelSaveRestResource.java 為例:
修改 ModelSaveRestResource.java
只需將 saveModel 接口入參做下修改,然后對下方對應的參數做下修改即可
/*** @author Tijs Rademakers*/ @RestController @RequestMapping(value = "/activiti-explorer/service") public class ModelSaveRestResource implements ModelDataJsonConstants {protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ObjectMapper objectMapper;@RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)@ResponseStatus(value = HttpStatus.OK)public void saveModel(@PathVariable String modelId, String name, String description, String json_xml, String svg_xml) {try {Model model = repositoryService.getModel(modelId);ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());modelJson.put(MODEL_NAME, name);modelJson.put(MODEL_DESCRIPTION, description);model.setMetaInfo(modelJson.toString());model.setName(name);repositoryService.saveModel(model);repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));TranscoderInput input = new TranscoderInput(svgStream);PNGTranscoder transcoder = new PNGTranscoder();// Setup outputByteArrayOutputStream outStream = new ByteArrayOutputStream();TranscoderOutput output = new TranscoderOutput(outStream);// Do the transformationtranscoder.transcode(input, output);final byte[] result = outStream.toByteArray();repositoryService.addModelEditorSourceExtra(model.getId(), result);outStream.close();} catch (Exception e) {LOGGER.error("Error saving model", e);throw new ActivitiException("Error saving model", e);}} }新增 createModel 接口
可以理解為流程頁面訪問的入口,對流程進行初始化
/*** 工作流代碼示例* @author: linjinp* @create: 2019-10-22 10:13**/ @RestController @RequestMapping("/activiti-explorer/model") public class ActivitiController {/*** 創建基本模型* @param request* @param response*/@RequestMapping("/create")public void createModel(HttpServletRequest request, HttpServletResponse response){try{String modelName = "modelName";String modelKey = "modelKey";String description = "description";ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();RepositoryService repositoryService = processEngine.getRepositoryService();ObjectMapper objectMapper = new ObjectMapper();ObjectNode editorNode = objectMapper.createObjectNode();editorNode.put("id", "canvas");editorNode.put("resourceId", "canvas");ObjectNode stencilSetNode = objectMapper.createObjectNode();stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");editorNode.put("stencilset", stencilSetNode);// 定義新模型Model modelData = repositoryService.newModel();ObjectNode modelObjectNode = objectMapper.createObjectNode();modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, modelName);modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);modelData.setMetaInfo(modelObjectNode.toString());modelData.setName(modelName);modelData.setKey(modelKey);//保存模型repositoryService.saveModel(modelData);repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());}catch (Exception e){}} }訪問頁面
https://localhost:8086/activiti-explorer/model/create
之后頁面就會被自動重定向到 modeler.html
看數據庫中 ACT_RE_MODEL 表
create 后初始化的數據
簡單配個流程
保存,save
看數據表 ACT_RE_MODEL,數據已更新
漢化
下載我上傳的 stencilset.json 文件替換掉即可
建議原文件先備份
參考
Activiti-Dmo 模塊
文章轉載于 https://blog.csdn.net/qq_37143673/article/details/102667824
總結
以上是生活随笔為你收集整理的Activiti 流程配置可视化:SpringBoot 集成 Activiti6 + Activiti Modeler的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1024看到这篇文章的程序员,能不能把你
- 下一篇: ajax上传文件判断大小,JavaScr