package org.example.kucoinclient.WsBean;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
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.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.List;
|
|
/**
|
* @ClassDescription: 客户端请求类
|
* @JdkVersion: 1.8
|
* @Created: 2023/8/31 16:13
|
*/
|
@Slf4j
|
@Configuration
|
public class KucoinWsBean {
|
|
@Autowired
|
private CurrencySerivceImpl currencyService;
|
|
@Autowired
|
private ConfigurableApplicationContext context;
|
|
@Autowired
|
@Qualifier("threadPoolTaskExecutor")
|
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
@Bean
|
public void kucoinWebsocketRunClientMap() throws Exception {
|
List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "kucoin"));
|
if (!CollectionUtils.isEmpty(mexc)) {
|
String result = doPost();
|
JSONObject jsonObject = new JSONObject(result);
|
String token = jsonObject.getJSONObject("data").getString("token");
|
int batchSize = 100; // 每个线程处理的数据量
|
int totalSize = mexc.size();
|
int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
|
|
for (int i = 0; i < threadCount; i++) {
|
int fromIndex = i * batchSize;
|
int toIndex = Math.min(fromIndex + batchSize, totalSize);
|
List<Currency> sublist = mexc.subList(fromIndex, toIndex);
|
|
// 使用自定义线程池提交任务
|
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);
|
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
|
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
|
HttpResponse response = defaultHttpClient.execute(httpPost);
|
HttpEntity respEntity = response.getEntity();
|
String text = EntityUtils.toString(respEntity, "UTF-8");
|
defaultHttpClient.getConnectionManager().shutdown();
|
return text;
|
}
|
}
|