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的能力, 对接单独的camunda 服务器使用.
<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>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>
    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>2.3.5</version>
    </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>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

增加 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 应用中.启动截图网站访问截图

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

参考文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果出现springboot-quickstart-0.0.1-SNAPSHOT.jar中没有主清单属性信息,你可以按照以下步骤进行处理: 1. 首先,检查你的pom文件中是否有正确配置spring-boot-maven-plugin插件。确保以下代码在pom.xml文件中的<build><plugins>部分中: ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> ``` 2. 其次,确认你的jar包中是否包含一个名为MANIFEST.MF的文件。该文件应该位于jar包的META-INF目录下。如果没有这个文件,或者文件中没有正确的清单属性信息,就会导致出现没有主清单属性的错误。 如果你遵循了以上步骤,并且仍然出现没有主清单属性的错误,可以尝试以下解决方法: 1. 在命令提示符中进入jar包所在位置,然后执行以下命令: ``` jar -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar ``` 这个命令会尝试执行jar包,并输出更详细的错误信息。根据错误信息,你可以进一步排查问题。 2. 检查你的Spring Boot引导类(通常是一个带有@SpringBootApplication注解的类)。确保这个类中包含了一个main方法,类似于下面的代码: ```java @SpringBootApplication public class Springboot01QuickstartApplication { public static void main(String[] args) { SpringApplication.run(Springboot01QuickstartApplication.class, args); } } ``` 以上是处理springboot_01_quickstart-0.0.1-SNAPSHOT.jar中没有主清单属性的一些方法和步骤。希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值