package com.zhgd.xmgl.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.security.core.context.SecurityContextHolder; import javax.annotation.PostConstruct; import java.util.concurrent.ThreadPoolExecutor; /** * @program: wisdomSite * @description: 线程池配置 * @author: Mr.Peng * @create: 2021-02-23 10:04 **/ @Configuration public class AsyncConfig { /** * Set the ThreadPoolExecutor's core pool size. */ private int corePoolSize = 8; /** * Set the ThreadPoolExecutor's maximum pool size. */ private int maxPoolSize = 16; /** * Set the capacity for the ThreadPoolExecutor's BlockingQueue. */ private int queueCapacity = 200; @PostConstruct public void init() { SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL); } /** * 默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务, * 当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; * 当队列满了,就继续创建线程,当线程数量大于等于maxPoolSize后,开始使用拒绝策略拒绝 */ @Primary @Bean("taskExecutor") public ThreadPoolTaskExecutor taskExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("taskExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } /** * 默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务, * 当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; * 当队列满了,就继续创建线程,当线程数量大于等于maxPoolSize后,开始使用拒绝策略拒绝 */ @Bean("doubleCarbonExecutor") public ThreadPoolTaskExecutor doubleCarbonExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("doubleCarbonExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("countAttendanceExecutor") public ThreadPoolTaskExecutor attendanceExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("countAttendanceExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("sendAttendanceExecutor") public ThreadPoolTaskExecutor sendAttendanceExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("sendAttendanceExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("sendWorkerExecutor") public ThreadPoolTaskExecutor sendWorkerExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("sendWorkerExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("sendExitWorkerExecutor") public ThreadPoolTaskExecutor sendExitWorkerExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("sendExitWorkerExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("environmentNoiseDataExecutor") public ThreadPoolTaskExecutor sendEnvironmentNoiseDataExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("environmentNoiseDataExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("electricalExecutor") public ThreadPoolTaskExecutor sendElectricalExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("electricalExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("towerExecutor") public ThreadPoolTaskExecutor sendTowerExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("towerExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("lifterExecutor") public ThreadPoolTaskExecutor sendLifterExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("lifterExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("devExcavationExecutor") public ThreadPoolTaskExecutor sendDevExcavationEExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("devExcavationExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("gantryCraneExecutor") public ThreadPoolTaskExecutor sendGantryCraneExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("gantryCraneExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("fileExecutor") public ThreadPoolTaskExecutor fileExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("fileExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("asyncExecutor") public ThreadPoolTaskExecutor asyncExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("asyncExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Primary @Bean("bimExecutor") public ThreadPoolTaskExecutor bimExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(2000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("bimExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } /** * 单个线程 * * @return */ @Bean("hkSimpleAuthExecutor") public ThreadPoolTaskExecutor hikvisionExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(1); /** 最大线程数 */ executor.setMaxPoolSize(1); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("sendWorkerExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("carInfoExecutor") public MdcThreadPoolTaskExecutor carInfoExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(100000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("carInfoExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("getCrossRecords") public MdcThreadPoolTaskExecutor getCrossRecords() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("getCrossRecords-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("getDoorEvents") public MdcThreadPoolTaskExecutor getDoorEvents() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("getDoorEvents-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("saveEventCallbackAttendance") public MdcThreadPoolTaskExecutor saveEventCallbackAttendance() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("saveEventCallbackAttendance-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("saveEventCallbackCarPassRecord") public MdcThreadPoolTaskExecutor saveEventCallbackCarPassRecord() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("saveEventCallbackCarPassRecord-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("saveEventCallbackAiAsync") public MdcThreadPoolTaskExecutor saveEventCallbackAiAsync() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("saveEventCallbackAiAsync-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("getRecordForHikvisionTask") public MdcThreadPoolTaskExecutor getRecordForHikvisionTask() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(1); /** 最大线程数 */ executor.setMaxPoolSize(160); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("getRecordForHikvisionTask-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("syncXzHikvisionCompareData") public MdcThreadPoolTaskExecutor syncXzHikvisionCompareData() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(corePoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("syncXzHikvisionCompareData-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("sendBatchExecutor") public ThreadPoolTaskExecutor sendBatchExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("sendBatchExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("workerHkExecutor") public ThreadPoolTaskExecutor workerHkExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity * 100); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("workerHkExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("workerAuthHkExecutor") public ThreadPoolTaskExecutor workerAuthHkExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity * 100); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("workerAuthHkExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("workerBeWaitHkExecutor") public ThreadPoolTaskExecutor workerBeWaitHkExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(maxPoolSize); /** 最大线程数 */ executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity * 100); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("workerBeWaitHkExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } @Bean("saveVideoItemCover") public ThreadPoolTaskExecutor saveVideoItemCoverExecutor() { MdcThreadPoolTaskExecutor executor = new MdcThreadPoolTaskExecutor(); /** 核心线程数(默认线程数) */ executor.setCorePoolSize(1); /** 最大线程数 */ executor.setMaxPoolSize(1); executor.setQueueCapacity(queueCapacity * 1000); /** 允许线程空闲时间(单位:默认为秒) */ executor.setKeepAliveSeconds(60); /** 线程池名前缀 */ executor.setThreadNamePrefix("saveVideoItemCoverExecutor-"); // 线程池对拒绝任务的处理策略 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy()); // 初始化 executor.initialize(); return executor; } }