一、前言及背景

1. Flowable是什么?

Flowable是一个使用Java编写的轻量级业务流程引擎。Flowable流程引擎可用于部署BPMN 2.0流程定义(用于定义流程的行业XML标准), 创建这些流程定义的流程实例,进行查询,访问运行中或历史的流程实例与相关数据,等等。
Flowable内部依赖的是liquibase框架,所以做适配改造相当于适配Flowable+liquibase框架

2. 适配版本

Flowable版本6.4.2
Flowable版本-liquibase版本3.6.3
Flowable版本-liquibase版本4.9.1
达梦jdbc驱动版本8.1.2.192

3. 适配思路总结

  1. 项目改造完后的gitee地址:https://gitee.com/gy297879328/dm_flowable
  2. 该框架适配主要的改造点在liquibase版本3.x与4.x差异上,主要报错是jdbc驱动的getDatabaseMinorVersion函数返回值不同导致适配报错。
  3. 适配改造的思路就是调整getDatabaseMinorVersion的识别即可,4.x版本框架使用的call DBMS_UTILITY.DB_VERSION(?,?),但达梦中没有需进行源代码调整。具体的可看第三部分内容
  4. 项目适配中的达梦驱动建议使用8.1.2.192及以上版本便于适配,低版本需要改的东西会比较多

二、新建flowable项目

image.png

1. 创建工作流xml文件

  1. 在resources/processes目录下,新建ExpenseProcess.bpmn20.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
             xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
             typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
             targetNamespace="http://www.flowable.org/processdef">
    <process id="Expense" name="ExpenseProcess" isExecutable="true">
        <documentation>报销流程</documentation>
        <startEvent id="start" name="开始"></startEvent>
        <userTask id="fillTask" name="出差报销" flowable:assignee="${taskUser}">
            <extensionElements>
                <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler">
                    <![CDATA[false]]></modeler:initiator-can-complete>
            </extensionElements>
        </userTask>
        <exclusiveGateway id="judgeTask"></exclusiveGateway>
        <userTask id="directorTak" name="经理审批">
            <extensionElements>
                <flowable:taskListener event="create"
                                       class="com.dameng.dm_flowable.task.ManagerTaskHandler"></flowable:taskListener>
            </extensionElements>
        </userTask>
        <userTask id="bossTask" name="老板审批">
            <extensionElements>
                <flowable:taskListener event="create"
                                       class="com.dameng.dm_flowable.task.BossTaskHandler"></flowable:taskListener>
            </extensionElements>
        </userTask>
        <endEvent id="end" name="结束"></endEvent>
        <sequenceFlow id="directorNotPassFlow" name="驳回" sourceRef="directorTak" targetRef="fillTask">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='驳回'}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="bossNotPassFlow" name="驳回" sourceRef="bossTask" targetRef="fillTask">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='驳回'}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="flow1" sourceRef="start" targetRef="fillTask"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="fillTask" targetRef="judgeTask"></sequenceFlow>
        <sequenceFlow id="judgeMore" name="大于500元" sourceRef="judgeTask" targetRef="bossTask">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${money > 500}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="bossPassFlow" name="通过" sourceRef="bossTask" targetRef="end">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='通过'}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="directorPassFlow" name="通过" sourceRef="directorTak" targetRef="end">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='通过'}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="judgeLess" name="小于500元" sourceRef="judgeTask" targetRef="directorTak">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${money <= 500}]]></conditionExpression>
        </sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_Expense">
        <bpmndi:BPMNPlane bpmnElement="Expense" id="BPMNPlane_Expense">
            <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
                <omgdc:Bounds height="30.0" width="30.0" x="285.0" y="135.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="fillTask" id="BPMNShape_fillTask">
                <omgdc:Bounds height="80.0" width="100.0" x="405.0" y="110.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="judgeTask" id="BPMNShape_judgeTask">
                <omgdc:Bounds height="40.0" width="40.0" x="585.0" y="130.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="directorTak" id="BPMNShape_directorTak">
                <omgdc:Bounds height="80.0" width="100.0" x="735.0" y="110.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="bossTask" id="BPMNShape_bossTask">
                <omgdc:Bounds height="80.0" width="100.0" x="555.0" y="255.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
                <omgdc:Bounds height="28.0" width="28.0" x="771.0" y="281.0"></omgdc:Bounds>
            </bpmndi:BPMNShape>
            <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
                <omgdi:waypoint x="315.0" y="150.0"></omgdi:waypoint>
                <omgdi:waypoint x="405.0" y="150.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
                <omgdi:waypoint x="505.0" y="150.16611295681062"></omgdi:waypoint>
                <omgdi:waypoint x="585.4333333333333" y="150.43333333333334"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="judgeLess" id="BPMNEdge_judgeLess">
                <omgdi:waypoint x="624.5530726256983" y="150.44692737430168"></omgdi:waypoint>
                <omgdi:waypoint x="735.0" y="150.1392757660167"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="directorNotPassFlow" id="BPMNEdge_directorNotPassFlow">
                <omgdi:waypoint x="785.0" y="110.0"></omgdi:waypoint>
                <omgdi:waypoint x="785.0" y="37.0"></omgdi:waypoint>
                <omgdi:waypoint x="455.0" y="37.0"></omgdi:waypoint>
                <omgdi:waypoint x="455.0" y="110.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="bossPassFlow" id="BPMNEdge_bossPassFlow">
                <omgdi:waypoint x="655.0" y="295.0"></omgdi:waypoint>
                <omgdi:waypoint x="771.0" y="295.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="judgeMore" id="BPMNEdge_judgeMore">
                <omgdi:waypoint x="605.4340277777778" y="169.56597222222223"></omgdi:waypoint>
                <omgdi:waypoint x="605.1384083044983" y="255.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="directorPassFlow" id="BPMNEdge_directorPassFlow">
                <omgdi:waypoint x="785.0" y="190.0"></omgdi:waypoint>
                <omgdi:waypoint x="785.0" y="281.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
            <bpmndi:BPMNEdge bpmnElement="bossNotPassFlow" id="BPMNEdge_bossNotPassFlow">
                <omgdi:waypoint x="555.0" y="295.0"></omgdi:waypoint>
                <omgdi:waypoint x="455.0" y="295.0"></omgdi:waypoint>
                <omgdi:waypoint x="455.0" y="190.0"></omgdi:waypoint>
            </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
