1
zj
2024-08-07 8ed60795a7c278f9699616f72eb05ce49800ba6f
kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java
@@ -6,9 +6,16 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j;
import org.example.kucoinclient.KucoinClientApplication;
import org.example.kucoinclient.comm.ApplicationContextProvider;
import org.example.kucoinclient.pojo.Currency;
import org.example.kucoinclient.util.RedisUtil;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import java.io.UnsupportedEncodingException;
@@ -55,7 +62,7 @@
        this.executorService = Executors.newScheduledThreadPool(1); // 创建调度线程池
    }
    public void start() {
    public void start() throws Exception {
        try {
            connect(); // 连接到 WebSocket 服务器
            if (session == null) {
@@ -71,7 +78,8 @@
            }
        } catch (Exception e) {
            log.error("kucoin ws 连接过程中发生异常: ", e); // 捕获并记录异常
            log.error("kucoin ws 连接过程中发生异常:" + e.getMessage(), e); // 记录连接过程中发生的异常
           throw e;
        } finally {
            executorService.shutdown(); // 关闭调度线程池
        }
@@ -144,34 +152,34 @@
            ObjectMapper mapper = new ObjectMapper(); // 创建 ObjectMapper 实例
            // 空值检查,避免存储 null 值到 Redis
            if (resultMap.get("bids") != null) {
                Object bidsObj = resultMap.get("bids");
                if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) {
                    List<String> bidsList = (List<String>) bidsObj;
                    HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                    pvMap.put("p", bidsList.get(0));
                    pvMap.put("V", bidsList.get(1));
                    hashMap.put("bids",pvMap);
                }
            }
            if (resultMap.get("asks") != null) {
            if (resultMap.get("bids") != null && resultMap.get("asks") != null) {
                Object asksObj = resultMap.get("bids");
                Object bidsObj = resultMap.get("asks");
                Object asksObj = resultMap.get("asks");
                if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) {
                    List<String> asksList = (List<String>) asksObj;
                    HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                    pvMap.put("p", asksList.get(0));
                    pvMap.put("V", asksList.get(1));
                    hashMap.put("asks",pvMap);
                }
            }
                if(bidsObj instanceof List && !((List<?>) bidsObj).isEmpty() && asksObj instanceof List && !((List<?>) asksObj).isEmpty()){
                    if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) {
                        List<String> bidsList = (List<String>) bidsObj;
                        HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                        pvMap.put("p", new BigDecimal(String.valueOf(bidsList.get(0))).toPlainString());
                        pvMap.put("v", new BigDecimal(String.valueOf(bidsList.get(1))).toPlainString());
                        hashMap.put("bids",pvMap);
                    }
            String symbol = extractSymbolFromTopic(topic); // 从 topic 提取符号
            String key = PREFIX + symbol; // 创建 Redis 缓存键
            try {
                RedisUtil.set(key, mapper.writeValueAsString(hashMap)); // 存储到 Redis
            } catch (JsonProcessingException e) {
                log.error("将数据存入 Redis 时出错: {}", e.getMessage()); // 输出数据存储错误日志
                    if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) {
                        List<String> asksList = (List<String>) asksObj;
                        HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
                        pvMap.put("p", new BigDecimal(String.valueOf(asksList.get(0))).toPlainString());
                        pvMap.put("v", new BigDecimal(String.valueOf(asksList.get(1))).toPlainString());
                        hashMap.put("asks",pvMap);
                    }
                    String symbol = extractSymbolFromTopic(topic); // 从 topic 提取符号
                    String key = PREFIX + symbol; // 创建 Redis 缓存键
                    try {
                        RedisUtil.set(key, mapper.writeValueAsString(hashMap)); // 存储到 Redis
                    } catch (JsonProcessingException e) {
                        log.error("将数据存入 Redis 时出错: {}", e.getMessage()); // 输出数据存储错误日志
                    }
                }
            }
        } else {
            log.error("topic--->存入redis失败"); // 输出处理失败日志
@@ -189,46 +197,46 @@
    }
    @OnClose
    public void onClose() {
    public void onClose() throws Exception {
        log.info("kucoin ws 连接已关闭,尝试重新连接..."); // 输出连接关闭日志
        handleConnectionClosedOrError(); // 处理连接关闭或错误
        throw new Exception();
    }
    @OnError
    public void onError(Throwable throwable) {
    public void onError(Throwable throwable) throws Exception {
        log.error("kucoin ws 发生错误: ", throwable); // 输出错误日志
        handleConnectionClosedOrError(); // 处理连接关闭或错误
        throw new Exception();
    }
    private void handleConnectionClosedOrError() {
        synchronized (lock) { // 同步块,确保线程安全
            if (!reconnecting) {
                reconnecting = true; // 状态标记为重连中
                executorService.execute(this::attemptReconnect); // 执行重连操作
            }
        }
    }
    private void attemptReconnect() {
        try {
            log.info("kucoin ws 开始重连"); // 输出重连开始日志
            connect(); // 尝试重新连接
            log.info("kucoin ws 重连成功"); // 输出重连成功日志
        } catch (Exception e) {
            log.error("kucoin ws 重连失败: ", e); // 输出重连失败日志
        } finally {
            synchronized (lock) { // 同步块
                reconnecting = false; // 状态标记为未重连
                scheduleReconnect(); // 重新调度重连任务
            }
        }
    }
    private void scheduleReconnect() {
        if (!executorService.isShutdown()) { // 检查线程池是否已经关闭
            executorService.schedule(this::attemptReconnect, RECONNECT_DELAY, TimeUnit.SECONDS); // 调度重连
        }
    }
//    private void handleConnectionClosedOrError() {
//        synchronized (lock) { // 同步块,确保线程安全
//            if (!reconnecting) {
//                reconnecting = true; // 状态标记为重连中
//                executorService.execute(this::attemptReconnect); // 执行重连操作
//            }
//        }
//    }
//
//    private void attemptReconnect() {
//        try {
//            log.info("kucoin ws 开始重连"); // 输出重连开始日志
//            connect(); // 尝试重新连接
//            log.info("kucoin ws 重连成功"); // 输出重连成功日志
//        } catch (Exception e) {
//            log.error("kucoin ws 重连失败: ", e); // 输出重连失败日志
//        } finally {
//            synchronized (lock) { // 同步块
//                reconnecting = false; // 状态标记为未重连
//                scheduleReconnect(); // 重新调度重连任务
//            }
//        }
//    }
//
//    private void scheduleReconnect() {
//        if (!executorService.isShutdown()) { // 检查线程池是否已经关闭
//            executorService.schedule(this::attemptReconnect, RECONNECT_DELAY, TimeUnit.SECONDS); // 调度重连
//        }
//    }
    private void sendPing() {
        try {