From f3d6d1e43bae088f7f2b85b629c951474cebe256 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期四, 02 九月 2021 15:20:31 +0800
Subject: [PATCH] update 优化全局线程池配置 使用泛型 防止错误输入

---
 ruoyi-common/src/main/java/com/ruoyi/common/enums/ThreadPoolRejectedPolicy.java               |   26 ++++++++++++++++++++++++++
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java |    3 ++-
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java                |   18 ++----------------
 ruoyi-admin/src/main/resources/application.yml                                                |   10 +++++-----
 4 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml
index 6363e47..d94ca65 100644
--- a/ruoyi-admin/src/main/resources/application.yml
+++ b/ruoyi-admin/src/main/resources/application.yml
@@ -246,11 +246,11 @@
   # 绾跨▼姹犵淮鎶ょ嚎绋嬫墍鍏佽鐨勭┖闂叉椂闂�
   keepAliveSeconds: 300
   # 绾跨▼姹犲鎷掔粷浠诲姟(鏃犵嚎绋嬪彲鐢�)鐨勫鐞嗙瓥鐣�
-  # CallerRunsPolicy 绛夊緟
-  # DiscardOldestPolicy 鏀惧純鏈�鏃х殑
-  # DiscardPolicy 涓㈠純
-  # AbortPolicy 涓
-  rejectedExecutionHandler: CallerRunsPolicy
+  # CALLER_RUNS_POLICY 绛夊緟
+  # DISCARD_OLDEST_POLICY 鏀惧純鏈�鏃х殑
+  # DISCARD_POLICY 涓㈠純
+  # ABORT_POLICY 涓
+  rejectedExecutionHandler: CALLER_RUNS_POLICY
 
 # feign 鐩稿叧閰嶇疆
 feign:
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/enums/ThreadPoolRejectedPolicy.java b/ruoyi-common/src/main/java/com/ruoyi/common/enums/ThreadPoolRejectedPolicy.java
new file mode 100644
index 0000000..0c40f34
--- /dev/null
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/enums/ThreadPoolRejectedPolicy.java
@@ -0,0 +1,26 @@
+package com.ruoyi.common.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 绾跨▼姹� 鎷掔粷绛栫暐 娉涘瀷
+ *
+ * @author Lion Li
+ */
+@Getter
+@AllArgsConstructor
+public enum ThreadPoolRejectedPolicy {
+
+    CALLER_RUNS_POLICY("绛夊緟", ThreadPoolExecutor.CallerRunsPolicy.class),
+    DISCARD_OLDEST_POLICY("鏀惧純鏈�鏃х殑", ThreadPoolExecutor.DiscardOldestPolicy.class),
+    DISCARD_POLICY("涓㈠純", ThreadPoolExecutor.DiscardPolicy.class),
+    ABORT_POLICY("涓", ThreadPoolExecutor.AbortPolicy.class);
+
+    private final String name;
+    private final Class<? extends RejectedExecutionHandler> clazz;
+
+}
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java
index 7758e97..5dd2b5f 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ThreadPoolConfig.java
@@ -1,6 +1,7 @@
 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;
@@ -12,7 +13,6 @@
 import java.util.concurrent.RejectedExecutionHandler;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadPoolExecutor;
 
 /**
  * 绾跨▼姹犻厤缃�
@@ -33,21 +33,7 @@
         executor.setCorePoolSize(threadPoolProperties.getCorePoolSize());
         executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());
         executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());
-        RejectedExecutionHandler handler;
-        switch (threadPoolProperties.getRejectedExecutionHandler()) {
-            case "CallerRunsPolicy":
-                handler = new ThreadPoolExecutor.CallerRunsPolicy();
-                break;
-            case "DiscardOldestPolicy":
-                handler = new ThreadPoolExecutor.DiscardOldestPolicy();
-                break;
-            case "DiscardPolicy":
-                handler = new ThreadPoolExecutor.DiscardPolicy();
-                break;
-            default:
-                handler = new ThreadPoolExecutor.AbortPolicy();
-                break;
-        }
+        RejectedExecutionHandler handler = ReflectUtils.newInstance(threadPoolProperties.getRejectedExecutionHandler().getClazz());
         executor.setRejectedExecutionHandler(handler);
         return executor;
     }
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java
index 08b6842..fbffc0f 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/ThreadPoolProperties.java
@@ -1,5 +1,6 @@
 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;
@@ -42,6 +43,6 @@
     /**
      * 绾跨▼姹犲鎷掔粷浠诲姟(鏃犵嚎绋嬪彲鐢�)鐨勫鐞嗙瓥鐣�
      */
-    private String rejectedExecutionHandler;
+    private ThreadPoolRejectedPolicy rejectedExecutionHandler;
 
 }

--
Gitblit v1.9.3