1
zj
2024-08-15 8d53c609a6589158d68b5945cdb3061460a43d47
1
5 files modified
82 ■■■■ changed files
websocketSerivce/src/main/java/org/example/server/impl/CurrencySerivceImpl.java 22 ●●●● patch | view | raw | blame | history
websocketSerivce/src/main/java/org/example/task/BitgetStock.java 13 ●●●●● patch | view | raw | blame | history
websocketSerivce/src/main/java/org/example/task/GateStock.java 11 ●●●● patch | view | raw | blame | history
websocketSerivce/src/main/java/org/example/task/KucoinStock.java 14 ●●●● patch | view | raw | blame | history
websocketSerivce/src/main/java/org/example/task/MexcStock.java 22 ●●●● patch | view | raw | blame | history
websocketSerivce/src/main/java/org/example/server/impl/CurrencySerivceImpl.java
@@ -61,15 +61,24 @@
        ExecutorService executor = Executors.newFixedThreadPool(20); // 适当增加线程数以匹配处理的交易所数量
        // 异步处理每个交易所的数据
        CompletableFuture<Void> mexcFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("mexc"), mexcList, "mexc"), executor);
        CompletableFuture<Void> gateFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("gate"), gateList, "gate"), executor);
        CompletableFuture<Void> bitgetFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("bitget"), bitgetList, "bitget"), executor);
        CompletableFuture<Void> kucoinFuture = CompletableFuture.runAsync(() -> processMarketData(RedisUtil.keys("kucoin"), kucoinList, "kucoin"), executor);
        CompletableFuture<Void> mexcFuture = checkAndProcess("mexc", mexcList, executor);
        CompletableFuture<Void> gateFuture = checkAndProcess("gate", gateList, executor);
        CompletableFuture<Void> bitgetFuture = checkAndProcess("bitget", bitgetList, executor);
        CompletableFuture<Void> kucoinFuture = checkAndProcess("kucoin", kucoinList, executor);
        // 等待所有任务完成
        CompletableFuture.allOf(mexcFuture, gateFuture, bitgetFuture, kucoinFuture).join();
        executor.shutdown(); // 关闭线程池
    }
    private CompletableFuture<Void> checkAndProcess(String exchangeName, List<MarketBo> marketList, ExecutorService executor) {
        return CompletableFuture.runAsync(() -> {
            Set<String> keys = RedisUtil.keys(exchangeName);
            if (keys != null && !keys.isEmpty()) {
                processMarketData(keys, marketList, exchangeName);
            }
        }, executor);
    }
    private void processMexc() {
@@ -281,7 +290,10 @@
    public void quotationCalculation(){
        extracted();
        findProfitablePairs(mexcList, gateList, bitgetList, kucoinList); // 请确保这些变量有定义和赋值
        // 检查列表是否为空并调用 findProfitablePairs 方法
        if (!mexcList.isEmpty() || !gateList.isEmpty() || !bitgetList.isEmpty() || !kucoinList.isEmpty()) {
            findProfitablePairs(mexcList, gateList, bitgetList, kucoinList);
        }
    }
    public void scheduler(){
websocketSerivce/src/main/java/org/example/task/BitgetStock.java
@@ -59,7 +59,7 @@
    /**
     * 同步bitget交易所交易对
     */
    @Scheduled(cron = "0 0/45 * * * ?")
    @Scheduled(cron = "0 0/30 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
@@ -91,7 +91,10 @@
            getList.parallelStream().forEach(person -> person.setSymbol(StringUtils.remove(person.getSymbol(), "-")));
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"bitget"));
            //  删除已经下架的币种
            Set<String> symbolSet = getList.stream().map(CurrencyBitgetBo::getSymbol).collect(Collectors.toSet());
            List<Currency> removeList = dbList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
@@ -100,13 +103,14 @@
            if(CollectionUtils.isNotEmpty(removeList)){
                removeList.forEach(f->{
                    RedisUtil.delete("bitget"+f.getSymbol());
                    currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSymbol,f.getSymbol()));
                });
            }
            //  获取数据库中已有的symbol列表
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"bitget"));
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<Currency> saveList = getList.stream()
                    .filter(currency -> !loclSymbolSet.contains(currency.getSymbol()))
                    .map(currency -> {
                        Currency newCurrency = new Currency();
                        newCurrency.setSymbol(currency.getSymbol());
@@ -116,6 +120,7 @@
                        return newCurrency;
                    })
                    .collect(Collectors.toList());
            //  批量保存新增数据到数据库
            if (CollectionUtils.isNotEmpty(saveList)) {
                currencyService.saveBatch(saveList);
websocketSerivce/src/main/java/org/example/task/GateStock.java
@@ -47,7 +47,7 @@
    /**
     * 同步gate交易所交易对
     */
    @Scheduled(cron = "0 0/40 * * * ?")
    @Scheduled(cron = "0 0/35 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
@@ -77,7 +77,9 @@
            getList.parallelStream().forEach(person -> person.setId(StringUtils.remove(person.getId(), "_")));
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "gate"));
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            //  删除已经下架的币种
            Set<String> symbolSet = getList.stream().map(CurrencyGateBo::getId).collect(Collectors.toSet());
            List<Currency> removeList = dbList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
                    .collect(Collectors.toList());
