From 0ad7b185212da31d8b6afdab6dd9cd8f72f83acb Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 26 Jun 2024 22:51:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E9=87=8D=E6=9E=84=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=B1=A0=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ThreadPoolAutoConfiguration.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/threadpool/ThreadPoolAutoConfiguration.java b/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/threadpool/ThreadPoolAutoConfiguration.java index a4cc5b38..24e6e837 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/threadpool/ThreadPoolAutoConfiguration.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/autoconfigure/threadpool/ThreadPoolAutoConfiguration.java @@ -18,6 +18,7 @@ package top.continew.starter.core.autoconfigure.threadpool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -25,6 +26,7 @@ import org.springframework.boot.task.TaskExecutorCustomizer; import org.springframework.boot.task.TaskSchedulerCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Lazy; +import org.springframework.scheduling.annotation.EnableScheduling; import top.continew.starter.core.constant.PropertiesConstants; /** @@ -40,8 +42,11 @@ public class ThreadPoolAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(ThreadPoolAutoConfiguration.class); - /** 核心(最小)线程数 = CPU 核心数 + 1 */ - private final int corePoolSize = Runtime.getRuntime().availableProcessors() + 1; + @Value("${spring.task.execution.pool.core-size:#{T(java.lang.Runtime).getRuntime().availableProcessors() + 1}}") + private int corePoolSize; + + @Value("${spring.task.execution.pool.max-size:#{T(java.lang.Runtime).getRuntime().availableProcessors() * 2}}") + private int maxPoolSize; /** * 异步任务线程池配置 @@ -50,14 +55,10 @@ public class ThreadPoolAutoConfiguration { @ConditionalOnProperty(prefix = "spring.task.execution.extension", name = PropertiesConstants.ENABLED, matchIfMissing = true) public TaskExecutorCustomizer taskExecutorCustomizer(ThreadPoolExtensionProperties properties) { return executor -> { - if (executor.getMaxPoolSize() == Integer.MAX_VALUE) { - // 核心(最小)线程数 - executor.setCorePoolSize(corePoolSize); - // 最大线程数 - executor.setMaxPoolSize(corePoolSize * 2); - // 队列容量 - executor.setQueueCapacity(executor.getMaxPoolSize()); - } + // 核心(最小)线程数 + executor.setCorePoolSize(corePoolSize); + // 最大线程数 + executor.setMaxPoolSize(maxPoolSize); // 当线程池的任务缓存队列已满并且线程池中的线程数已达到 maxPoolSize 时采取的任务拒绝策略 executor.setRejectedExecutionHandler(properties.getExecution() .getRejectedPolicy() @@ -67,19 +68,19 @@ public class ThreadPoolAutoConfiguration { } /** - * 调度任务线程池配置 + * 定时任务线程池配置 */ - @Bean + @EnableScheduling @ConditionalOnProperty(prefix = "spring.task.scheduling.extension", name = PropertiesConstants.ENABLED, matchIfMissing = true) - public TaskSchedulerCustomizer taskSchedulerCustomizer(ThreadPoolExtensionProperties properties) { - return executor -> { - if (executor.getPoolSize() <= 1) { - executor.setPoolSize(corePoolSize); - } - executor.setRejectedExecutionHandler(properties.getScheduling() - .getRejectedPolicy() - .getRejectedExecutionHandler()); - log.debug("[ContiNew Starter] - Auto Configuration 'TaskScheduler' completed initialization."); - }; + public static class TaskSchedulerConfiguration { + @Bean + public TaskSchedulerCustomizer taskSchedulerCustomizer(ThreadPoolExtensionProperties properties) { + return executor -> { + executor.setRejectedExecutionHandler(properties.getScheduling() + .getRejectedPolicy() + .getRejectedExecutionHandler()); + log.debug("[ContiNew Starter] - Auto Configuration 'TaskScheduler' completed initialization."); + }; + } } }