代码示例
/** * 全国 / 区域数据统计: * 1. 新能源规模 * 2. 电网容量 * 3. 输配线路 * 4. 变电站 * 5. 工作场站 * * @param area area * @param user user * @return the object * @since 3.0.0 */ public StatisticsDataVO statistics(String area, CurrentUser user) { final StatisticsDataVO vo = StatisticsDataVO.builder().build(); // 1. 新能源规模 CompletableFuture<NewEnergyVO> newEnergyQuery = getNewEnergyFuture(vo, area, user); // 2. 电网容量 CompletableFuture<Double> gridCapacitQuery = getGridCapacitQueryFuture(vo, area, user); // 3. 输配线路 CompletableFuture<LineStatisticsResponse> lineQuery = getLineQueryFuture(vo, area, user); // 4. 变电站 CompletableFuture<JsonNode> substationQuery = getSubstationQueryFuture(vo, area, user); // 5. 工作场站 CompletableFuture<List<StationResponse>> stationQuery = getStationQueryFuture(vo, user); CompletableFuture<Void> allQuery = CompletableFuture.allOf(newEnergyQuery, gridCapacitQuery, lineQuery, substationQuery, stationQuery); CompletableFuture<StatisticsDataVO> future = allQuery.thenApply((result) -> { log.info("------------------ 全部查询都完成 ------------------ "); return vo; }).exceptionally((e) -> { log.error(e.getMessage(), e); return StatisticsDataVO.builder().build(); }); future.join(); return vo; }
private CompletableFuture<NewEnergyVO> getNewEnergyFuture(StatisticsDataVO vo, String area, CurrentUser user) { CompletableFuture<NewEnergyVO> newEnergyQuery = CompletableFuture.supplyAsync(() -> { NewEnergyVO newEnergyVO = NewEnergyVO.builder().build(); if (UserUtils.isChongqingUser(user)) { return newEnergyVO; } if (UserUtils.isZhejiangUser(user)) { // 如果是浙江油田 目前只获取 燃气发电 类型 final NewEnergyQuery query = NewEnergyQuery.builder().type(NewEnergyType.GAS_POWER).build(); return getNewEnergyVO(area, newEnergyVO, query); } final NewEnergyQuery query = NewEnergyQuery.builder().region(area).build(); return getNewEnergyVO(area, newEnergyVO, query); }); newEnergyQuery.thenAccept((result) -> { log.info(" 查询新能源规模结果: [{}]", result); vo.setNewEnergy(result); }).exceptionally((e) -> { log.error(" 查询新能源规模异常: {}", e.getMessage(), e); return null; }); return newEnergyQuery; }
使用文档参考:https://blog.csdn.net/zsx_xiaoxin/article/details/123898171
标签:newEnergyQuery,return,area,示例,vo,CompletableFuture,user,代码执行 From: https://www.cnblogs.com/guanxiaohe/p/17956108