1
zj
2024-10-11 5f97f550b3fb60ac2142d7ca3c78d6bd04f18b80
src/main/java/com/nq/service/impl/StockServiceImpl.java
@@ -19,6 +19,7 @@
import com.nq.pojo.*;
import com.nq.pojo.reponse.RPageInfo;
import com.nq.service.*;
import com.nq.utils.ConverterUtil;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.redis.RedisKeyUtil;
@@ -184,21 +185,14 @@
            stockListVOS.addAll(Objects.requireNonNull(StockApi.getStockReailTimes(stockList)));
        }
        // Create a list to hold Futures
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        // Use CompletableFuture to perform asynchronous processing for each stockListVO
        for (StockListVO stockListVO : stockListVOS) {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                stockListVO.setNowPrice(iPriceServices.getNowPrice(stockListVO.getCode(), stockType).toString());
                Map<String, Object> newStock = iPriceServices.getNewStock(stockListVO.getCode());
                stockListVO.setHcrateP(newStock.get("pcp").toString());
            });
            futures.add(future);
            Stock stock = new Stock();
            stock.setStockType(stockListVO.getStock_type());
            stock.setStockCode(stockListVO.getCode());
            StockRealTimeBean cacheRealTimeStock = RedisKeyUtil.getCacheRealTimeStock(stock);
            stockListVO.setNowPrice(cacheRealTimeStock.getLast());
            stockListVO.setHcrateP(cacheRealTimeStock.getPcp());
        }
        // Wait for all futures to complete
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        RPageInfo pageInfo = new RPageInfo();
        pageInfo.setList(stockListVOS);