疯狂的狮子li
2022-04-22 83cdeacaa88461fdc83921864c34770252b7d0b4
update 简化 全局线程池配置 使用cpu核心数自动处理
已删除1个文件
已修改3个文件
68 ■■■■ 文件已修改
ruoyi-admin/src/main/resources/application.yml 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-common/src/main/java/com/ruoyi/common/enums/ThreadPoolRejectedPolicy.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruoyi-admin/src/main/resources/application.yml
@@ -256,20 +256,10 @@
thread-pool:
  # æ˜¯å¦å¼€å¯çº¿ç¨‹æ± 
  enabled: false
  # æ ¸å¿ƒçº¿ç¨‹æ± å¤§å°
  corePoolSize: 8
  # æœ€å¤§å¯åˆ›å»ºçš„线程数
  maxPoolSize: 16
  # é˜Ÿåˆ—最大长度
  queueCapacity: 128
  # çº¿ç¨‹æ± ç»´æŠ¤çº¿ç¨‹æ‰€å…è®¸çš„空闲时间
  keepAliveSeconds: 300
  # çº¿ç¨‹æ± å¯¹æ‹’绝任务(无线程可用)的处理策略
  # CALLER_RUNS_POLICY è°ƒç”¨æ–¹æ‰§è¡Œ
  # DISCARD_OLDEST_POLICY æ”¾å¼ƒæœ€æ—§çš„
  # DISCARD_POLICY ä¸¢å¼ƒ
  # ABORT_POLICY ä¸­æ­¢
  rejectedExecutionHandler: CALLER_RUNS_POLICY
--- # redisson ç¼“存配置
redisson:
ruoyi-common/src/main/java/com/ruoyi/common/enums/ThreadPoolRejectedPolicy.java
ÎļþÒÑɾ³ý
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java
@@ -1,7 +1,6 @@
package com.ruoyi.framework.config;
import com.ruoyi.common.utils.Threads;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.ruoyi.framework.config.properties.ThreadPoolProperties;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -10,7 +9,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@@ -23,6 +21,11 @@
@Configuration
public class ThreadPoolConfig {
    /**
     * æ ¸å¿ƒçº¿ç¨‹æ•° = cpu æ ¸å¿ƒæ•° + 1
     */
    private final int core = Runtime.getRuntime().availableProcessors() + 1;
    @Autowired
    private ThreadPoolProperties threadPoolProperties;
@@ -30,12 +33,11 @@
    @ConditionalOnProperty(prefix = "thread-pool", name = "enabled", havingValue = "true")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(threadPoolProperties.getMaxPoolSize());
        executor.setCorePoolSize(threadPoolProperties.getCorePoolSize());
        executor.setMaxPoolSize(core);
        executor.setCorePoolSize(core * 2);
        executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());
        RejectedExecutionHandler handler = ReflectUtils.newInstance(threadPoolProperties.getRejectedExecutionHandler().getClazz());
        executor.setRejectedExecutionHandler(handler);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
@@ -44,7 +46,7 @@
     */
    @Bean(name = "scheduledExecutorService")
    protected ScheduledExecutorService scheduledExecutorService() {
        return new ScheduledThreadPoolExecutor(threadPoolProperties.getCorePoolSize(),
        return new ScheduledThreadPoolExecutor(core,
            new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(),
            new ThreadPoolExecutor.CallerRunsPolicy()) {
            @Override
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java
@@ -1,6 +1,5 @@
package com.ruoyi.framework.config.properties;
import com.ruoyi.common.enums.ThreadPoolRejectedPolicy;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@@ -21,16 +20,6 @@
    private boolean enabled;
    /**
     * æ ¸å¿ƒçº¿ç¨‹æ± å¤§å°
     */
    private int corePoolSize;
    /**
     * æœ€å¤§å¯åˆ›å»ºçš„线程数
     */
    private int maxPoolSize;
    /**
     * é˜Ÿåˆ—最大长度
     */
    private int queueCapacity;
@@ -39,10 +28,5 @@
     * çº¿ç¨‹æ± ç»´æŠ¤çº¿ç¨‹æ‰€å…è®¸çš„空闲时间
     */
    private int keepAliveSeconds;
    /**
     * çº¿ç¨‹æ± å¯¹æ‹’绝任务(无线程可用)的处理策略
     */
    private ThreadPoolRejectedPolicy rejectedExecutionHandler;
}