From bda4a97ee41aaac639220ebc030335f9d3342fdb Mon Sep 17 00:00:00 2001
From: ZengTao <2773468879@qq.com>
Date: 星期五, 06 九月 2024 15:14:30 +0800
Subject: [PATCH] 添加线程异步执行

---
 UI-Project/config.js                                                                                    |    8 +-
 hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/DownLoadCacheGlassTask.java |    5 +
 hangzhoumesParent/common/servicebase/src/main/java/com/mes/common/config/AsyncTaskExecutorConfig.java   |   51 +++++++++++++++++
 UI-Project/src/utils/constants.js                                                                       |    4 
 hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java               |   85 ++++++++++++++++------------
 hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/resources/application.yml                    |    7 ++
 6 files changed, 117 insertions(+), 43 deletions(-)

diff --git a/UI-Project/config.js b/UI-Project/config.js
index 9a4f798..8801d0c 100644
--- a/UI-Project/config.js
+++ b/UI-Project/config.js
@@ -1,10 +1,10 @@
 export default {
- serverUrl: "10.153.19.150:88/api",
+//  serverUrl: "10.153.19.150:88/api",
 //  serverUrl: "192.168.1.199:88/api",
-//  serverUrl: "127.0.0.1:88/api",
-  serverUrl2: "10.153.19.150:88"
+ serverUrl: "127.0.0.1:88/api",
+  // serverUrl2: "10.153.19.150:88"
   // serverUrl2: "192.168.1.199:88"
-    // serverUrl2: "127.0.0.1:88"
+    serverUrl2: "127.0.0.1:88"
 
   //serverUrl:"res.abeim.cn"
 }
\ No newline at end of file
diff --git a/UI-Project/src/utils/constants.js b/UI-Project/src/utils/constants.js
index b551cfb..69000bc 100644
--- a/UI-Project/src/utils/constants.js
+++ b/UI-Project/src/utils/constants.js
@@ -1,4 +1,4 @@
 //  export const WebSocketHost = "192.168.1.199";
-export const WebSocketHost = "10.153.19.150";
-// export const WebSocketHost = "127.0.0.1";
+// export const WebSocketHost = "10.153.19.150";
+export const WebSocketHost = "127.0.0.1";
 export const host = "88";
\ No newline at end of file
diff --git a/hangzhoumesParent/common/servicebase/src/main/java/com/mes/common/config/AsyncTaskExecutorConfig.java b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/common/config/AsyncTaskExecutorConfig.java
new file mode 100644
index 0000000..c36aa7c
--- /dev/null
+++ b/hangzhoumesParent/common/servicebase/src/main/java/com/mes/common/config/AsyncTaskExecutorConfig.java
@@ -0,0 +1,51 @@
+package com.mes.common.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.task.TaskExecutor;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * <p> @Title AsyncTaskExecutorConfig
+ * <p> @Description 寮傛绾跨▼姹犻厤缃�
+ *
+ * @author ACGkaka
+ * @date 2023/4/24 19:48
+ */
+@EnableAsync
+@Configuration
+public class AsyncTaskExecutorConfig {
+
+    /**
+     * 鏍稿績绾跨▼鏁帮紙绾跨▼姹犵淮鎶ょ嚎绋嬬殑鏈�灏忔暟閲忥級
+     */
+    private int corePoolSize = 10;
+    /**
+     * 鏈�澶х嚎绋嬫暟锛堢嚎绋嬫睜缁存姢绾跨▼鐨勬渶澶ф暟閲忥級
+     */
+    private int maxPoolSize = 200;
+    /**
+     * 闃熷垪鏈�澶ч暱搴�
+     */
+    private int queueCapacity = 10;
+
+    @Bean
+    public TaskExecutor taskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(corePoolSize);
+        executor.setMaxPoolSize(maxPoolSize);
+        executor.setQueueCapacity(queueCapacity);
+        executor.setThreadNamePrefix("MyExecutor-");
+        // for passing in request scope context 杞崲璇锋眰鑼冨洿鐨勪笂涓嬫枃
+//        executor.setTaskDecorator(new ContextCopyingDecorator());
+        // rejection-policy锛氬綋pool宸茬粡杈惧埌max size鐨勬椂鍊欙紝濡備綍澶勭悊鏂颁换鍔�
+        // CALLER_RUNS锛氫笉鍦ㄦ柊绾跨▼涓墽琛屼换鍔★紝鑰屾槸鏈夎皟鐢ㄨ�呮墍鍦ㄧ殑绾跨▼鏉ユ墽琛�
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.setWaitForTasksToCompleteOnShutdown(true);
+        executor.initialize();
+        return executor;
+    }
+}
diff --git a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/DownLoadCacheGlassTask.java b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/DownLoadCacheGlassTask.java
index d1fc2b2..95facd4 100644
--- a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/DownLoadCacheGlassTask.java
+++ b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/DownLoadCacheGlassTask.java
@@ -30,6 +30,7 @@
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
@@ -80,6 +81,7 @@
 
 
     @Scheduled(fixedDelay = 300)
