Spring Boot 集成 Flowable 并自定义数据源

永久链接:https://blog.kekwy.com/flowable-datasource/

问题描述

在使用 flowable-spring-boot-starter 进行 spring boot 集成 flowable 时,flowable 会使用配置文件中 spring.datasource 前缀下设置的数据源:

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: datasource1
    username: xxxx
    password: xxxx

而项目中的其他数据库工具,如 mybatis 等,同样也会使用此数据源。由于 flowable 相关的表结构过多,该数据源我们只希望保存与项目业务相关的表结构,故尝试使 flowable 使用其他数据源(非默认,自定义)。

Spring Boot 版本:3.0.5

Flowable 版本:7.0.0.M1

相关依赖:

<!-- MySQL -->
<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
  <version>8.0.32</version>
  <scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.flowable/flowable-spring-boot-starter -->
<dependency>
  <groupId>org.flowable</groupId>
  <artifactId>flowable-spring-boot-starter</artifactId>
  <version>7.0.0.M1</version>
</dependency>

解决方案

首先在配置文件中添加我们自定义的数据源,前缀任意,不冲突即可:

flowable:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: datasource2
    username: xxxx
    password: xxxx

定义 org.flowable.common.engine.impl.EngineConfigurator 接口的一个实现类,读取配置文件信息并在 beforeInit 方法中创建数据源。使用 @Component 注解生成 bean:

@Component
public class DatasourceConfigurator implements EngineConfigurator {
    
    @Value("${flowable.datasource.url}")
    private String url;

    @Value("${flowable.datasource.type}")
    private Class<? extends DataSource> type;

    @Value("${flowable.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${flowable.datasource.username}")
    private String username;

    @Value("${flowable.datasource.password}")
    private String password;

    
    @Override
    public void beforeInit(AbstractEngineConfiguration engineConfiguration) {
        DataSource dataSource = DataSourceBuilder.create()
                .type(type)
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password).build();
        engineConfiguration.setDataSource(dataSource);
    }

    @Override
    public void configure(AbstractEngineConfiguration engineConfiguration) {
    }

    @Override
    public int getPriority() {
        return 600000; // 保证该优先级最高
    }

}

定义一个配置类,实现接口 org.flowable.spring.boot.EngineConfigurationConfigurer。将 datasourceConfigurator 注入,在 configure 方法中将其加入 Flowable 引擎的上下文中。

@Configuration
public class ProcessEngineConfig implements EngineConfigurationConfigurer<SpringAppEngineConfiguration> {

    private DatasourceConfigurator datasourceConfigurator;

    @Autowired
    public void setDatasourceConfigurator(DatasourceConfigurator datasourceConfigurator) {
        this.datasourceConfigurator = datasourceConfigurator;
    }

    @Override
    public void configure(SpringAppEngineConfiguration engineConfiguration) {
        engineConfiguration.addConfigurator(datasourceConfigurator);
    }
    
}

观察日志发现我们自定义的配置成功地被 Flowable 引擎调用执行了:

image-20230426115352537

  • 在日志中还可以看到其他五个 Flowable 自动添加的 Configurator 和各自的优先级,优先级高的晚调用,相同配置高优先级会覆盖低优先级。故我们在定义自己的 Configurator 时需要设置一个比上述五个 Configurator 更高的优先级。
  • 实现接口 org.flowable.spring.boot.EngineConfigurationConfigurer 时,注意一定要使用 SpringAppEngineConfiguration 作为泛型参数。

至此,问题成功解决,欢迎大家指正。

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值