Administrator
2025-12-15 9a8d276f6ea40d2016a420a37c617fc5cdadf526
fix(okxNewPrice): 优化 WebSocket 客户端销毁逻辑

- 使用 closeBlocking 方法确保 WebSocket 连接完全关闭
- 在销毁过程中添加日志记录以便调试和监控
- 处理中断异常以提高代码健壮性
- 确保共享线程池正确关闭
- 移除不必要的 reconnectScheduler 关闭操作注释
1 files modified
33 ■■■■■ changed files
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java 33 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
@@ -91,21 +91,50 @@
     * 销毁方法,在 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() {
        log.info("开始销毁OkxQuantWebSocketClient");
        // 设置关闭标志,避免重连
        if (sharedExecutor != null && !sharedExecutor.isShutdown()) {
            sharedExecutor.shutdown();
        }
        if (webSocketClient != null && webSocketClient.isOpen()) {
            try {
            subscribeAccountChannel(UNSUBSCRIBE);
            subscribePositionChannel(UNSUBSCRIBE);
            subscribeOrderInfoChannel(UNSUBSCRIBE);
            webSocketClient.close();
                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) {