+    @Async
     public void plcHomeEdgTask() {
         PlcParameterObject plcParameterObject = S7object.getinstance().PlcMesObject;
         String requestWord = plcParameterObject.getPlcParameter("requestWord").getValue();
@@ -129,6 +131,7 @@
     }
 
     @Scheduled(fixedDelay = 300)
+    @Async
     public void plcShelfFull() {
         List<DownWorkstation> list = downGlassInfoService.queryWorkStationIsFull();
         if (CollectionUtils.isNotEmpty(list)) {
@@ -139,6 +142,7 @@
     }
 
     @Scheduled(fixedDelay = 1000)
+    @Async
     public void dealDamageTask() {
         Date startDate = new Date();
         log.info("涓嬬墖鐮存崯鐜荤拑娓呴櫎浠诲姟寮�濮嬫墽琛屾椂闂达細{}", startDate);
@@ -183,6 +187,7 @@
      * 鏇存柊宸茬粦瀹氬伐浣嶆祦绋嬪崱鐨勭牬鎹�/鎷胯蛋鐨勬暟閲忎俊鎭�
      */
     @Scheduled(fixedDelay = 1000)
+    @Async
     public void updateWorkStationOtherCount() {
         Date startDate = new Date();
         log.info("涓嬬墖鐮存洿鏂版崯鐜荤拑鏁伴噺浠诲姟寮�濮嬫墽琛屾椂闂达細{}", startDate);
diff --git a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
index 6a17f8a..b210c53 100644
--- a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
+++ b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/job/Downpush.java
@@ -22,6 +22,8 @@
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.TaskScheduler;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
@@ -31,6 +33,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.*;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -47,6 +50,13 @@
     private DownGlassTaskService downGlassTaskService;
     @Autowired
     private GlassInfoService glassInfoService;
+    private final ExecutorService scanCodeThreadPool = new ThreadPoolExecutor(
+            10, // core pool size
+            20, // maximum pool size
+            60L, TimeUnit.SECONDS,
+            new LinkedBlockingQueue<>()
+    );
+    private final ExecutorService isRunThreadPool = Executors.newFixedThreadPool(5);
 
     @Value("${mes.scan.ip}")
     private String scanIp;
@@ -185,52 +195,55 @@
 
     @Scheduled(fixedDelay = 1000)
     public void isRun() {
-        JSONObject jsonObject = new JSONObject();
-        //姝e湪杩涜鐨勪换鍔�
-        List<DownWorkstation> downWorkstation = downWorkstationService.getoneDownWorkstations(1, 6);
-        jsonObject.append("downWorkstation", downWorkstation);
-        List<DownGlassTask> downGlassTask = downGlassTaskService.selectOutTaskCache();
-        jsonObject.append("downGlassTask", downGlassTask);
-        ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unLoadGlassIsRun");
-        if (sendwServer != null) {
-            for (WebSocketServer webserver : sendwServer) {
-                if (webserver != null) {
-                    webserver.sendMessage(jsonObject.toString());
-                } else {
-                    log.info("unLoadGlassIsRun is closed");
+        isRunThreadPool.execute(() -> {
+            JSONObject jsonObject = new JSONObject();
+            //姝e湪杩涜鐨勪换鍔�
+            List<DownWorkstation> downWorkstation = downWorkstationService.getoneDownWorkstations(1, 6);
+            jsonObject.append("downWorkstation", downWorkstation);
+            List<DownGlassTask> downGlassTask = downGlassTaskService.selectOutTaskCache();
+            jsonObject.append("downGlassTask", downGlassTask);
+            ArrayList<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unLoadGlassIsRun");
+            if (sendwServer != null) {
+                for (WebSocketServer webserver : sendwServer) {
+                    if (webserver != null) {
+                        webserver.sendMessage(jsonObject.toString());
+                    } else {
+                        log.info("unLoadGlassIsRun is closed");
+                    }
                 }
             }
-        }
+        });
+
     }
 
+    @Async
     @Scheduled(fixedDelay = Long.MAX_VALUE)
     public void scanCodeTask() {
+        JSONObject jsonObject = new JSONObject();
         log.info("鎵弿浠诲姟宸插惎鍔�");
         while (true) {
-            new Thread(() -> {
-                try (Socket socket = new Socket(scanIp, scanPort);
-                     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
-                    // 鎺ユ敹鏈嶅姟鍣ㄥ搷搴�
-                    String response;
-                    log.info("绛夊緟鎵爜涓�......");
-                    while ((response = in.readLine()) != null) {
-                        log.info("鎵弿鍒扮殑鐜荤拑id锛歿}", response);
-                        List<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("scanCode");
-                        if (CollectionUtils.isNotEmpty(sendwServer)) {
-                            //鎸夌収鐜荤拑id鑾峰彇鐜荤拑淇℃伅杩斿洖缁欏墠绔晫闈紝鍏蜂綋闇�瑕佸摢浜涙暟鎹緟纭
-                            GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>()
-                                    .eq(GlassInfo::getGlassId, response).last("limit 1"));
-                            if (null == glassInfo) {
-                                log.info("鎸夌収鐜荤拑id锛歿}锛屾棤娉曟壘鍒扮幓鐠冧俊鎭�", response);
-                            } else {
-                                sendwServer.get(0).sendMessage(glassInfo.toString());
-                            }
+            try (Socket socket = new Socket(scanIp, scanPort);
+                 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
+                // 鎺ユ敹鏈嶅姟鍣ㄥ搷搴�
+//                socket.setSoTimeout(1000);
+                log.info("绛夊緟鎵爜涓�......");
+                    String glassId = in.readLine();
+                    log.info("鎵弿鍒扮殑鐜荤拑id锛歿}", glassId);
+                    List<WebSocketServer> sendwServer = WebSocketServer.sessionMap.get("unloadglass");
+                    if (CollectionUtils.isNotEmpty(sendwServer)) {
+                        //鎸夌収鐜荤拑id鑾峰彇鐜荤拑淇℃伅杩斿洖缁欏墠绔晫闈紝鍏蜂綋闇�瑕佸摢浜涙暟鎹緟纭
+                        GlassInfo glassInfo = glassInfoService.getOne(new LambdaQueryWrapper<GlassInfo>()
+                                .eq(GlassInfo::getGlassId, glassId).last("limit 1"));
+                        if (null == glassInfo) {
+                            log.info("鎸夌収鐜荤拑id锛歿}锛屾棤娉曟壘鍒扮幓鐠冧俊鎭�", glassId);
+                        } else {
+                            jsonObject.append("scanGlass",glassInfo);
+                            sendwServer.get(0).sendMessage(jsonObject.toString());
                         }
                     }
-                } catch (Exception exception) {
-                    log.info("璇诲彇寮傚父锛屽師鍥犱负{}", exception.getMessage());
-                }
-            }).start();
+            } catch (Exception exception) {
+                log.info("璇诲彇寮傚父锛屽師鍥犱负{}", exception.getMessage());
+            }
         }
     }
 }
diff --git a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/resources/application.yml b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/resources/application.yml
index 95d42b1..8a593cc 100644
--- a/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/resources/application.yml
+++ b/hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/resources/application.yml
@@ -8,6 +8,11 @@
     name: unLoadGlass
   liquibase:
     enabled: false
+  task:
+    scheduling:
+      pool:
+        size: 10
+      thread-name-prefix: task-unLoad
 
 
 mybatis-plus:
@@ -24,5 +29,5 @@
   throughHeight: 2500
   threshold: 5      #涓嬬墖鐨勬渶澶ч槇鍊�
   scan:
-    ip: 127.0.0.1
+    ip: 169.254.171.199
     port: 5000

--
Gitblit v1.8.0