1
zj
2024-08-07 8ed60795a7c278f9699616f72eb05ce49800ba6f
kucoinClient/src/main/java/org/example/kucoinclient/WsBean/KucoinWsBean.java
File was renamed from kucoinClient/src/main/java/org/example/kucoinclient/WsBean/MexcWsBean.java
@@ -1,8 +1,6 @@
package org.example.kucoinclient.WsBean;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -11,23 +9,22 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.example.kucoinclient.KucoinClientApplication;
import org.example.kucoinclient.pojo.Currency;
import org.example.kucoinclient.server.impl.CurrencySerivceImpl;
import org.example.kucoinclient.wsClient.KucoinClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @ClassDescription: 客户端请求类
@@ -36,10 +33,13 @@
 */
@Slf4j
@Configuration
public class MexcWsBean {
public class KucoinWsBean {
    @Autowired
    private CurrencySerivceImpl currencyService;
    @Autowired
    private ConfigurableApplicationContext context;
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
@@ -62,12 +62,41 @@
                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
                // 使用自定义线程池提交任务
                threadPoolTaskExecutor.execute(new KucoinClient(sublist,token)::start);
                threadPoolTaskExecutor.execute(() -> {
                    try {
                        new KucoinClient(sublist,token).start();
                    } catch (Exception e) {
                        run();
                    }
                });
            }
        }
    }
    private boolean runExecuted = false;
    private synchronized void run() {
        if (runExecuted) {
            return; // 已经执行过,直接返回
        }
        runExecuted = true;
        log.info("ws 异常开始重启");
        Thread restartThread = new Thread(() -> {
            try {
                SpringApplication.exit(context, () -> 0);
                SpringApplication.run(KucoinClientApplication.class);
                log.info("ws 重启成功");
            } catch (Exception e) {
                e.printStackTrace();
                log.error("ws 重启失败");
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
        log.info("ws 重启失败");
    }
    public static String doPost() throws Exception {
        String url = "https://api.kucoin.com/api/v1/bullet-public";
        HttpPost httpPost = new HttpPost(url);