camunda_06_quickstart_springboot
目標
- 在SpringBoot項目中集成Camunda流程引擎, 并啟動啟動一個流程實例
- 了解集成Camunda需要調整哪些配置
pom.xml
最簡單的方式是使用 camunda 的Spring Boot 向導生成項目文件.
需要注意與Spring Boot版本的兼容性, 詳見官網兼容性說明
訪問 官網starter頁面 生成 SpringBoot starter 項目.

一個完整的pom.xml
Camunda 為Spring Boot 提供了多個 starter jar, 可以非常輕松地在我們的項目中嵌入Camunda engine 和 Camunda webapps.
- camunda-bpm-spring-boot-starter: 提供流程引擎, 這個是基礎.
- camunda-bpm-spring-boot-starter-rest: 對外提供API 接口.
- camunda-bpm-spring-boot-starter-webapp: 提供camunda 管理功能.
- camunda-bpm-spring-boot-starter-external-task-client: 提供 external task client 對接spring boot的能力, 對接remote camunda 服務器使用.
- camunda-platform-7-rest-client-spring-boot-starter : 封裝remote camunda REST的客戶端jar包
- camunda-external-task-client: 這個是 external task client的普通 jar 包, 非 starter 包.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.camunda.bpm.getstarted</groupId>
<artifactId>loan-approval-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<camunda.spring-boot.version>7.17.0</camunda.spring-boot.version>
<spring-boot.version>2.6.6</spring-boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
主要的 camunda 依賴包
也可以直接將下面的幾個依賴加到已有的項目中.
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
<version>7.17.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>7.17.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-engine-plugin-spin</artifactId>
</dependency>
<dependency>
<groupId>org.camunda.spin</groupId>
<artifactId>camunda-spin-dataformat-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
application.yaml 簡單配置
先簡單新增一個application.yaml 文件, 內容:
camunda.bpm:
#配置賬戶密碼來訪問Camunda自帶的管理界面
admin-user:
id: demo
password: demo
first-name: demo
filter:
create: All tasks
application.properties 配置文件
# 啟用 h2 console, 這樣可以通過瀏覽器 http://localhost:8080/h2-console/ 查看該數據庫
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
# 配置 h2 數據庫為非memory模式, 這樣可以使用 dbeaver 等工具通過 H2 embeded 方式打開該數據庫文件.
#spring.datasource.url=jdbc:h2:mem:camunda-h2-database;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url=jdbc:h2:file:./h2/camunda-h2-database;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
增加 resources/META-INF/processes.xml 流程部署描述文件
可以在process.xml為一個或多個Process engine設置參數. process.xml 文件內容可以為空, 空的process.xml文件等同于下面內容.
<process-application
xmlns="http://www.camunda.org/schema/1.0/ProcessApplication"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- process-archive即deployment, 這里定義了了唯一的一個 deployment -->
<process-archive>
<properties>
<!-- isDeleteUponUndeploy含義: 當應用程序停止后, 是否要要將 deployment 從數據庫中刪除 -->
<property name="isDeleteUponUndeploy">false</property>
<!-- isScanForProcessDefinitions為true, 自動掃描 classpath 路徑下的文件, 擴展名為.bpmn20.xml, .bpmn, .cmmn11.xml, .cmmn, .dmn11.xml, .dmn -->
<property name="isScanForProcessDefinitions">true</property>
</properties>
</process-archive>
</process-application>
制作并上傳流程模型文件
Modeler 制作一個簡單的流程, 文件名為 loanApproval.bpmn, 復制到 main/resources 目錄中

