Activiti 7 集成概述

Activiti流程引擎是BPMN开源产品中做得比较优秀的产品,经历了jbpm3,jbpm4,activiti5,activiti6的几代产品迭代,目前推出针对单体应用与云应用的activiti7,重构了原有的pvm的流程引擎机制,让流程执行效率更优,平台集成activiti7,只是使用了其流程定义与流程实例,流程待办的几块特性外,大部分进行了业务外围特性的扩展,同时保留了Activiti的其他引擎能力。

依赖导入

  1. <dependency>
  2. <groupId>org.activiti</groupId>
  3. <artifactId>activiti-spring-boot-starter</artifactId>
  4. <version>7.1.0.M6</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.mybatis</groupId>
  8. <artifactId>mybatis</artifactId>
  9. </exclusion>
  10. <exclusion>
  11. <groupId>org.springframework.security</groupId>
  12. <artifactId>spring-security-core</artifactId>
  13. </exclusion>
  14. </exclusions>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.activiti</groupId>
  18. <artifactId>activiti-image-generator</artifactId>
  19. <version>7.1.0.M6</version>
  20. </dependency>

SpringBoot中引擎配置入口

  1. package com.redxun.bpm.config;
  2. import com.redxun.bpm.activiti.event.GlobalEventListener;
  3. import com.redxun.bpm.activiti.ext.FlowDefCache;
  4. import com.redxun.bpm.activiti.ext.RxIdGenerator;
  5. import com.redxun.bpm.activiti.img.ActivitiDiagramGenerator;
  6. import com.redxun.datasource.DataSourceUtil;
  7. import org.activiti.api.runtime.shared.identity.UserGroupManager;
  8. import org.activiti.engine.cfg.ProcessEngineConfigurator;
  9. import org.activiti.engine.delegate.event.ActivitiEventListener;
  10. import org.activiti.engine.impl.bpmn.parser.factory.DefaultActivityBehaviorFactory;
  11. import org.activiti.image.ProcessDiagramGenerator;
  12. import org.activiti.spring.SpringAsyncExecutor;
  13. import org.activiti.spring.SpringProcessEngineConfiguration;
  14. import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
  15. import org.activiti.spring.boot.ActivitiProperties;
  16. import org.activiti.spring.boot.DefaultActivityBehaviorFactoryMappingConfigurer;
  17. import org.activiti.spring.boot.process.validation.AsyncPropertyValidator;
  18. import org.activiti.validation.ProcessValidatorImpl;
  19. import org.activiti.validation.validator.ValidatorSet;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Qualifier;
  22. import org.springframework.context.annotation.Bean;
  23. import org.springframework.context.annotation.Configuration;
  24. import org.springframework.context.annotation.Primary;
  25. import org.springframework.core.task.SyncTaskExecutor;
  26. import org.springframework.core.task.TaskExecutor;
  27. import org.springframework.transaction.PlatformTransactionManager;
  28. import javax.sql.DataSource;
  29. import java.io.IOException;
  30. import java.util.ArrayList;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. /**
  34. * Activiti流程引擎构建配置入口
  35. * @author csx
  36. * @since 1.0
  37. */
  38. @Configuration
  39. public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {
  40. @Bean
  41. @Primary
  42. public SpringProcessEngineConfiguration springProcessEngineConfiguration(
  43. PlatformTransactionManager transactionManager,
  44. SpringAsyncExecutor springAsyncExecutor,
  45. ActivitiProperties activitiProperties,
  46. @Autowired(required = false) DefaultActivityBehaviorFactoryMappingConfigurer processEngineConfigurationConfigurer,
  47. @Autowired(required = false) List<ProcessEngineConfigurator> processEngineConfigurators,
  48. UserGroupManager userGroupManager,
  49. @Qualifier(DataSourceUtil.GLOBAL_DATASOURCE) DataSource dataSource) throws IOException {
  50. SpringProcessEngineConfiguration conf = new SpringProcessEngineConfiguration();
  51. conf.setConfigurators(processEngineConfigurators);
  52. //设置数据源
  53. conf.setDataSource(dataSource);
  54. //设置全局事务管理器
  55. conf.setTransactionManager(transactionManager);
  56. if (springAsyncExecutor != null) {
  57. conf.setAsyncExecutor(springAsyncExecutor);
  58. }
  59. //设置发布名
  60. conf.setDeploymentName(activitiProperties.getDeploymentName());
  61. conf.setDatabaseSchema(activitiProperties.getDatabaseSchema());
  62. conf.setDatabaseSchemaUpdate(activitiProperties.getDatabaseSchemaUpdate());
  63. conf.setDbHistoryUsed(activitiProperties.isDbHistoryUsed());
  64. conf.setAsyncExecutorActivate(activitiProperties.isAsyncExecutorActivate());
  65. if (!activitiProperties.isAsyncExecutorActivate()) {
  66. ValidatorSet springBootStarterValidatorSet = new ValidatorSet("activiti-spring-boot-starter");
  67. springBootStarterValidatorSet.addValidator(new AsyncPropertyValidator());
  68. if (conf.getProcessValidator() == null) {
  69. ProcessValidatorImpl processValidator = new ProcessValidatorImpl();
  70. processValidator.addValidatorSet(springBootStarterValidatorSet);
  71. conf.setProcessValidator(processValidator);
  72. } else {
  73. conf.getProcessValidator().getValidatorSets().add(springBootStarterValidatorSet);
  74. }
  75. }
  76. //设置邮件发送配置
  77. conf.setMailServerHost(activitiProperties.getMailServerHost());
  78. conf.setMailServerPort(activitiProperties.getMailServerPort());
  79. conf.setMailServerUsername(activitiProperties.getMailServerUserName());
  80. conf.setMailServerPassword(activitiProperties.getMailServerPassword());
  81. conf.setMailServerDefaultFrom(activitiProperties.getMailServerDefaultFrom());
  82. conf.setMailServerUseSSL(activitiProperties.isMailServerUseSsl());
  83. conf.setMailServerUseTLS(activitiProperties.isMailServerUseTls());
  84. if (userGroupManager != null) {
  85. conf.setUserGroupManager(userGroupManager);
  86. }
  87. conf.setHistoryLevel(activitiProperties.getHistoryLevel());
  88. conf.setCopyVariablesToLocalForTasks(activitiProperties.isCopyVariablesToLocalForTasks());
  89. conf.setSerializePOJOsInVariablesToJson(activitiProperties.isSerializePOJOsInVariablesToJson());
  90. conf.setJavaClassFieldForJackson(activitiProperties.getJavaClassFieldForJackson());
  91. if (activitiProperties.getCustomMybatisMappers() != null) {
  92. conf.setCustomMybatisMappers(
  93. getCustomMybatisMapperClasses(activitiProperties.getCustomMybatisMappers()));
  94. }
  95. if (activitiProperties.getCustomMybatisXMLMappers() != null) {
  96. conf.setCustomMybatisXMLMappers(
  97. new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
  98. }
  99. if (activitiProperties.getCustomMybatisXMLMappers() != null) {
  100. conf.setCustomMybatisXMLMappers(
  101. new HashSet<>(activitiProperties.getCustomMybatisXMLMappers()));
  102. }
  103. //设置其Id产生器
  104. conf.setIdGenerator(new RxIdGenerator());
  105. if (activitiProperties.getDeploymentMode() != null) {
  106. conf.setDeploymentMode(activitiProperties.getDeploymentMode());
  107. }
  108. conf.setActivityBehaviorFactory(new DefaultActivityBehaviorFactory());
  109. if (processEngineConfigurationConfigurer != null) {
  110. processEngineConfigurationConfigurer.configure(conf);
  111. }
  112. //加上全局事件的控制
  113. List<ActivitiEventListener> eventListeners=new ArrayList<>();
  114. eventListeners.add(new GlobalEventListener());
  115. conf.setEventListeners(eventListeners);
  116. //修改其流程缓存
  117. conf.setProcessDefinitionCache(new FlowDefCache());
  118. return conf;
  119. }
  120. @Override
  121. @Primary
  122. @Bean
  123. public TaskExecutor taskExecutor(){
  124. return new SyncTaskExecutor();
  125. }
  126. @Bean
  127. public ProcessDiagramGenerator activitiDiagramGenerator(){
  128. return new ActivitiDiagramGenerator();
  129. }
  130. }

在application.yml配置数据源

  1. defaultDs:
  2. string:
  3. # url: jdbc:dm://localhost:5236/JPAASBPM?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
  4. # username: JPAASBPM
  5. # password: JPAASBPM
  6. # driver-class-name: dm.jdbc.driver.DmDriver
  7. # connection-init-sqls: SET SCHEMA JPAASBPM
  8. #url: ${bpm.datasource.url}://${bpm.datasource.ip}:${bpm.datasource.port}/${bpm.datasource.name}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
  9. url: ${bpm.datasource.url}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
  10. username: ${bpm.datasource.username}
  11. password: ${bpm.datasource.password}
  12. driver-class-name: ${bpm.datasource.driver}
  13. connection-init-sqls: ${bpm.datasource.connection}
  14. validationQuery: select 1
  15. filters: stat
  16. int:
  17. initialSize: 5
  18. minIdle: 5
  19. maxActive: 10
  20. maxPoolPreparedStatementPerConnectionSize: 20
  21. long:
  22. maxWait: 60000
  23. timeBetweenEvictionRunsMillis: 60000
  24. minEvictableIdleTimeMillis: 300000
  25. boolean:
  26. testWhileIdle: true
  27. testOnBorrow: false
  28. testOnReturn: false
  29. poolPreparedStatements: true
  30. mybatis:
  31. mapper-locations: classpath:/mapper/**/*Mapper.xml

在application.yml引入seata支持分布式事务

  1. seata:
  2. enabled: true
  3. application-id: ${spring.application.name}
  4. tx-service-group: bpm_tx_group
  5. enable-auto-data-source-proxy: true
  6. registry:
  7. type: nacos
  8. nacos:
  9. application: seata-server
  10. server-addr: ${nacos.address}
  11. group: DEFAULT_GROUP
  12. userName: nacos
  13. password: nacos
  14. config:
  15. type: nacos
  16. nacos:
  17. namespace:
  18. serverAddr: ${nacos.address}
  19. group: SEATA_GROUP
  20. cluster: default
  21. userName: nacos
  22. password: nacos

在业务服务类中使用Activiti服务类

  1. @Service
  2. public class ActRepService {
  3. @Resource
  4. RepositoryService repositoryService;
  5. @Resource
  6. ManagementService managementService;
  7. @Resource
  8. RuntimeService runtimeService;
  9. @Resource
  10. TaskService taskService;
文档更新时间: 2020-10-12 09:25   作者:csx