@@ -85,14 +87,16 @@
            if(CollectionUtils.isNotEmpty(removeList)){
                removeList.forEach(f->{
                    RedisUtil.delete("gate"+f.getSymbol());
                    currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSymbol,f.getSymbol()));
                });
            }
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"gate"));
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<Currency> saveList = getList.stream()
                    .filter(currency -> !loclSymbolSet.contains(currency.getId()))
                    .map(currency -> {
                        Currency newCurrency = new Currency();
                        newCurrency.setSymbol(currency.getId());
@@ -102,6 +106,7 @@
                        return newCurrency;
                    })
                    .collect(Collectors.toList());
            //  批量保存新增数据到数据库
            if(CollectionUtils.isNotEmpty(saveList)){
                currencyService.saveBatch(saveList);
websocketSerivce/src/main/java/org/example/task/KucoinStock.java
@@ -11,6 +11,7 @@
import org.apache.commons.lang.StringUtils;
import org.example.common.MarketDataClient;
import org.example.pojo.Currency;
import org.example.pojo.bo.CurrencyGateBo;
import org.example.pojo.bo.CurrencyKucoin;
import org.example.server.impl.CurrencySerivceImpl;
import org.example.util.ConverterUtil;
@@ -43,7 +44,7 @@
    /**
     * 同步kucoin交易所交易对
     */
    @Scheduled(cron = "0 0/35 * * * ?")
    @Scheduled(cron = "0 0/40 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
@@ -74,8 +75,11 @@
            }.getType());
            getList.parallelStream().forEach(person -> person.setSymbol(StringUtils.remove(person.getSymbol(), "-")));
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "kucoin"));
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            //  删除已经下架的币种
            Set<String> symbolSet = getList.stream().map(CurrencyKucoin::getSymbol).collect(Collectors.toSet());
            List<Currency> removeList = dbList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
                    .collect(Collectors.toList());
@@ -83,13 +87,17 @@
            if(CollectionUtils.isNotEmpty(removeList)){
                removeList.forEach(f->{
                    RedisUtil.delete("kucoin"+f.getSymbol());
                    currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSymbol,f.getSymbol()));
                });
            }
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"kucoin"));
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<Currency> saveList = getList.stream()
                    .filter(currency -> !loclSymbolSet.contains(currency.getSymbol()))
                    .map(currency -> {
                        Currency newCurrency = new Currency();
                        newCurrency.setSymbol(currency.getSymbol());
websocketSerivce/src/main/java/org/example/task/MexcStock.java
@@ -11,6 +11,7 @@
import org.example.common.MexcMarketDataClient;
import org.example.pojo.Currency;
import org.example.pojo.ExchangeInfo;
import org.example.pojo.bo.CurrencyKucoin;
import org.example.pojo.bo.CurrencyMexcBo;
import org.example.server.impl.CurrencySerivceImpl;
import org.example.util.ConverterUtil;
@@ -41,7 +42,7 @@
    /**
     * 同步mexc交易所交易对
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    @Scheduled(cron = "0 0/45 * * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    public void syncCurrency() {
        //  使用Lock来确保同步
@@ -75,8 +76,12 @@
            Gson gson = new Gson();
            List<CurrencyMexcBo> getList = gson.fromJson(symbolsJson, new TypeToken<List<CurrencyMexcBo>>() {
            }.getType());
            //  获取数据库中已有的symbol列表
            List<Currency> dbList = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "mexc"));
            Set<String> symbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            //  删除已经下架的币种
            Set<String> symbolSet = getList.stream().map(CurrencyMexcBo::getSymbol).collect(Collectors.toSet());
            List<Currency> removeList = dbList.stream()
                    .filter(currency -> !symbolSet.contains(currency.getSymbol()))
                    .collect(Collectors.toList());
@@ -84,10 +89,19 @@
            if(CollectionUtils.isNotEmpty(removeList)){
                removeList.forEach(f->{
                    RedisUtil.delete("mexc"+f.getSymbol());
                    currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSymbol,f.getSymbol()));
                });
            }
            currencyService.remove(new LambdaQueryWrapper<Currency>().eq(Currency::getSource,"mexc"));
            List<Currency> currencies = ConverterUtil.convertToList(getList, Currency.class);
            //  比对接口返回的数据和数据库中已有的数据,找出新增的数据
            Set<String> loclSymbolSet = dbList.stream().map(Currency::getSymbol).collect(Collectors.toSet());
            List<CurrencyMexcBo> saveList = getList.stream()
                    .filter(CurrencyMexcBo -> !loclSymbolSet.contains(CurrencyMexcBo.getSymbol()))
                    .collect(Collectors.toList());
            List<Currency> currencies = ConverterUtil.convertToList(saveList, Currency.class);
            //  批量保存新增数据到数据库
            if(CollectionUtils.isNotEmpty(currencies)){
                currencyService.saveBatch(currencies);