Springboot21x配置Activiti7单独数据源问题

40次阅读

共计 6593 个字符,预计需要花费 17 分钟才能阅读完成。

Springboot2.1.x 配置 Activiti7 单独数据源问题

简介

最近基于最新的 Activiti7 配置了 SpringBoot2
简单上手使用了一番。发现市面上解决 Activiti7 的教程很少,采坑也比较多,在 Activiti6 配置数据源和 Activiti7 有所区别,基于 Activiti6Activiti7里是无法正常使用的。接下来让我们看下区别。

问题

Activiti6多数据源配置

6 的配置比较简单点。

  1. 先加入配置:
# activiti 数据源
spring.datasource.activiti.driver=com.mysql.jdbc.Driver
spring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
spring.datasource.activiti.username=root
spring.datasource.activiti.password=Rtqw123OpnmER
spring.datasource.activiti.driverClassName=com.mysql.jdbc.Driver
  1. @ConfigurationProperties 加载以 spring.datasource.activiti 开头的DataSource
  2. 创建 ActivitiConfig 继承 AbstractProcessEngineAutoConfiguration 方法注入 SpringProcessEngineConfigurationbean, 调用AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration 方法把创建的数据源注入。
@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.activiti")
  public DataSource activitiDataSource() {return DataSourceBuilder.create().build();}

  @Bean
  public SpringProcessEngineConfiguration springProcessEngineConfiguration(
      PlatformTransactionManager transactionManager,
      SpringAsyncExecutor springAsyncExecutor) throws IOException {

    return baseSpringProcessEngineConfiguration(activitiDataSource(),
        transactionManager,
        springAsyncExecutor);
  }
}

Activiti6的数据源已经配置完成,如果是 7 的话会发现 AbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfiguration 方法已经不存在了,我们需要产考源码构建方式改造一番。

修复

Activiti7多数据源配置

配置还是要上面的。

  1. 创建 ActivitiConfig 继承 AbstractProcessEngineAutoConfiguration 方法注入SpringProcessEngineConfigurationbean。
  2. @ConfigurationProperties 加载以 spring.datasource.activiti 开头的DataSource