</definitions>
  1. 安装IDEA的Flowable BPMN插件后可查看流程图

image.png

2. pom文件导入相关依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--flowable工作流依赖-->
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter</artifactId>
            <version>6.3.0</version>
        </dependency>
        <!--导入dm的驱动版本-->
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
            <version>8.1.2.192</version>
        </dependency>

3. 新建application.yml文件

在resources目录下新建application.yml文件
image.png

spring:
  datasource:
    url: jdbc:dm://127.0.0.1:5236
    username: SYSDBA
    password: SYSDBA
    driver-class-name: dm.jdbc.driver.DmDriver
    type: com.alibaba.druid.pool.DruidDataSource

flowable:
  #关闭定时任务JOB
  async-executor-activate: true
  database-schema-update: true
server:
  port: 8081

4. 新建工作流的节点触发类

经理审批节点的BossTaskHandler类

新建com.dameng.dm_flowable.task目录,并新建ManagerTaskHandler类

package com.dameng.dm_flowable.task;

import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;



public class BossTaskHandler implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        delegateTask.setAssignee("老板");
    }

}

老板审批节点的ManagerTaskHandler类

新建com.dameng.dm_flowable.task目录,并新建ManagerTaskHandler类

package com.dameng.dm_flowable.task;

import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;

public class ManagerTaskHandler implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        delegateTask.setAssignee("经理");
    }

}

修改工作流中xml的节点触发类

image.png

5. 新建controller层ExpenseController

新建com.dameng.dm_flowable.controller目录,并新建ExpenseController类

package com.dameng.dm_flowable.controller;



import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "expense")
public class ExpenseController {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;

    /**
     * 添加报销
     *
     * @param userId    用户Id
     * @param money     报销金额
     * @param descption 描述
     */
    @RequestMapping(value = "add")
    @ResponseBody
    public String addExpense(String userId, Integer money, String descption) {
        //启动流程
        HashMap<String, Object> map = new HashMap<>();
        map.put("taskUser", userId);
        map.put("money", money);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Expense", map);
        return "提交成功.流程Id为:" + processInstance.getId();
    }

    /**
     * 获取审批管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String userId) {
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
        for (Task task : tasks) {
            System.out.println(task.toString());
        }
        return tasks.toArray().toString();
    }

    /**
     * 批准
     *
     * @param taskId 任务ID
     */
    @RequestMapping(value = "apply")
    @ResponseBody
    public String apply(String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            throw new RuntimeException("流程不存在");
        }
        //通过审核
        HashMap<String, Object> map = new HashMap<>();
        map.put("outcome", "通过");
        taskService.complete(taskId, map);
        return "processed ok!";
    }

    /**
     * 拒绝
     */
    @ResponseBody
    @RequestMapping(value = "reject")
    public String reject(String taskId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("outcome", "驳回");
        taskService.complete(taskId, map);
        return "reject";
    }

}

6. 新建FlowableConfig配置类

新建com.dameng.dm_flowable.conf文件目录,并新建FlowableConfig类

package com.dameng.dm_flowable.conf;

import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {


    @Override
    public void configure(SpringProcessEngineConfiguration engineConfiguration) {
        engineConfiguration.setActivityFontName("宋体");
        engineConfiguration.setLabelFontName("宋体");
        engineConfiguration.setAnnotationFontName("宋体");
    }
}

7. 启动项目报错

image.png
image.png

三、达梦适配步骤整理

步骤一:报错处理_couldn’t deduct database type from database product name ‘DM DBMS’

image.png

原因分析

  1. flowable启动后会从已有的databaseTypeMappings集合中看到数据库类型。
  2. databaseTypeMappings集合调用的是getDefaultDatabaseTypeMappings方法
  3. getDefaultDatabaseTypeMappings函数中并没有DM DBMS类型,所以会报错。

