Administrator
5 days ago 8d84a90a076676dfeb80b1882029895d28e9b238
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.xcong.excoin.modules.gateApi;
 
import com.xcong.excoin.modules.okxNewPrice.OkxKlineWebSocketClient;
import com.xcong.excoin.modules.okxNewPrice.OkxQuantWebSocketClient;
import com.xcong.excoin.modules.okxNewPrice.celue.CaoZuoService;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.ExchangeInfoEnum;
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;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 管理多个OKX WebSocket客户端实例,每个实例对应一个账号
 */
@Slf4j
@Component
public class GateWebSocketClientManager {
    @Autowired
    private CaoZuoService caoZuoService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private WangGeListService wangGeListService;
 
    private GateKlineWebSocketClient klinePriceClient;
 
 
    /**
     * 初始化方法,在Spring Bean构造完成后执行
     * 创建并初始化所有账号的WebSocket客户端实例
     */
    @PostConstruct
    public void init() {
        log.info("开始初始化OkxWebSocketClientManager");
        
        // 初始化价格WebSocket客户端
        try {
            klinePriceClient = new GateKlineWebSocketClient(caoZuoService, this, wangGeListService);
            klinePriceClient.init();
            log.info("已初始化OkxNewPriceWebSocketClient");
        } catch (Exception e) {
            log.error("初始化OkxNewPriceWebSocketClient失败", e);
        }
        
    }
 
    /**
     * 销毁方法,在Spring Bean销毁前执行
     * 关闭所有WebSocket客户端连接和相关资源
     */
    @PreDestroy
    public void destroy() {
        log.info("开始销毁OkxWebSocketClientManager");
        
        // 关闭价格WebSocket客户端
        if (klinePriceClient != null) {
            try {
                klinePriceClient.destroy();
                log.info("已销毁OkxNewPriceWebSocketClient");
            } catch (Exception e) {
                log.error("销毁OkxNewPriceWebSocketClient失败", e);
            }
        }
        
        log.info("OkxWebSocketClientManager销毁完成");
    }
 
    /**
     * 获取OkxNewPriceWebSocketClient实例
     * @return 价格WebSocket客户端实例
     */
    public GateKlineWebSocketClient getKlineWebSocketClient() {
        return klinePriceClient;
    }
}