@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {
  
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.activiti")
  public DataSource activitiDataSource() {return DataSourceBuilder.create().build();}
  
  ... 略
}
  1. SpringProcessEngineConfiguration注入方式改为下面:
  @Bean
  @Primary
  public SpringProcessEngineConfiguration springProcessEngineConfiguration(
      PlatformTransactionManager transactionManager,
      SpringAsyncExecutor springAsyncExecutor,
      ActivitiProperties activitiProperties,
      ProcessDefinitionResourceFinder processDefinitionResourceFinder,
      @Autowired(required = false) DefaultActivityBehaviorFactoryMappingConfigurer processEngineConfigurationConfigurer,
      @Autowired(required = false) List<ProcessEngineConfigurator> processEngineConfigurators,
      UserGroupManager userGroupManager,
      DataSource dataSource) throws IOException {SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration();
    conf.setConfigurators(processEngineConfigurators);
    configureProcessDefinitionResources(processDefinitionResourceFinder,
        conf);
    conf.setDataSource(dataSource);
    conf.setTransactionManager(transactionManager);

    if (springAsyncExecutor != null) {conf.setAsyncExecutor(springAsyncExecutor);
    }
    conf.setDeploymentName(activitiProperties.getDeploymentName());
    conf.setDatabaseSchema(activitiProperties.getDatabaseSchema());
    conf.setDatabaseSchemaUpdate(activitiProperties.getDatabaseSchemaUpdate());
    conf.setDbHistoryUsed(activitiProperties.isDbHistoryUsed());
    conf.setAsyncExecutorActivate(activitiProperties.isAsyncExecutorActivate());
    if (!activitiProperties.isAsyncExecutorActivate()) {ValidatorSet springBootStarterValidatorSet = new ValidatorSet("activiti-spring-boot-starter");
      springBootStarterValidatorSet.addValidator(new AsyncPropertyValidator());
      if (conf.getProcessValidator() == null) {ProcessValidatorImpl processValidator = new ProcessValidatorImpl();
        processValidator.addValidatorSet(springBootStarterValidatorSet);
        conf.setProcessValidator(processValidator);
      } else {conf.getProcessValidator().getValidatorSets().add(springBootStarterValidatorSet);
      }
    }
    conf.setMailServerHost(activitiProperties.getMailServerHost());
    conf.setMailServerPort(activitiProperties.getMailServerPort());
    conf.setMailServerUsername(activitiProperties.getMailServerUserName());
    conf.setMailServerPassword(activitiProperties.getMailServerPassword());
    conf.setMailServerDefaultFrom(activitiProperties.getMailServerDefaultFrom());
    conf.setMailServerUseSSL(activitiProperties.isMailServerUseSsl());
    conf.setMailServerUseTLS(activitiProperties.isMailServerUseTls());

    if (userGroupManager != null) {conf.setUserGroupManager(userGroupManager);
    }

    conf.setHistoryLevel(activitiProperties.getHistoryLevel());
    conf.setCopyVariablesToLocalForTasks(activitiProperties.isCopyVariablesToLocalForTasks());
    conf.setSerializePOJOsInVariablesToJson(activitiProperties.isSerializePOJOsInVariablesToJson());
    conf.setJavaClassFieldForJackson(activitiProperties.getJavaClassFieldForJackson());

    if (activitiProperties.getCustomMybatisMappers() != null) {
      conf.setCustomMybatisMappers(getCustomMybatisMapperClasses(activitiProperties.getCustomMybatisMappers()));
    }

    if (activitiProperties.getCustomMybatisXMLMappers() != null) {
      conf.setCustomMybatisXMLMappers(new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
    }

    if (activitiProperties.getCustomMybatisXMLMappers() != null) {
      conf.setCustomMybatisXMLMappers(new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
    }

    if (activitiProperties.isUseStrongUuids()) {conf.setIdGenerator(new StrongUuidGenerator());
    }

    if (activitiProperties.getDeploymentMode() != null) {conf.setDeploymentMode(activitiProperties.getDeploymentMode());
    }

    conf.setActivityBehaviorFactory(new DefaultActivityBehaviorFactory());

    if (processEngineConfigurationConfigurer != null) {processEngineConfigurationConfigurer.configure(conf);
    }

    return conf;
  }

  private void configureProcessDefinitionResources(
      ProcessDefinitionResourceFinder processDefinitionResourceFinder,
      SpringProcessEngineConfiguration conf) throws IOException {
    List<Resource> procDefResources = processDefinitionResourceFinder
        .discoverProcessDefinitionResources();
    if (!procDefResources.isEmpty()) {conf.setDeploymentResources(procDefResources.toArray(new Resource[0]));
    }
  }

常见错误

在以上配置中可能会有 jdbcUrl is required with driverClassName 错误

解决办法如下:

# activiti 数据源
spring.datasource.activiti.driver=com.mysql.jdbc.Driver
spring.datasource.activiti.url=jdbc:mysql://10.1.1.97:3311/test-activiti7-db?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
# url 换为 jdbc-url 解决 jdbcUrl is required with driverClassName 错误
# 官方文档的解释是:# 因为连接池的实际类型没有被公开,所以在您的自定义数据源的元数据中没有生成密钥,而且在 IDE 中没有完成(因为 DataSource 接口没有暴露属性)。# 另外,如果您碰巧在类路径上有 Hikari,那么这个基本设置就不起作用了,因为 Hikari 没有 url 属性(但是确实有一个 jdbcUrl 属性)。在这种情况下,您必须重写您的配置如下:
spring.datasource.activiti.jdbc-url=${spring.datasource.activiti.url}
spring.datasource.activiti.username=root
spring.datasource.activiti.password=Rtqw123OpnmER
spring.datasource.activiti.driverClassName=com.mysql.jdbc.Driver

总结

Activiti7国内的教程不是很多,需要自己在社区里或者官方文档,源码去看了解,细心学习。

示例代码地址:activiti-starter

作者 GitHub:
Purgeyao 欢迎关注

正文完
 0