From 8ed60795a7c278f9699616f72eb05ce49800ba6f Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 07 Aug 2024 01:20:50 +0800
Subject: [PATCH] 1

---
 mexcClient/src/main/java/org/example/mexcclient/WsBean/MexcWsBean.java |   37 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 36 insertions(+), 1 deletions(-)

diff --git a/mexcClient/src/main/java/org/example/mexcclient/WsBean/MexcWsBean.java b/mexcClient/src/main/java/org/example/mexcclient/WsBean/MexcWsBean.java
index 3adeebf..f2f504e 100644
--- a/mexcClient/src/main/java/org/example/mexcclient/WsBean/MexcWsBean.java
+++ b/mexcClient/src/main/java/org/example/mexcclient/WsBean/MexcWsBean.java
@@ -14,6 +14,7 @@
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
+import org.example.mexcclient.MexcClientApplication;
 import org.example.mexcclient.pojo.Currency;
 import org.example.mexcclient.server.impl.CurrencySerivceImpl;
 import org.example.mexcclient.wsClient.MexcClient;
@@ -21,6 +22,8 @@
 import org.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.SpringApplication;
+import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@@ -45,6 +48,9 @@
     private CurrencySerivceImpl currencyService;
 
     @Autowired
+    private ConfigurableApplicationContext context;
+
+    @Autowired
     @Qualifier("threadPoolTaskExecutor")
     private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
@@ -62,10 +68,39 @@
                 List<Currency> sublist = mexc.subList(fromIndex, toIndex);
 
                 // 使用自定义线程池提交任务
-                threadPoolTaskExecutor.execute(new MexcClient(sublist)::start);
+                threadPoolTaskExecutor.execute(() -> {
+                    try {
+                        new MexcClient(sublist).start();
+                    } catch (Exception e) {
+                        run();
+                    }
+                });
             }
 
         }
     }
+
+    private boolean runExecuted = false;
+    private synchronized void run() {
+
+        if (runExecuted) {
+            return; // 已经执行过,直接返回
+        }
+        runExecuted = true;
+        log.info("ws 异常开始重启");
+        Thread restartThread = new Thread(() -> {
+            try {
+                SpringApplication.exit(context, () -> 0);
+                SpringApplication.run(MexcClientApplication.class);
+                log.info("ws 重启成功");
+            } catch (Exception e) {
+                e.printStackTrace();
+                log.error("ws 重启失败");
+            }
+        });
+        restartThread.setDaemon(false);
+        restartThread.start();
+        log.info("ws 重启失败");
+    }
 }
 

--
Gitblit v1.9.3