huang
2025-11-25 19f59c243e8df97c8b9fd9dba4e758be8235d68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.mes.config;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
 
/**
 * 任务执行器配置
 * 用于异步执行多设备组任务
 * 
 * @author mes
 * @since 2025-01-XX
 */
@Slf4j
@Configuration
@EnableAsync
public class TaskExecutorConfig {
 
    /**
     * 设备组任务执行线程池
     * 每个设备组作为一个独立线程执行
     */
    @Bean(name = "deviceGroupTaskExecutor")
    public Executor deviceGroupTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 核心线程数:支持同时执行的核心设备组数量
        executor.setCorePoolSize(5);
        
        // 最大线程数:最多同时执行的设备组数量
        executor.setMaxPoolSize(20);
        
        // 队列容量:等待执行的设备组任务数量
        executor.setQueueCapacity(100);
        
        // 线程名前缀
        executor.setThreadNamePrefix("DeviceGroupTask-");
        
        // 线程空闲时间(秒)
        executor.setKeepAliveSeconds(60);
        
        // 拒绝策略:当线程池和队列都满时,由调用线程执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        
        // 等待时间(秒)
        executor.setAwaitTerminationSeconds(60);
        
        executor.initialize();
        
        log.info("设备组任务线程池初始化完成: corePoolSize=5, maxPoolSize=20, queueCapacity=100");
        
        return executor;
    }
}