From 69feff48c5b26b01d3d1849d476bc8cd6071b4cd Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 10 Dec 2025 17:33:34 +0800
Subject: [PATCH] feat(okx): 调整交易订单事件处理逻辑
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java | 45 ++++++++++++++-------------------------------
1 files changed, 14 insertions(+), 31 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 6a0183f..0e380bc 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
@@ -1,11 +1,13 @@
package com.xcong.excoin.modules.okxNewPrice;
+import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.modules.okxNewPrice.celue.CaoZuoService;
import com.xcong.excoin.modules.okxNewPrice.okxWs.*;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
+import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.ExchangeInfoEnum;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
import com.xcong.excoin.modules.okxNewPrice.utils.SSLConfig;
import com.xcong.excoin.modules.okxNewPrice.wangge.WangGeService;
@@ -49,7 +51,6 @@
private static final String WS_URL_MONIPAN = "wss://wspap.okx.com:8443/ws/v5/private";
private static final String WS_URL_SHIPAN = "wss://ws.okx.com:8443/ws/v5/private";
- private static final boolean INTERNET = false;
/**
* 订阅频道指令
@@ -122,7 +123,7 @@
SSLConfig.configureSSL();
System.setProperty("https.protocols", "TLSv1.2,TLSv1.3");
String WS_URL = WS_URL_MONIPAN;
- if (INTERNET){
+ if (ExchangeInfoEnum.OKX_UAT.isAccountType()){
WS_URL = WS_URL_SHIPAN;
}
URI uri = new URI(WS_URL);
@@ -130,8 +131,13 @@
@Override
public void onOpen(ServerHandshake handshake) {
log.info("OKX account-order WebSocket连接成功");
- resetHeartbeatTimer();
- websocketLogin();
+ // 检查应用是否正在关闭
+ if (!sharedExecutor.isShutdown()) {
+ resetHeartbeatTimer();
+ websocketLogin();
+ } else {
+ log.warn("应用正在关闭,忽略WebSocket连接成功回调");
+ }
}
@Override
@@ -262,35 +268,11 @@
AccountWs.handleEvent(response, redisUtils);
} else if (PositionsWs.POSITIONSWS_CHANNEL.equals(channel)) {
PositionsWs.handleEvent(response, redisUtils);
-
- String avgPxKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":avgPx";
- String avgPx = (String) redisUtils.get(avgPxKey);
- if (StrUtil.isBlank(avgPx)) {
- log.error("未获取到持仓均价");
- TradeOrderWs.orderEvent(webSocketClient, redisUtils, OrderParamEnums.INIT.getValue());
- return;
- }
-
- String uplKey = PositionsWs.POSITIONSWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":upl";
- String totalOrderUsdtKey = AccountWs.ACCOUNTWS_CHANNEL + ":" + CoinEnums.USDT.getCode() + ":totalOrderUsdt";
-
- String upl = (String) redisUtils.get(uplKey);
- String totalOrderUsdt = (String) redisUtils.get(totalOrderUsdtKey);
- BigDecimal multiply = new BigDecimal(upl).multiply(new BigDecimal("-1"));
-
- if (new BigDecimal(totalOrderUsdt).compareTo(multiply) < 0) {
- log.error("持仓盈亏超过下单总保证金,止损冷静一天......");
- TradeOrderWs.orderEvent(webSocketClient, redisUtils, OrderParamEnums.OUT.getValue());
- return;
- }
-
- String side = caoZuoService.caoZuo();
- if (StrUtil.isNotBlank(avgPx)) {
- TradeOrderWs.orderEvent(webSocketClient, redisUtils, side);
- }
} else if (BalanceAndPositionWs.CHANNEL_NAME.equals(channel)) {
BalanceAndPositionWs.handleEvent(response);
}
+ String side = caoZuoService.caoZuo();
+ TradeOrderWs.orderEvent(webSocketClient, redisUtils, side);
}
/**
@@ -318,7 +300,8 @@
private void resetHeartbeatTimer() {
cancelPongTimeout();
- if (heartbeatExecutor != null) {
+ // 检查线程池状态,避免在关闭过程中提交任务
+ if (heartbeatExecutor != null && !heartbeatExecutor.isShutdown()) {
pongTimeoutFuture = heartbeatExecutor.schedule(this::checkHeartbeatTimeout,
HEARTBEAT_TIMEOUT, TimeUnit.SECONDS);
}
--
Gitblit v1.9.1