Spring boot 整合flowable工作流引擎並提供rest-api

前言

工作流引擎,現在Java世界使用的主流是Activiti 和 Flowable 這兩款。Flowable 是Activiti 的fork 分支,所以用法其實差不多。
最理想的狀態,引擎能夠提供一個rest

依賴

flowable 所有的依賴都在maven center裏,所以上maven center 裏查一下 flowable 已經上傳了依賴。

查詢地址 https://search.maven.org/search?q=flowable-spring-boot-starter

現有的依賴列表

Group ID Artifact ID 說明
org.flowable flowable-spring-boot-starter spring boot flowable 依賴
org.flowable flowable-spring-boot-starter-rest-api spring boot flowable 全部五個模塊的 rest api 的依賴,已廢棄
org.flowable flowable-spring-boot-starter-process-rest spring boot flowable 流程引擎 ProcessEngine rest api 的依賴
org.flowable flowable-spring-boot-starter-rest-api spring boot flowable 決策引擎 DmnEngine rest api 的依賴
org.flowable flowable-spring-boot-starter-rest-api spring boot flowable CMMN 決策引擎 CMMNEngine api 的依賴

其他的各位看官自行查看,此處僅列出我們需要用的。

與Spring boot 整合

pom.xml

<?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.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.unreal</groupId>
    <artifactId>flowable-rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>parent</name>
    <url>http://maven.apache.org</url>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-process-rest</artifactId>
            <version>${flowable.version}</version>
        </dependency>

    </dependencies>

</project>

增加RestResponseFactory 和 ContentTypeResolver 提供rest服務

    @Bean
    public RestResponseFactory restResponseFactory() {
        return new RestResponseFactory(new ObjectMapper());
    }

    @Bean
    public ContentTypeResolver contentTypeResolver() {
        return new DefaultContentTypeResolver();
    }

解決中文亂碼

    @Bean
    SpringProcessEngineConfiguration processEngineConfiguration(SimpleDriverDataSource dataSource, DataSourceTransactionManager transactionManager, FormEngineConfiguration formEngineConfiguration, IdmEngineConfiguration idmEngineConfiguration) throws IOException {
        SpringProcessEngineConfiguration cfg = new SpringProcessEngineConfiguration();
        cfg.setDataSource(dataSource).setDatabaseSchemaUpdate("true").setAsyncExecutorActivate(true);
        Resource[] resources = this.applicationContext.getResources("classpath:bpmn/process/*.bpmn20.xml");
        String classPath = ResourceUtils.getURL("classpath:").getPath();
        this.showResourceLog(resources);
        cfg.setDeploymentResources(resources);
        cfg.setTransactionManager(transactionManager);
        FormEngineConfigurator formEngineConfigurator = new FormEngineConfigurator();
        formEngineConfigurator.setFormEngineConfiguration(formEngineConfiguration);
        cfg.addConfigurator(formEngineConfigurator);
        IdmEngineConfigurator idmEngineConfigurator = new IdmEngineConfigurator();
        idmEngineConfigurator.setIdmEngineConfiguration(idmEngineConfiguration);
        cfg.addConfigurator(idmEngineConfigurator);
        cfg.setActivityFontName("宋體");//windows 下使用 中文字體 ,linux 需要換成支持中文的字體
        cfg.setLabelFontName("宋體");
        cfg.setAnnotationFontName("宋體");
        cfg.buildProcessEngine();
        return cfg;
    }

使用

官方 tomcat 部署方式,是需要使用idm登錄或者在rest-api 上使用Basic auth 來使用rest服務。
但是博文裏這種方式,直接可以訪問,不需要驗證。
在不改變server.port 的參數時。默認端口是8080.
請求地址如下:

  • http://127.0.0.1:8080/process-api/

process-api 是 依賴了flowable-spring-boot-starter-process-rest ,
dmn-api 需要依賴flowable-spring-boot-starter-dmn-rest ,
cmmn-api 需要依賴flowable-spring-boot-starter-cmmn-rest

Rest API doc

官方網站上有 rest -api 的說明,請自行查看

https://www.flowable.org/docs/userguide/index.html#restApiChapter

測試

Postman 導入下面的測試集合自行玩一下吧

鏈接: https://pan.baidu.com/s/1gRPw9O-LO-NCMVL3tl0Faw
提取碼: 3k9k

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章