1 /**
 2  * @param reqList
 3  * @param localUrl
 4  * @param type     0:摄像头抓拍, 
 5  * @return
 6  */
 7 public static List<String> capturePic(List<MdCameraInfoReq> reqList, String localUrl, Integer type) throws ExecutionException, InterruptedException {
 8     List<CompletableFuture<String>> capList = new ArrayList<>();
 9     List<String> list = new ArrayList<>();
10     if (CollectionUtils.isNotEmpty(reqList)) {
11         for (MdCameraInfoReq req : reqList) {
12             String picUrl = "";
13             if (Consts.INT_ZERO.equals(type)) {
14                 //开启线程
15                 CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
16                     return CapturePicUtils.capturePic(req.getIp(), req.getPort(), req.getUserName(), req.getPwd(), req.getChannel(), localUrl);
17                 }, executorFinishWeighImgService);
18                 //抓图线程list
19                 capList.add(future);
20             } else {
21                 picUrl = CapturePicUtils.captureNvrPic(req, localUrl);
22             }
23             if (StringUtils.isNotBlank(picUrl)) {
24                 list.add(picUrl);
25             }
26         }
27     }
28 
29     List<String> captures = new ArrayList<>();
30     CompletableFuture<Void> completableFuture = CompletableFuture.allOf(capList.toArray(new CompletableFuture[capList.size()]));
31     //阻塞,直到所有任务结束
32     completableFuture.join();
33     CompletableFuture<List<String>> captureList = completableFuture.thenApply(returnFuture -> {
34         capList.forEach(future -> {
35             try {
36                 if (Objects.nonNull(future.get(5, TimeUnit.SECONDS))) {
37                     captures.add(future.get(5, TimeUnit.SECONDS));
38                 }else {
39                     log.error("获取数据超时");
40                 }
41             } catch (InterruptedException e) {
42                 log.error("终端异常:{}", e);
43             } catch (ExecutionException e) {
44                 log.error("capturePic执行异常:{}", e);
45             } catch (TimeoutException e) {
46                 log.error("capturePic超时:{}", e);
47             }
48 
49         });
50         return captures;
51     });
52     List<String> baseList = captureList.get().stream().collect(Collectors.toList());
53     return baseList;
54 }