package org.example.WsBean; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.extern.slf4j.Slf4j; import org.example.pojo.Currency; import org.example.server.impl.CurrencySerivceImpl; import org.example.wsClient.MexcClient; import org.springframework.beans.factory.annotation.Autowired; 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.List; /** * @ClassDescription: 客户端请求类 * @JdkVersion: 1.8 * @Created: 2023/8/31 16:13 */ @Slf4j @Configuration public class MexcWsBean { @Autowired private CurrencySerivceImpl currencyService; @Autowired private ThreadPoolTaskExecutor threadPoolTaskExecutor; @Bean public void mexcWebsocketRunClientMap() { List mexc = currencyService.list(new LambdaQueryWrapper().eq(Currency::getSource, "mexc")); if (!CollectionUtils.isEmpty(mexc)) { int batchSize = 30; // 每个线程处理的数据量 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 sublist = mexc.subList(fromIndex, toIndex); // 使用自定义线程池提交任务 threadPoolTaskExecutor.execute(new MexcClient(sublist)::start); } } } }