package com.vci.starter.web.autoconfigure; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; 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; /** * timer需要的默认的线程池 * @author weidy * @date 2021-2-2 */ @Configuration @EnableAsync @ConditionalOnProperty(prefix = "vciasync",name="enabled",havingValue = "true",matchIfMissing = false) public class VciAsyncConfig { /** * 初始化大小 */ private int corePoolSize; /** * 最大的值 */ private int maxPoolSize; /** * 队列的个数 */ private int queueCapacity; /** * 存活时间 */ private int keepAliveSeconds = 60; /** * 默认的线程池, * @return 线程池对象 */ @Bean @ConditionalOnMissingBean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new VciThreadPoolTaskExecutor(); int cpu = Runtime.getRuntime().availableProcessors(); if(getCorePoolSize() == 0){ setCorePoolSize(cpu); } if(getMaxPoolSize() == 0){ setMaxPoolSize(2* cpu); } if(getQueueCapacity() == 0){ setQueueCapacity(100); } executor.setCorePoolSize(getCorePoolSize()); executor.setMaxPoolSize(getMaxPoolSize()); executor.setQueueCapacity(getQueueCapacity()); executor.setKeepAliveSeconds(getKeepAliveSeconds()); executor.initialize(); return executor; } public int getCorePoolSize() { return corePoolSize; } public void setCorePoolSize(int corePoolSize) { this.corePoolSize = corePoolSize; } public int getMaxPoolSize() { return maxPoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } public int getQueueCapacity() { return queueCapacity; } public void setQueueCapacity(int queueCapacity) { this.queueCapacity = queueCapacity; } public int getKeepAliveSeconds() { return keepAliveSeconds; } public void setKeepAliveSeconds(int keepAliveSeconds) { this.keepAliveSeconds = keepAliveSeconds; } @Override public String toString() { return "VciAsyncConfig{" + "corePoolSize=" + corePoolSize + ", maxPoolSize=" + maxPoolSize + ", queueCapacity=" + queueCapacity + '}'; } }