diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java index e1aa9a000..68a0c904e 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/impl/AMapServiceImpl.java @@ -32,6 +32,7 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ScheduledExecutorService; +import java.util.stream.Collectors; /** * 高德地图Service @@ -74,7 +75,7 @@ public class AMapServiceImpl implements AMapService { @Override public List getStationInfos(GetStationInfoDTO dto) { StopWatch sw = new StopWatch(); - List resultList = new ArrayList<>(); + // List resultList = new ArrayList<>(); if (StringUtils.equals("page", dto.getType())) { int pageNo = dto.getCurrentPage() == null ? 1 : dto.getCurrentPage(); @@ -91,22 +92,23 @@ public class AMapServiceImpl implements AMapService { } // 拼装高德格式数据 sw.start("拼装高德格式数据"); - for (PileStationInfo stationInfo : stationInfos) { - CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> assembleAMapData(stationInfo), executor); + // for (PileStationInfo stationInfo : stationInfos) { + // + // // AMapStationInfo aMapStationInfo = assembleAMapData(stationInfo); + // // resultList.add(aMapStationInfo); + // } + List collect = stationInfos.parallelStream().map(x -> { try { - AMapStationInfo aMapStationInfo = completableFuture.get(); - resultList.add(aMapStationInfo); - } catch (InterruptedException e) { - throw new RuntimeException(e); + return assembleAMapData(x); } catch (ExecutionException e) { throw new RuntimeException(e); + } catch (InterruptedException e) { + throw new RuntimeException(e); } - // AMapStationInfo aMapStationInfo = assembleAMapData(stationInfo); - // resultList.add(aMapStationInfo); - } + }).collect(Collectors.toList()); sw.stop(); log.info("接口耗时:{}, 详情:{}", sw.getTotalTimeMillis(), sw.prettyPrint()); - return resultList; + return collect; } /** @@ -114,11 +116,10 @@ public class AMapServiceImpl implements AMapService { * @param stationInfo 站点信息 * @return 高度需要的数据格式 */ - private AMapStationInfo assembleAMapData(PileStationInfo stationInfo) { - StopWatch sw = new StopWatch(); - sw.start("set数据"); + private AMapStationInfo assembleAMapData(PileStationInfo stationInfo) throws ExecutionException, InterruptedException { AMapStationInfo aMapInfo = new AMapStationInfo(); - aMapInfo.setStationID(String.valueOf(stationInfo.getId())); + Long stationInfoId = stationInfo.getId(); + aMapInfo.setStationID(String.valueOf(stationInfoId)); aMapInfo.setOperatorID(""); aMapInfo.setEquipmentOwnerID(""); aMapInfo.setOperatorName(""); @@ -141,24 +142,30 @@ public class AMapServiceImpl implements AMapService { } aMapInfo.setConstruction(Integer.parseInt(construction)); aMapInfo.setBusineHours(stationInfo.getBusinessHours()); - sw.stop(); + // 根据站点id查询计费模板 - sw.start("根据站点id查询计费模板"); - List priceList = getPriceInfoByStationId(String.valueOf(stationInfo.getId())); - aMapInfo.setPriceChargingInfo(priceList); - sw.stop(); + // List priceList = getPriceInfoByStationId(String.valueOf(stationInfoId)); + CompletableFuture> future1 = CompletableFuture.supplyAsync(() -> getPriceInfoByStationId(String.valueOf(stationInfoId))); + // 根据站点id查询快、慢充设备数量 - sw.start("根据站点id查询快、慢充设备数量"); - Map pileNumMap = pileConnectorInfoService.getPileTypeNum(stationInfo.getId()); + // Map pileNumMap = pileConnectorInfoService.getPileTypeNum(stationInfoId); + CompletableFuture> future2 = CompletableFuture.supplyAsync(() -> pileConnectorInfoService.getPileTypeNum(stationInfoId)); + + // 根据站点查询站点下所有桩 + // List aMapEquipmentInfos = getPileListByStationId(String.valueOf(stationInfoId)); + CompletableFuture> future3 = CompletableFuture.supplyAsync(() -> getPileListByStationId(String.valueOf(stationInfoId))); + + CompletableFuture all = CompletableFuture.allOf(future1, future2, future3); + // .join()和.get()都会阻塞并获取线程的执行情况 + // .join()会抛出未经检查的异常,不会强制开发者处理异常 .get()会抛出检查异常,需要开发者处理 + all.join(); + all.get(); + aMapInfo.setPriceChargingInfo(future1.get()); + Map pileNumMap = future2.get(); aMapInfo.setFastEquipmentNum(pileNumMap.get("fastTotal")); aMapInfo.setSlowEquipmentNum(pileNumMap.get("slowTotal")); - sw.stop(); - // 根据站点查询站点下所有桩 - sw.start("根据站点查询站点下所有桩"); - List aMapEquipmentInfos = getPileListByStationId(String.valueOf(stationInfo.getId())); - aMapInfo.setEquipmentInfos(aMapEquipmentInfos); - sw.stop(); - log.info("组装高德数据格式耗时详细:{}, 线程名:{}", prettyPrintBySecond(sw), Thread.currentThread().getName()); + + aMapInfo.setEquipmentInfos(future3.get()); return aMapInfo; }