package org.example.bitgetsclient.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.example.bitgetsclient.BitgetsClientApplication;
|
import org.example.bitgetsclient.pojo.Currency;
|
import org.example.bitgetsclient.server.impl.CurrencySerivceImpl;
|
import org.example.bitgetsclient.wsClient.BitgetClient;
|
import org.json.JSONException;
|
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.concurrent.atomic.AtomicInteger;
|
import java.util.stream.Collectors;
|
|
/**
|
* @ClassDescription: 客户端请求类
|
* @JdkVersion: 1.8
|
* @Created: 2023/8/31 16:13
|
*/
|
@Slf4j
|
@Configuration
|
public class BitgetsWsBean {
|
|
@Autowired
|
private CurrencySerivceImpl currencyService;
|
|
@Autowired
|
private ConfigurableApplicationContext context;
|
|
@Autowired
|
@Qualifier("threadPoolTaskExecutor")
|
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
@Bean
|
public void bitgetWebsocketRunClientMap() throws JSONException, JsonProcessingException {
|
List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "bitget"));
|
if (!CollectionUtils.isEmpty(mexc)) {
|
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); // 切分子列表
|
String parameter = getParameter(sublist); // 获取参数
|
threadPoolTaskExecutor.execute(() -> {
|
try {
|
new BitgetClient(parameter).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(BitgetsClientApplication.class);
|
log.info("ws 重启成功");
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("ws 重启失败");
|
}
|
});
|
restartThread.setDaemon(false);
|
restartThread.start();
|
log.info("ws 重启失败");
|
}
|
|
public String getParameter(List<Currency> list) throws JsonProcessingException, JSONException {
|
// 创建一个ObjectMapper实例
|
ObjectMapper mapper = new ObjectMapper();
|
List<String> symbolList = list.stream().map(Currency::getSymbol).collect(Collectors.toList());
|
// 使用Map构建JSON对象
|
Map<String, Object> jsonMap = new HashMap<>();
|
jsonMap.put("op", "subscribe");
|
List<Map<String, String>> mapList = new ArrayList<>();
|
symbolList.forEach(f->{
|
Map<String, String> argsMap = new HashMap<>();
|
argsMap.put("instType", "SPOT");
|
argsMap.put("channel", "books1");
|
argsMap.put("instId", f);
|
mapList.add(argsMap);
|
});
|
jsonMap.put("args", mapList);
|
|
// 将Map转换为JSON字符串
|
String jsonString = mapper.writeValueAsString(jsonMap);
|
return jsonString;
|
|
}
|
}
|