package com.tievd.jyz.config.mqtt; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; 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.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; /** * 自定义事件async线程池配置 * @author timi */ @Slf4j @Configuration @EnableAsync public class EventAsyncConfig { @Value("${init.pool.custom.corePoolSize:10}") private Integer corePoolSize; @Value("${init.pool.custom.maxPoolSize:20}") private Integer maxPoolSize; @Value("${init.pool.custom.queueCapacity:10000}") private Integer queueCapacity; @Value("${init.pool.custom.keepAliveSeconds:100}") private Integer keepAliveSeconds; @Bean public Executor eventTaskAsyncPool() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); //核心线程数 executor.setCorePoolSize(corePoolSize); //最大线程数 executor.setMaxPoolSize(maxPoolSize); //队列大小 executor.setQueueCapacity(queueCapacity); //线程最大空闲时间 executor.setKeepAliveSeconds(keepAliveSeconds); executor.setThreadNamePrefix("event-async-Executor-"); executor.setRejectedExecutionHandler(new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) { long submitted = threadPoolExecutor.getTaskCount(); long completed = threadPoolExecutor.getCompletedTaskCount(); log.error("自定义事件线程池已满,丢弃!任务总数:{},未完成任务数:{}",submitted,(submitted-completed)); } }); executor.initialize(); return executor; } }