Parcourir la source

增加线程池最大线程数和核心线程数的配置

lixing il y a 3 ans
Parent
commit
f312473f71

+ 5 - 6
src/main/java/com/persagy/client/WebSocketClientFactory.java

@@ -65,12 +65,8 @@ public class WebSocketClientFactory {
      */
     @Value("${project.iotid}")
     String iotProjectId;
-    ExecutorService executor = ExecutorBuilder.create()
-            .setCorePoolSize(5)
-            .setMaxPoolSize(10)
-            .setWorkQueue(new LinkedBlockingQueue<>(102400))
-            .setHandler(new ThreadPoolExecutor.AbortPolicy())
-            .build();
+
+
     private WebSocketClient outCallWebSocketClientHolder;
 
     public WebSocketClient getOutCallWebSocketClientHolder() {
@@ -84,6 +80,9 @@ public class WebSocketClientFactory {
     @Autowired
     AlarmHandleService alarmHandleService;
 
+    @Autowired
+    ExecutorService executor;
+
 
     AtomicInteger total = new AtomicInteger(0);
 

+ 40 - 0
src/main/java/com/persagy/client/WebSocketThreadPool.java

@@ -0,0 +1,40 @@
+package com.persagy.client;
+
+import cn.hutool.core.thread.ExecutorBuilder;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 处理webSocket消息的线程池
+ *
+ * @author lixing
+ * @version V1.0 2022/1/24 11:24 上午
+ **/
+@Configuration
+public class WebSocketThreadPool {
+    /**
+     * 核心线程数
+     */
+    @Value("${corePoolSize:5}")
+    int corePoolSize;
+    /**
+     * 最大线程数
+     */
+    @Value("${maxPoolSize:10}")
+    int maxPoolSize;
+
+    @Bean
+    public ExecutorService executor() {
+        return ExecutorBuilder.create()
+                .setCorePoolSize(corePoolSize)
+                .setMaxPoolSize(maxPoolSize)
+                .setWorkQueue(new LinkedBlockingQueue<>(102400))
+                .setHandler(new ThreadPoolExecutor.AbortPolicy())
+                .build();
+    }
+}

+ 2 - 0
src/main/resources/application.yml

@@ -1,3 +1,5 @@
+corePoolSize: 30
+maxPoolSize: 50
 server:
   port: 8101
 spring: