最简单的方式是使用 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
    &#10;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&#10;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
  • github camunda-get-started-spring-boot
  • 博客园-Camunda 创建springboot项目 (一)
  •