Administrator
7 days ago b70f32814aa9dc23ad284b43e91bbc6c96c70366
src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxWebSocketClientManager.java
@@ -2,7 +2,7 @@
import com.xcong.excoin.modules.okxNewPrice.celue.CaoZuoService;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.ExchangeInfoEnum;
import com.xcong.excoin.modules.okxNewPrice.wangge.WangGeService;
import com.xcong.excoin.modules.okxNewPrice.okxWs.wanggeList.WangGeListService;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,14 +22,18 @@
@ConditionalOnProperty(prefix = "app", name = "quant", havingValue = "true")
public class OkxWebSocketClientManager {
    @Autowired
    private WangGeService wangGeService;
    @Autowired
    private CaoZuoService caoZuoService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private WangGeListService wangGeListService;
    // 存储所有WebSocket客户端实例,key为账号类型名称
    private final Map<String, OkxQuantWebSocketClient> clientMap = new ConcurrentHashMap<>();
    // 存储所有OkxQuantWebSocketClient实例,key为账号类型名称
    private final Map<String, OkxQuantWebSocketClient> quantClientMap = new ConcurrentHashMap<>();
    // 存储OkxNewPriceWebSocketClient实例
    private OkxKlineWebSocketClient klinePriceClient;
    /**
     * 初始化方法,在Spring Bean构造完成后执行
@@ -39,14 +43,23 @@
    public void init() {
        log.info("开始初始化OkxWebSocketClientManager");
        
        // 初始化价格WebSocket客户端
        try {
            klinePriceClient = new OkxKlineWebSocketClient(redisUtils, caoZuoService, this, wangGeListService);
            klinePriceClient.init();
            log.info("已初始化OkxNewPriceWebSocketClient");
        } catch (Exception e) {
            log.error("初始化OkxNewPriceWebSocketClient失败", e);
        }
        // 获取所有ExchangeInfoEnum枚举值
        ExchangeInfoEnum[] accounts = ExchangeInfoEnum.values();
        
        // 为每个账号创建一个WebSocket客户端实例
        for (ExchangeInfoEnum account : accounts) {
            try {
                OkxQuantWebSocketClient client = new OkxQuantWebSocketClient(account, wangGeService, caoZuoService, redisUtils);
                clientMap.put(account.name(), client);
                OkxQuantWebSocketClient client = new OkxQuantWebSocketClient(account, caoZuoService, redisUtils);
                quantClientMap.put(account.name(), client);
                client.init();
                log.info("已初始化账号 {} 的WebSocket客户端", account.name());
            } catch (Exception e) {
@@ -65,8 +78,18 @@
    public void destroy() {
        log.info("开始销毁OkxWebSocketClientManager");
        
        // 关闭所有客户端实例
        for (Map.Entry<String, OkxQuantWebSocketClient> entry : clientMap.entrySet()) {
        // 关闭价格WebSocket客户端
        if (klinePriceClient != null) {
            try {
                klinePriceClient.destroy();
                log.info("已销毁OkxNewPriceWebSocketClient");
            } catch (Exception e) {
                log.error("销毁OkxNewPriceWebSocketClient失败", e);
            }
        }
        // 关闭所有量化交易WebSocket客户端实例
        for (Map.Entry<String, OkxQuantWebSocketClient> entry : quantClientMap.entrySet()) {
            try {
                OkxQuantWebSocketClient client = entry.getValue();
                client.destroy();
@@ -77,25 +100,33 @@
        }
        
        // 清空客户端映射
        clientMap.clear();
        quantClientMap.clear();
        
        log.info("OkxWebSocketClientManager销毁完成");
    }
    /**
     * 获取指定账号的WebSocket客户端实例
     * 获取指定账号的OkxQuantWebSocketClient实例
     * @param accountName 账号类型名称
     * @return WebSocket客户端实例
     */
    public OkxQuantWebSocketClient getClient(String accountName) {
        return clientMap.get(accountName);
        return quantClientMap.get(accountName);
    }
    /**
     * 获取所有WebSocket客户端实例
     * 获取所有OkxQuantWebSocketClient实例
     * @return 所有客户端实例的集合
     */
    public Collection<OkxQuantWebSocketClient> getAllClients() {
        return clientMap.values();
        return quantClientMap.values();
    }
    /**
     * 获取OkxNewPriceWebSocketClient实例
     * @return 价格WebSocket客户端实例
     */
    public OkxKlineWebSocketClient getKlineWebSocketClient() {
        return klinePriceClient;
    }
}