| | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | return ServerResponse.createBySuccess(map); |
| | | } |
| | | |
| | | public String doGet(String pid, EStockType eStockType) { |
| | | String apiUrl = eStockType.getStockUrl() + "/indices?country_id="+eStockType.getContryId()+"&key=" + eStockType.getStockKey(); |
| | | try { |
| | | URL url = new URL(apiUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("GET"); |
| | | |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| | | String inputLine; |
| | | StringBuffer response = new StringBuffer(); |
| | | |
| | | while ((inputLine = in.readLine()) != null) { |
| | | response.append(inputLine); |
| | | } |
| | | in.close(); |
| | | return response.toString(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public Map getSingleStock(String code) { |
| | | if (StringUtils.isBlank(code)) |
| | | return null; |