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 CommandAsyncConfig {
|
@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 commandTaskAsyncPool() {
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
//核心线程数
|
executor.setCorePoolSize(corePoolSize);
|
//最大线程数
|
executor.setMaxPoolSize(maxPoolSize);
|
//队列大小
|
executor.setQueueCapacity(queueCapacity);
|
//线程最大空闲时间
|
executor.setKeepAliveSeconds(keepAliveSeconds);
|
executor.setThreadNamePrefix("command-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;
|
}
|
}
|