diff --git a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java index f60f56f42..56fe7eef4 100644 --- a/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java +++ b/jsowell-thirdparty/src/main/java/com/jsowell/thirdparty/amap/service/AMapService.java @@ -5,6 +5,7 @@ import com.jsowell.pile.dto.amap.GetStationInfoDTO; import com.jsowell.thirdparty.amap.domain.AMapStationInfo; import java.util.List; +import java.util.concurrent.ExecutionException; /** * 高德地图Service @@ -19,7 +20,7 @@ public interface AMapService { * @param dto * @return */ - List getStationInfos(GetStationInfoDTO dto); + List getStationInfos(GetStationInfoDTO dto) throws ExecutionException, InterruptedException; /** * 商家推送充电设备动态数据 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 f87e6d6e4..a68fc417a 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 @@ -29,9 +29,7 @@ import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.*; import java.util.stream.Collectors; /** @@ -73,7 +71,7 @@ public class AMapServiceImpl implements AMapService { * @return */ @Override - public List getStationInfos(GetStationInfoDTO dto) { + public List getStationInfos(GetStationInfoDTO dto) throws ExecutionException, InterruptedException { StopWatch sw = new StopWatch(); // List resultList = new ArrayList<>(); @@ -92,21 +90,27 @@ public class AMapServiceImpl implements AMapService { } // 拼装高德格式数据 sw.start("拼装高德格式数据"); - // for (PileStationInfo stationInfo : stationInfos) { - // - // // AMapStationInfo aMapStationInfo = assembleAMapData(stationInfo); - // // resultList.add(aMapStationInfo); - // } - List collect = stationInfos.parallelStream().map(x -> { - try { - return assembleAMapData(x); - } catch (ExecutionException | InterruptedException e) { - throw new RuntimeException(e); - } - }).collect(Collectors.toList()); + + // 使用自定义ForkJoinPool + List resultList = new ForkJoinPool(10).submit(() -> + stationInfos.parallelStream().map(x -> { + try { + return assembleAMapData(x); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + }).collect(Collectors.toList()) + ).fork().join(); + // List collect = stationInfos.parallelStream().map(x -> { + // try { + // return assembleAMapData(x); + // } catch (ExecutionException | InterruptedException e) { + // throw new RuntimeException(e); + // } + // }).collect(Collectors.toList()); sw.stop(); log.info("接口耗时:{}, 详情:{}", sw.getTotalTimeMillis(), sw.prettyPrint()); - return collect; + return resultList; } /**