houzhongjian
2024-12-03 874287a4c02d0a980d8b97c4a691b4f37ec5e812
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.framework.flowable.config;
H 2
3 import cn.hutool.core.collection.ListUtil;
4 import com.iailab.module.bpm.framework.flowable.core.behavior.BpmActivityBehaviorFactory;
5 import com.iailab.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker;
6 import com.iailab.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateStrategy;
7 import com.iailab.module.bpm.framework.flowable.core.event.BpmProcessInstanceEventPublisher;
8 import com.iailab.module.system.api.user.AdminUserApi;
9 import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
10 import org.flowable.spring.SpringProcessEngineConfiguration;
11 import org.flowable.spring.boot.EngineConfigurationConfigurer;
12 import org.springframework.beans.factory.ObjectProvider;
13 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
14 import org.springframework.context.ApplicationEventPublisher;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Configuration;
17 import org.springframework.core.task.AsyncListenableTaskExecutor;
18 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
19
20 import java.util.List;
21
22 /**
23  * BPM 模块的 Flowable 配置类
24  *
25  * @author jason
26  */
27 @Configuration(proxyBeanMethods = false)
28 public class BpmFlowableConfiguration {
29
30     /**
31      * 参考 {@link org.flowable.spring.boot.FlowableJobConfiguration} 类,创建对应的 AsyncListenableTaskExecutor Bean
32      *
33      * 如果不创建,会导致项目启动时,Flowable 报错的问题
34      */
35     @Bean(name = "applicationTaskExecutor")
36     @ConditionalOnMissingBean(name = "applicationTaskExecutor")
37     public AsyncListenableTaskExecutor taskExecutor() {
38         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
39         executor.setCorePoolSize(8);
40         executor.setMaxPoolSize(8);
41         executor.setQueueCapacity(100);
42         executor.setThreadNamePrefix("flowable-task-Executor-");
43         executor.setAwaitTerminationSeconds(30);
44         executor.setWaitForTasksToCompleteOnShutdown(true);
45         executor.setAllowCoreThreadTimeOut(true);
46         executor.initialize();
47         return executor;
48     }
49
50     /**
51      * BPM 模块的 ProcessEngineConfigurationConfigurer 实现类:
52      *
53      * 1. 设置各种监听器
54      * 2. 设置自定义的 ActivityBehaviorFactory 实现
55      */
56     @Bean
57     public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> bpmProcessEngineConfigurationConfigurer(
58             ObjectProvider<FlowableEventListener> listeners,
59             BpmActivityBehaviorFactory bpmActivityBehaviorFactory) {
60         return configuration -> {
61             // 注册监听器,例如说 BpmActivityEventListener
62             configuration.setEventListeners(ListUtil.toList(listeners.iterator()));
63             // 设置 ActivityBehaviorFactory 实现类,用于流程任务的审核人的自定义
64             configuration.setActivityBehaviorFactory(bpmActivityBehaviorFactory);
65         };
66     }
67
68     // =========== 审批人相关的 Bean ==========
69
70     @Bean
71     public BpmActivityBehaviorFactory bpmActivityBehaviorFactory(BpmTaskCandidateInvoker bpmTaskCandidateInvoker) {
72         BpmActivityBehaviorFactory bpmActivityBehaviorFactory = new BpmActivityBehaviorFactory();
73         bpmActivityBehaviorFactory.setTaskCandidateInvoker(bpmTaskCandidateInvoker);
74         return bpmActivityBehaviorFactory;
75     }
76
77     @Bean
78     @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") // adminUserApi 可以注入成功
79     public BpmTaskCandidateInvoker bpmTaskCandidateInvoker(List<BpmTaskCandidateStrategy> strategyList,
80                                                            AdminUserApi adminUserApi) {
81         return new BpmTaskCandidateInvoker(strategyList, adminUserApi);
82     }
83
84     // =========== 自己拓展的 Bean ==========
85
86     @Bean
87     public BpmProcessInstanceEventPublisher processInstanceEventPublisher(ApplicationEventPublisher publisher) {
88         return new BpmProcessInstanceEventPublisher(publisher);
89     }
90
91 }