Flowable與springBoot專案整合及出現的問題
單純地將Flowable和springBoot整合,使用mysql作為資料庫,整合中踩了兩個坑,見文末。
在pom中新增依賴
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.happen</groupId>
<artifactId>flowable1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>flowable1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.flowable/flowable-spring-boot-starter -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml檔案
database-schema-update: true的作用是自動更新資料庫中需要的表,在第一次執行時必要
server:
port: 8000
web:
domain: http://localhost
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: WHP199617whp
url: jdbc:mysql://localhost:3306/flowable1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
flowable:
async-executor-activate: false
database-schema-update: true
Flowable1Application
對資料庫中的流程數量進行查詢
package com.happen.flowable1;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(proxyBeanMethods = false)
public class Flowable1Application {
public static void main(String[] args) {
SpringApplication.run(Flowable1Application.class, args);
}
@Bean
public CommandLineRunner init(final RepositoryService repositoryService,
final RuntimeService runtimeService,
final TaskService taskService) {
return new CommandLineRunner() {
@Override
public void run(String... strings) throws Exception {
System.out.println("Number of process definitions : "
+ repositoryService.createProcessDefinitionQuery().count());
System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
runtimeService.startProcessInstanceByKey("oneTaskProcess");
System.out.println("Number of tasks after process start: "
+ taskService.createTaskQuery().count());
}
};
}
}
xml檔案,這是一個 BPMN 2.0的標準檔案
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:flowable="http://flowable.org/bpmn"
targetNamespace="Examples">
<process id="oneTaskProcess" name="The One Task Process">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
<userTask id="theTask" name="my task" flowable:assignee="kermit" />
<sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
完整的專案目錄如下:
執行結果
會在資料庫中生成對應的表
控制檯中:
遇到的坑
- 建表的時候出現 table 'flowable1.act_id_user' doesn't exist
這是因為之前本機上建過相同的表,現在不許重複建表了?在資料庫配置時後面追加&nullCatalogMeansCurrent=true即可 - nested exception is org.flowable.common.engine.api.FlowableException: Error initialising eventregistry data model
將springboot版本設定為2.2.0.RELEASE即可(我試了2.4.5就是不行,真坑,不行就刪了資料庫中的所有表再試試)。