BPMN 源碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="_DdZocL47EeOQo_IRkjDF6w" targetNamespace="http://camunda.org/schema/1.0/bpmn" exporter="Camunda Modeler" exporterVersion="3.5.0" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
<bpmn2:process id="loanApproval" name="Loan Approval" isExecutable="true">
<bpmn2:startEvent id="StartEvent_1" name="Loan Request
Received">
<bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:sequenceFlow id="SequenceFlow_1" name="" sourceRef="StartEvent_1" targetRef="Task_0dfv74n" />
<bpmn2:endEvent id="EndEvent_1" name="Loan Request Processed">
<bpmn2:incoming>SequenceFlow_0oy9c54</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:userTask id="Task_0dfv74n" name="Check the request" camunda:assignee="demo">
<bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
<bpmn2:outgoing>SequenceFlow_0oy9c54</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_0oy9c54" sourceRef="Task_0dfv74n" targetRef="EndEvent_1" />
</bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="loanApproval">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_3" bpmnElement="StartEvent_1">
<dc:Bounds x="170" y="104" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="154" y="140" width="70" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_1" bpmnElement="SequenceFlow_1" sourceElement="_BPMNShape_StartEvent_3" targetElement="UserTask_0k9otqc_di">
<di:waypoint x="206" y="122" />
<di:waypoint x="264" y="122" />
<bpmndi:BPMNLabel>
<dc:Bounds x="240" y="157" width="90" height="20" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_EndEvent_3" bpmnElement="EndEvent_1">
<dc:Bounds x="419" y="104" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="403" y="140" width="70" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="UserTask_0k9otqc_di" bpmnElement="Task_0dfv74n">
<dc:Bounds x="264" y="82" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0oy9c54_di" bpmnElement="SequenceFlow_0oy9c54">
<di:waypoint x="364" y="122" />
<di:waypoint x="419" y="122" />
<bpmndi:BPMNLabel>
<dc:Bounds x="441.5" y="161" width="0" height="12" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>
SpringBoot main class
在引入 camunda-bpm-spring-boot-starter-webapp 后, 甚至不用專門寫一行 camunda 的代碼, 就可以自動將camunda webapps 啟動.
Main class增加 @EnableProcessApplication后, camunda starter會自動掃描 resources/META-INF/processes.xml 文件, 并按照processes.xml描述文件進行流程引擎配置.
package org.camunda.bpm.getstarted.loanapproval;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.spring.boot.starter.annotation.EnableProcessApplication;
import org.camunda.bpm.spring.boot.starter.event.PostDeployEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.event.EventListener;
@SpringBootApplication
@EnableProcessApplication
public class WebappExampleProcessApplication {
@Autowired
private RuntimeService runtimeService;
public static void main(String... args) {
SpringApplication.run(WebappExampleProcessApplication.class, args);
}
@EventListener
private void processPostDeploy(PostDeployEvent event) {
runtimeService.startProcessInstanceByKey("loanApproval");
}
}
啟動 SpringBoot 應用
瀏覽器輸入 http://localhost:8080 , 直接跳轉到 camunda admin 應用中.


常用的地址是:
- Camunda web :
http://localhost:8080/camunda/ - REST API:
http://localhost:8080/engine-rest/ - H2 console:
http://localhost:8080/h2-console/
H2 console 進入camunda 后臺的H2數據庫:


application.yaml 更多定制
缺省的配置適合demo, 正式項目肯定需要換DB, 增加一個profile文件就能實現定制化配置, 新增文件 src/main/resources/application.yaml
下面是一個簡單的配置樣板, camunda 提供很多可配置項, 可參考官網, 配置屬性
真實項目可參考 camunda platform下載包中的 production.yaml 文件.
camunda.bpm:
#配置賬戶密碼來訪問Camunda自帶的管理界面
admin-user:
id: demo
password: demo
first-name: demo
filter:
create: All tasks
#指定數據庫類型
database:
type: mysql
schema-update: true
#自動部署resources下面的bpmn文件
auto-deployment-enabled: true
#禁止index跳轉到Camunda自帶的管理界面,默認true
webapp:
index-redirect-enabled: false
常用的jdbc配置
H2: jdbc:h2:tcp://localhost/camunda
MySQL: jdbc:mysql://localhost:3306/camunda?autoReconnect=true&sendFractionalSeconds=false
Oracle: jdbc:oracle:thin:@localhost:1521:xe
PostgreSQL: jdbc:postgresql://localhost:5432/camunda
DB2: jdbc:db2://localhost:50000/camunda
MSSQL: jdbc:sqlserver://localhost:1433/camunda
MariaDB: jdbc:mariadb://localhost:3306/camunda

浙公網安備 33010602011771號