Springboot2.1.x Configuration Activiti7 separate data source issue

Introduction to the

SpringBoot2 was recently configured based on the latest Activiti7. A simple use. It is found that there are few tutorials for solving Activiti7 in the market, and there are many pit mining. There are differences between configuring data sources in Activiti6 and Activiti7, and Activiti6 cannot be used normally in Activiti7. Now let’s look at the difference.

The problem

Activiti6Multi-data Source Configuration

The configuration of 6 is relatively simple.

  1. Add configuration first:
# activiti data source spring. The datasource. Activiti. Driver = com. Mysql.. JDBC driver Spring. The 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.DriverCopy the code

  1. with@ConfigurationPropertiesLoaded withspring.datasource.activitiAt the beginning ofDataSource.
  2. createActivitiConfiginheritanceAbstractProcessEngineAutoConfigurationMethods to injectSpringProcessEngineConfigurationBean, callAbstractProcessEngineAutoConfiguration#baseSpringProcessEngineConfigurationMethod to inject the created data source.
@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); }}Copy the code

Activiti6 data source is configured, if is 7 will find AbstractProcessEngineAutoConfiguration# baseSpringProcessEngineConfiguration method has gone, We need to reinvent the way we build source code.

repair

Activiti7Multi-data Source Configuration

The configuration is still up there.

  1. createActivitiConfiginheritanceAbstractProcessEngineAutoConfigurationMethods to injectSpringProcessEngineConfigurationBean.
  2. with@ConfigurationPropertiesLoaded withspring.datasource.activitiAt the beginning ofDataSource
@Configuration public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration { @Bean @ConfigurationProperties(prefix = "spring.datasource.activiti") public DataSource activitiDataSource() { return DataSourceBuilder.create().build(); }... Slightly}Copy the code

  1. SpringProcessEngineConfigurationChange the injection mode to the following:
@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])); }}Copy the code

Common mistakes

The jdbcUrl is required with driverClassName error may occur in the preceding configuration

The solutions are as follows:

# activiti data source spring. The datasource. Activiti. Driver = com. Mysql.. JDBC driver Spring. The datasource. Activiti. Url = JDBC: mysql: / / 10.1.1.97:3311 / test - activiti7 - db? UseUnicode =true&characterEncoding= UTf8 &useSSL=false&allowMultiQueries=true # URL Change to jdbcUrl is required with # because the actual type of the connection pool is not exposed, the key generation is not done in the metadata of your custom DataSource and is not done in the IDE (because the DataSource interface has no exposed properties). # Also, if you happen to have Hikari on your classpath, this basic setting won't work because Hikari doesn't have a URL attribute (but it does have a jdbcUrl attribute). In this case, you must rewrite your configuration as follows: 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.DriverCopy the code

conclusion

There are not many Activiti7 tutorials in China, so you need to learn about them carefully in the community or official documents and source code.

Example code address: Activiti-starter

GitHub: Purgeyao welcome to follow