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