wordpress自動廣告位二級域名和一級域名優(yōu)化難度
springboot集成camunda學(xué)習(xí)與使用.md
- 0、前言
- 一、Spring Boot 集成camunda流程引擎
- 1.新建全新的springboot工程
- 2.添加pom.xml依賴
- 3.啟動Spring Boot工程
- 4.切換成mysql數(shù)據(jù)庫
- 5.設(shè)計并部署一個BPMN流程
- 6.camunda流程引擎測試
- 6.1 通過camunda web控制臺測試
- 6.2 通過camunda rest接口測試
- 6.3 通過Java API接口測試
- 二、自建BPMN Web Modeler
0、前言
Camunda是一款開源的業(yè)務(wù)流程管理(BPM)平臺,旨在幫助企業(yè)自動化和優(yōu)化他們的業(yè)務(wù)流程。Camunda的核心功能包括流程設(shè)計、執(zhí)行和監(jiān)控,支持BPMN(業(yè)務(wù)流程模型與標注)、CMMN(案例管理模型與標注)和DMN(決策模型與標注)等標準。
一、Spring Boot 集成camunda流程引擎
本文內(nèi)容參考此文總結(jié)得出:https://blog.csdn.net/wxz258/article/details/136279524
1.新建全新的springboot工程
新建一個springboot工程,springboot版本為2.7.18,jdk版本1.8,過程略。
2.添加pom.xml依賴
為新項目設(shè)置 Maven 依賴項。需要將 Maven 依賴添加到項目,添加后支持camunda流程引擎、web界面、Rest服務(wù)接口,導(dǎo)入camunda-bpm-spring-boot-starter-rest、camunda-bpm-spring-boot-starter-webapp依賴包。導(dǎo)入后自動將camunda 引擎、rest服務(wù)接口和 Web應(yīng)用程序包含在springboot工程。
使用camunda7.19.0版本,該版本支持jdk8和springboot2。camunda和springboot版本的依賴對應(yīng)關(guān)系,查看官方文檔說明:Spring Boot Version Compatibility | docs.camunda.org
- pom.xml增加依賴
<!-- camunda start --><dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId><version>${camunda.spring-boot.version}</version></dependency><dependency><groupId>org.camunda.bpm.springboot</groupId><artifactId>camunda-bpm-spring-boot-starter-rest</artifactId><version>${camunda.spring-boot.version}</version></dependency><!-- camunda end --><!-- spring jdbc start --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- spring jdbc end --><!-- h2database start --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId></dependency><!-- h2database end -->
注:需要添加spring-boot-starter-jdbc依賴,否則會報Field transactionManager in org.camunda.bpm.spring.boot.starter.configuration.impl.DefaultDatasourceConfiguration required a bean of type ‘org.springframework.transaction.PlatformTransactionManager’ that could not be found.
- application.yml中增加配置
spring:datasource:url: jdbc:h2:mem:camunda;DB_CLOSE_DELAY=1000;LOCK_TIMEOUT=10000username: sapassword:driver-class-name: org.h2.Driver
camunda:bpm:database:type: h2 #可改成 mysqlschema-update: trueauto-deployment-enabled: false # 自動部署 resources 下的 bpmn文件admin-user:id: demopassword: demo
3.啟動Spring Boot工程
以上增加依賴后,就可以基于內(nèi)存數(shù)據(jù)庫(h2)在本地啟動好camunda流程引擎了,啟動后,在瀏覽器中打開 http://localhost:8080/ 時,您可以使用我們之前配置的登錄名和密碼"demo/demo"來訪問 Camunda Web應(yīng)用程序,能做到下面效果說明成功。
4.切換成mysql數(shù)據(jù)庫
如果camunda要在生產(chǎn)環(huán)境中使用,不太可能是直接使用h2數(shù)據(jù)庫的,camunda本身對mysql的支持性也相當(dāng)好,因此需要改造一下切換成使用mysql.
其他數(shù)據(jù)庫的支持情況見:https://docs.camunda.org/manual/7.19/introduction/supported-environments/#databases
- pom.xml增加mysql依賴
<!-- mysql driver start --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><!-- mysql driver end -->
- application.yml中修改
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/camundatest?characterEncoding=UTF-8&useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver
camunda:bpm:database:type: mysqlschema-update: false # 是否自動建表,但我測試為true時,創(chuàng)建表會出現(xiàn),因此還是改成false由手工建表。auto-deployment-enabled: false # 自動部署 resources 下的 bpmn文件admin-user:id: demopassword: demo
- mysql中創(chuàng)建數(shù)據(jù)庫camundatest
create database camundatest;
- 生成數(shù)據(jù)庫表
使用schma-update:true后,啟動工程也不會完全生成完整的mysql表,原因不明,因此還是采用手工執(zhí)行數(shù)據(jù)庫腳本的方式來建表。
Sql建表語句位置:找到maven依賴,org\camunda\bpm\camunda-engine\7.19.0\camunda-engine-7.19.0.jar,org.camunda.bpm.engine.db.create包下面。
/org/camunda/bpm/engine/db/create/activiti.mysql.create.case.engine.sql
/org/camunda/bpm/engine/db/create/activiti.mysql.create.case.history.sql
/org/camunda/bpm/engine/db/create/activiti.mysql.create.decision.engine.sql
/org/camunda/bpm/engine/db/create/activiti.mysql.create.decision.history.sql
/org/camunda/bpm/engine/db/create/activiti.mysql.create.engine.sql
/org/camunda/bpm/engine/db/create/activiti.mysql.create.history.sql
/org/camunda/bpm/engine/db/create/activiti.mysql.create.identity.sql
使用navicat或其他工具依次執(zhí)行完。注:我測試的時候其中activiti.mysql.create.case.engine.sql有部分sql執(zhí)行報錯,提示重復(fù)創(chuàng)建之類的,那就把報錯的sql一一刪除再執(zhí)行完。
最后得到49張表.
最后再執(zhí)行一下springboot工程重啟成功。再一次進行http://localhost:8080測試,輸入demo/demo能登錄成功。
5.設(shè)計并部署一個BPMN流程
這里先使用windows下載安裝的流程設(shè)計器進行一個流程設(shè)計與測試,后面章面再說明如果通過web構(gòu)建一個瀏覽器形式的在線流程設(shè)計器。
- 下載安裝流程設(shè)計器
windows版本下載地址:https://downloads.camunda.cloud/release/camunda-modeler/5.19.0/camunda-modeler-5.19.0-win-x64.zip
下載后得到一個zip,解壓后即可運行Camunda Modeler.exe使用。
- 設(shè)計BPMN流程
點擊后會提示保存xml,然后出現(xiàn)以下彈窗,配置好REST endpoint,然后點Deploy即可發(fā)布成功。
發(fā)布成功后會在act_ge_bytearray,act_re_deployment表中生成對應(yīng)的數(shù)據(jù)
6.camunda流程引擎測試
6.1 通過camunda web控制臺測試
現(xiàn)在,當(dāng)您在瀏覽器中打開 http://localhost:8080/camunda/app/tasklist/ 時,您可以使用我們之前配置的登錄名和密碼“demo/demo”來訪問 Camunda Web 應(yīng)用程序。
可以點擊Start process進行流程的啟動
Start process后:act_hi_procinst表中會有新的一條流程實例數(shù)據(jù),對應(yīng)的act_ru_task和act_hi_taskinst等表均有相應(yīng)的記錄
6.2 通過camunda rest接口測試
以上我們通過camunda的web界面進行了發(fā)起流程測試驗證,下面我們通過Camunda REST API的方式進行測試驗證。
Camunda Platform REST API官方說明文檔:https://docs.camunda.org/rest/camunda-bpm-platform/7.19/
- 查詢流程定義
GET http://{host}:{port}/{contextPath}/process-definition
# 示例
curl http://localhost:8080/engine-rest/process-definition# 輸出
[{"id":"Process_0woc0aw:1:711f5f2d-5c38-11ef-9f87-e46017b79329","key":"Process_0woc0aw","category":"http://bpmn.io/schema/bpmn","description":null,"name":null,"version":1,"resource":"diagram_1.bpmn","deploymentId":"7104f95b-5c38-11ef-9f87-e46017b79329","diagram":null,"suspended":false,"tenantId":null,"versionTag":null,"historyTimeToLive":180,"startableInTasklist":true}]
- 查詢流程定義數(shù)量
GET http://{host}:{port}/{contextPath}/process-definition/count
curl http://localhost:8080/engine-rest/process-definition/count
- 發(fā)起流程實例(流程啟動)
POST http://{host}:{port}/{contextPath}/process-definition/key/{key}/start
# 示例
curl -X POST -H 'Content-Type: application/json' \-d '{"variables": {"variable1": {"value": "hello","type": "String"},"variable2": {"value": true,"type": "Boolean"}},"businessKey": "myBusinessKey-test1"}' \"http://localhost:8080/engine-rest/process-definition/key/Process_0woc0aw/start"# 輸出
{"links":[{"method":"GET","href":"http://localhost:8080/engine-rest/process-instance/7aac2fb7-5c48-11ef-9f87-e46017b79329","rel":"self"}],"id":"7aac2fb7-5c48-11ef-9f87-e46017b79329","definitionId":"Process_0woc0aw:1:711f5f2d-5c38-11ef-9f87-e46017b79329","businessKey":"myBusinessKey-test1","caseInstanceId":null,"ended":false,"suspended":false,"tenantId":null}
- 查詢待辦任務(wù)
GET http://{host}:{port}/{contextPath}/task
# 示例
# 查詢所有待辦任務(wù)
curl http://localhost:8080/engine-rest/task# 查詢分配給liujh的待辦任務(wù)
curl http://localhost:8080/engine-rest/task?assignee=liujh[{"id":"7aaec7ce-5c48-11ef-9f87-e46017b79329","name":"審批","assignee":"liujh","created":"2024-08-17T11:26:24.000+0800","due":null,"followUp":null,"lastUpdated":null,"delegationState":null,"description":null,"executionId":"7aac2fb7-5c48-11ef-9f87-e46017b79329","owner":null,"parentTaskId":null,"priority":50,"processDefinitionId":"Process_0woc0aw:1:711f5f2d-5c38-11ef-9f87-e46017b79329","processInstanceId":"7aac2fb7-5c48-11ef-9f87-e46017b79329","taskDefinitionKey":"Activity_0p57gxb","caseExecutionId":null,"caseInstanceId":null,"caseDefinitionId":null,"suspended":false,"formKey":null,"camundaFormRef":null,"tenantId":null},{"id":"b11eed80-5c39-11ef-9f87-e46017b79329","name":"審批","assignee":"liujh","created":"2024-08-17T09:40:33.000+0800","due":null,"followUp":null,"lastUpdated":null,"delegationState":null,"description":null,"executionId":"b1148d3d-5c39-11ef-9f87-e46017b79329","owner":null,"parentTaskId":null,"priority":50,"processDefinitionId":"Process_0woc0aw:1:711f5f2d-5c38-11ef-9f87-e46017b79329","processInstanceId":"b1148d3d-5c39-11ef-9f87-e46017b79329","taskDefinitionKey":"Activity_0p57gxb","caseExecutionId":null,"caseInstanceId":null,"caseDefinitionId":null,"suspended":false,"formKey":null,"camundaFormRef":null,"tenantId":null}]
- 完成待辦任務(wù)
POST http://{host}:{port}/{contextPath}/task/{id}/complete
curl -X POST -H 'Content-Type: application/json' \-d '{"variables": {"variable1": {"value": "agree","type": "String"}}}' \"http://localhost:8080/engine-rest/task/d2925d3f-5c49-11ef-9f87-e46017b79329/complete"
6.3 通過Java API接口測試
- 流程引擎接口實現(xiàn)相關(guān)引入
/*** 流程定義相關(guān)接口實現(xiàn)類*/@Resourceprivate RepositoryService repositoryService;/*** 流程實例相關(guān)接口實現(xiàn)類*/@Resourceprivate RuntimeService runtimeService;/*** 任務(wù)相關(guān)接口實現(xiàn)類*/@Resourceprivate TaskService taskService;/*** 身份相關(guān)服務(wù)*/@Resourceprivate IdentityService identityService;
- 查詢所有的流程定義
/*** 獲取所有的流程定義* @return*/@GetMapping("/getProcessDefinitions")public List<String> getProcessDefinitions() {// 創(chuàng)建流程定義查詢并列出所有的流程定義List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().list();// 定義輸出變量List<String> definitionKeys = new ArrayList<String>();// 輸出流程定義信息for (ProcessDefinition processDefinition : processDefinitions) {System.out.println("Process Definition ID: " + processDefinition.getId());System.out.println("Process Definition Key: " + processDefinition.getKey());System.out.println("Process Definition Name: " + processDefinition.getName());System.out.println("Version: " + processDefinition.getVersion());System.out.println("-------------------------------------------------");// 加入definitionKeysdefinitionKeys.add(processDefinition.getKey());}return definitionKeys;}
- 查詢流程定義數(shù)量
/*** 獲取所有的流程定義的數(shù)量* * @return 流程定義的數(shù)量*/@GetMapping("/getProcessDefinitionCount")public Long getProcessDefinitionCount() {// 創(chuàng)建流程定義查詢并列出所有的流程定義個數(shù)Long processDefinitionCount = repositoryService.createProcessDefinitionQuery().count();// 輸出結(jié)果System.out.println("Process Definition size: " + processDefinitionCount);return processDefinitionCount;}
- 發(fā)起流程實例(流程啟動)
/*** 發(fā)起流程實例(流程啟動)* * @param procDefKey 業(yè)務(wù)Key* @return*/@GetMapping("/processInstanceStart")public String processInstanceStart(@NotBlank(message = "業(yè)務(wù)Key不能為空") String businessKey) {// 流程定義KeyString procDefKey = "Process_0woc0aw";ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(procDefKey, businessKey);// 輸出結(jié)果System.out.println(processInstance.getProcessInstanceId());return processInstance.getProcessInstanceId();}
- 查詢待辦任務(wù)
使用TaskService進行待辦任務(wù)查詢
/*** 查詢待辦任務(wù)(分配置assigee的所有任務(wù))* 測試:curl http://localhost:8080/camundaTest/listTasks?assignee=liujh*/@SuppressWarnings({ "unchecked", "rawtypes" })@GetMapping("/listTasks")public List<String> listTasks(@NotBlank(message = "assignee不能為空") String assignee) {// 定義返回結(jié)果變量listList<String> taskIds = new ArrayList<>();// 列表用戶assignee下的當(dāng)前所有的待辦任務(wù)列表List<TaskEntity> taskList = (List) taskService.createTaskQuery().taskAssignee(assignee).list();for (Task task : taskList) {String taskTitle = "待辦任務(wù)ID=" + task.getId() + ",流程實例ID=" + task.getProcessInstanceId() + "\n";System.out.println(taskTitle);taskIds.add(task.getId());}return taskIds;}
- 完成待辦任務(wù)
/*** 完成待辦任務(wù),這里僅測試,正式環(huán)境需要寫到Service層并且寫在同一事務(wù)*/@GetMapping("/completeTask")public void completeTask(@NotBlank(message = "任務(wù)ID不能為空") String taskId,@NotBlank(message = "任務(wù)處理人不能為空") String assignee) {// 工作流程傳遞變量定義Map<String, Object> variables = new HashMap<>();variables.put("approved", true);// 根據(jù)任務(wù)Id查詢出相關(guān)信息Task task = taskService.createTaskQuery().taskId(taskId).singleResult();if(task == null) {throw new RuntimeException("未找到任務(wù)Id為 " + taskId + " 的任務(wù)");}// 設(shè)置當(dāng)前線程的用戶身份,用于增加批注信息時設(shè)置用戶IDidentityService.setAuthenticatedUserId(assignee);// 完成任務(wù)taskService.complete(taskId, variables);// 增加批注信息taskService.createComment(taskId, task.getProcessInstanceId(), "同意~");}
二、自建BPMN Web Modeler
Camunda Modeler如何在web瀏覽器上設(shè)計?
第一章節(jié)中的流程設(shè)計器是直接采用在windows下下載流程設(shè)計器安裝程序在本機上使用的,但往往希望在瀏覽器上跟隨應(yīng)用系統(tǒng)部署在一起,形成一個web版的在線設(shè)計器。
我公司的大部分項目是使用react的,因此這里就使用開源的BPMN建模庫,如:Bpmn.js + react,結(jié)合后臺java接口來完成。
思路:
- 1.前端開發(fā)
使用bpmn-js構(gòu)建一個BPMN流程設(shè)計器。
提供流程元素的拖拽、連線、屬性編輯等功能。
支持流程的保存、加載和導(dǎo)出為BPMN XML格式。
- 2.后端開發(fā)
使用Spring Boot等技術(shù)搭建后端服務(wù)。
提供API接口來保存用戶設(shè)計的BPMN XML文件。
提供接口將BPMN文件部署到Camunda流程引擎。
支持版本管理和用戶權(quán)限管理。
自建BPMN Web Modeler未完成待補充