From 9a8d276f6ea40d2016a420a37c617fc5cdadf526 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 15 Dec 2025 11:23:29 +0800
Subject: [PATCH] fix(okxNewPrice): 优化 WebSocket 客户端销毁逻辑

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
index ad7b32e..70069ef 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
@@ -68,37 +68,73 @@
         return t;
     });
 
+    // 在 OkxQuantWebSocketClient 中添加初始化标记
+    private final AtomicBoolean isInitialized = new AtomicBoolean(false);
+
     /**
      * 初始化方法,在 Spring Bean 构造完成后执行。
      * 负责建立 WebSocket 连接并启动心跳检测任务。
      */
     @PostConstruct
     public void init() {
+        // 防止重复初始化
+        if (!isInitialized.compareAndSet(false, true)) {
+            log.warn("OkxQuantWebSocketClient 已经初始化过,跳过重复初始化");
+            return;
+        }
+
         connect();
         startHeartbeat();
-        
-        // 移除了每小时重连的定时任务
     }
 
     /**
      * 销毁方法,在 Spring Bean 销毁前执行。
      * 关闭 WebSocket 连接、停止心跳定时器及相关的线程资源。
      */
+//    @PreDestroy
+//    public void destroy() {
+//        if (webSocketClient != null && webSocketClient.isOpen()) {
+//            subscribeAccountChannel(UNSUBSCRIBE);
+//            subscribePositionChannel(UNSUBSCRIBE);
+//            subscribeOrderInfoChannel(UNSUBSCRIBE);
+//            webSocketClient.close();
+//        }
+//        shutdownExecutorGracefully(heartbeatExecutor);
+//        if (pongTimeoutFuture != null) {
+//            pongTimeoutFuture.cancel(true);
+//        }
+//        shutdownExecutorGracefully(sharedExecutor);
+//
+//        // 移除了 reconnectScheduler 的关闭操作
+//    }
     @PreDestroy
     public void destroy() {
-        if (webSocketClient != null && webSocketClient.isOpen()) {
-            subscribeAccountChannel(UNSUBSCRIBE);
-            subscribePositionChannel(UNSUBSCRIBE);
-            subscribeOrderInfoChannel(UNSUBSCRIBE);
-            webSocketClient.close();
+        log.info("开始销毁OkxQuantWebSocketClient");
+
+        // 设置关闭标志,避免重连
+        if (sharedExecutor != null && !sharedExecutor.isShutdown()) {
+            sharedExecutor.shutdown();
         }
+
+        if (webSocketClient != null && webSocketClient.isOpen()) {
+            try {
+                subscribeAccountChannel(UNSUBSCRIBE);
+                subscribePositionChannel(UNSUBSCRIBE);
+                subscribeOrderInfoChannel(UNSUBSCRIBE);
+                webSocketClient.closeBlocking();
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                log.warn("关闭WebSocket连接时被中断");
+            }
+        }
+
         shutdownExecutorGracefully(heartbeatExecutor);
         if (pongTimeoutFuture != null) {
             pongTimeoutFuture.cancel(true);
         }
         shutdownExecutorGracefully(sharedExecutor);
 
-        // 移除了 reconnectScheduler 的关闭操作
+        log.info("OkxQuantWebSocketClient销毁完成");
     }
 
     private void shutdownExecutorGracefully(ExecutorService executor) {

--
Gitblit v1.9.1