春风项目四线(合箱线、总装线)
懒羊羊
2024-01-08 fd220719968cd03cf50bb43dfca021ad0d256b4e
提交 | 用户 | 时间
fd2207 1 package com.jcdm.framework.config;
2
3 import com.jcdm.common.utils.Threads;
4 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
5 import org.springframework.context.annotation.Bean;
6 import org.springframework.context.annotation.Configuration;
7 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
8 import java.util.concurrent.ScheduledExecutorService;
9 import java.util.concurrent.ScheduledThreadPoolExecutor;
10 import java.util.concurrent.ThreadPoolExecutor;
11
12 /**
13  * 线程池配置
14  *
15  * @author jc
16  **/
17 @Configuration
18 public class ThreadPoolConfig
19 {
20     // 核心线程池大小
21     private int corePoolSize = 50;
22
23     // 最大可创建的线程数
24     private int maxPoolSize = 200;
25
26     // 队列最大长度
27     private int queueCapacity = 1000;
28
29     // 线程池维护线程所允许的空闲时间
30     private int keepAliveSeconds = 300;
31
32     @Bean(name = "threadPoolTaskExecutor")
33     public ThreadPoolTaskExecutor threadPoolTaskExecutor()
34     {
35         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
36         executor.setMaxPoolSize(maxPoolSize);
37         executor.setCorePoolSize(corePoolSize);
38         executor.setQueueCapacity(queueCapacity);
39         executor.setKeepAliveSeconds(keepAliveSeconds);
40         // 线程池对拒绝任务(无线程可用)的处理策略
41         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
42         return executor;
43     }
44
45     /**
46      * 执行周期性或定时任务
47      */
48     @Bean(name = "scheduledExecutorService")
49     protected ScheduledExecutorService scheduledExecutorService()
50     {
51         return new ScheduledThreadPoolExecutor(corePoolSize,
52                 new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(),
53                 new ThreadPoolExecutor.CallerRunsPolicy())
54         {
55             @Override
56             protected void afterExecute(Runnable r, Throwable t)
57             {
58                 super.afterExecute(r, t);
59                 Threads.printException(r, t);
60             }
61         };
62     }
63 }