image.png
image.png
image.png

解决方案

在jdbc连接串后添加compatibleMode=oracle的兼容参数

注意:不采用网上的解决方案也就是将DM DBMS识别添加到getDefaultDatabaseTypeMappings函数中

spring:
  datasource:
    url: jdbc:dm://127.0.0.1:5236?compatibleMode=oracle
    username: SYSDBA
    password: SYSDBA
    driver-class-name: dm.jdbc.driver.DmDriver
    type: com.alibaba.druid.pool.DruidDataSource

步骤二:确认liquibase的版本

接下来需要查看下flowable的框架中内置的liquibase的版本,经测试发现3.x版本与4.x版本的代码有所差异,适配步骤不同
image.png

liquibase4.x的适配步骤

Cannot read from DBMS_UTILITY.DB_VERSION: -2193 第1 行附近出现错误:无效的方法名[DB_VERSION]

image.png

原因分析
  1. 在liquibase.database.core.OracleDatabase#setConnection中代码会通过调用存储过程{call DBMS_UTILITY.DB_VERSION(?,?)}获取版本号通过解析后封装到databaseMajorVersion与databaseMinorVersion中。
  2. 在达梦中并没有该存储过程,执行后控制台会报改错,因为取不到值

image.png

解决方案

在项目中新建liquibase.database.core目录并重写该部分源码,代码如下
调整的逻辑就是 判断connection类型 如果是DmdbConnection类 就直接把值写死即可。

                    CallableStatement statement = null;
                    try {
                        DatabaseMetaData metaData = sqlConn.getMetaData();
                        Connection connection = metaData.getConnection();
                        if (connection instanceof DmdbConnection) {
                            String compatibleVersion = "11.2.0.4.0";
                            Matcher majorVersionMatcher = Pattern.compile("(\\d+)\\.(\\d+)\\..*").matcher(compatibleVersion);
                            if (majorVersionMatcher.matches()) {
                                this.databaseMajorVersion = Integer.valueOf(majorVersionMatcher.group(1));
                                this.databaseMinorVersion = Integer.valueOf(majorVersionMatcher.group(2));
                            }
                        }else{
                            //noinspection HardCodedStringLiteral
                            statement = sqlConn.prepareCall("{call DBMS_UTILITY.DB_VERSION(?,?)}");
                            statement.registerOutParameter(1, Types.VARCHAR);
                            statement.registerOutParameter(2, Types.VARCHAR);
                            statement.execute();

                            String compatibleVersion = statement.getString(2); //"11.2.0.4.0";

                            if (compatibleVersion != null) {
                                Matcher majorVersionMatcher = Pattern.compile("(\\d+)\\.(\\d+)\\..*").matcher(compatibleVersion);
                                if (majorVersionMatcher.matches()) {
                                    this.databaseMajorVersion = Integer.valueOf(majorVersionMatcher.group(1));
                                    this.databaseMinorVersion = Integer.valueOf(majorVersionMatcher.group(2));
                                }
                            }
                        }




                    } catch (SQLException e) {
                        @SuppressWarnings("HardCodedStringLiteral") String message = "Cannot read from DBMS_UTILITY.DB_VERSION: " + e.getMessage();

                        //noinspection HardCodedStringLiteral
                        Scope.getCurrentScope().getLog(getClass()).info("Could not set check compatibility mode on OracleDatabase, assuming not running in any sort of compatibility mode: " + message);
                    } finally {
                        JdbcUtil.closeStatement(statement);
                    }

image.png

liquibase3.x的适配步骤_现场环境

注:使用2023以后的达梦jdbc驱动版本,理论来说可规避该问题。

Error creating dmn liquibase instance

现场适配的版本是3.6.3版本,报错堆栈如下:
image.png

原因分析

根据堆栈可以看到liquibase.changelog.ChangeLogParameters类初始化取getDatabaseMinorVersion时报错。
修改思路就是,找到源码直接强制修改结果集
image.png

解决方案

在项目中新建liquibase.changelog目录并重写liquibase.changelog.ChangeLogParameters#ChangeLogParameters(liquibase.database.Database) 该部分源码,代码如下
调整的逻辑就是 把getDatabaseMinorVersion函数的返回值写死,根据jdbc的查询oracle的结果,直接写死该值

            try {
              //  this.set("database.databaseMinorVersion", database.getDatabaseMinorVersion());
                this.set("database.databaseMinorVersion", "2");
            } catch (Exception ignore) {
            }

image.png
image.png

四、验证适配结果

1. 访问项目的controller接口

http://localhost:8081/expense/add?userId=123&money=1000
http://localhost:8081/expense/list?userId=123

image.png

2. 查询数据库生成的表

该框架会在数据库中生成一批表结构以及序列,都是ACT开头的表。可以直接在库中查看
image.png

其他

1. 下载插件Flowable BPMN

image.png

2. pom排除指定依赖步骤

    <!--flowable工作流依赖-->
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter</artifactId>
            <version>6.4.2</version>
        </dependency>


        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter</artifactId>
            <version>6.4.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.6.3</version>
        </dependency>
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