Springboot2.1.x配置Activiti7單獨數據源問題

Springboot2.1.x配置Activiti7單獨數據源問題

簡介

最近基於最新的Activiti7配置了SpringBoot2
簡單上手使用了一番。發現市面上解決Activiti7的教程不多,採坑也比較多,在Activiti6配置數據源和Activiti7有所區別,基於Activiti6Activiti7裏是沒法正常使用的。接下來讓咱們看下區別。mysql

問題

Activiti6多數據源配置

6的配置比較簡單點。git

  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方法已經不存在了,咱們須要產考源碼構建方式改造一番。github

修復

Activiti7多數據源配置

配置仍是要上面的。spring

  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錯誤sql

解決辦法以下:app

# 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國內的教程不是不少,須要本身在社區裏或者官方文檔,源碼去看了解,細心學習。spring-boot

示例代碼地址: activiti-starter

做者GitHub:
Purgeyao 歡迎關注學習

相關文章
相關標籤/搜索