• 多线程调用外部接口


    private final ExecutorService executorService = new ThreadPoolExecutor(nThreads, nThreads,
            0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(200), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "InteractIndex-thread-pool-" + threadNumber.getAndIncrement());
        }
    }, new ThreadPoolExecutor.CallerRunsPolicy());

    CompletableFuture batchQueryShopRealTimeInfoFuture = CompletableFuture.runAsync(() -> {
        /**
         * 获取店铺实时信息(tab列表、直播状态、上新、店铺关注数)
         */
        batchQueryShopRealTimeInfo(clientInfo, goodShopTagInfos);
    }, executorService);
    
    CompletableFuture batchQueryShopNotRealTimeInfoFuture = CompletableFuture.runAsync(() -> {
        /**
         * 获取店铺非实时信息(回头客、X年老店、好店)
         */
        batchQueryShopNotRealTimeInfo(clientInfo, goodShopTagInfos);
    }, executorService);

    批量 
    List shopBaseInfoFutures = batchQueryShopBaseInfoByVenderId(clientInfo, goodShopTagInfos);
    

    private List batchQueryShopBaseInfoByVenderId(com.jd.shop.findgoodshop.common.client.ClientInfo clientInfo, List goodShopTagInfos) {
    
        com.jd.m.soa.shop.base.client.ClientInfo outClientInfo = convertShopBaseClientInfo(clientInfo);
        List completableFutures = new ArrayList<>();
        goodShopTagInfos.forEach(
                goodShopTagInfo ->{
                    // 启用多线程
                    CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
                        CallerInfo info = null;
                        List queryAttributes = buildShopTagAttributes();
                        try {
                           info = Profiler.registerInfo("com.jd.shop.findgoodshop.rpc.shop.ShopInfoRpcService.queryShopBaseInfoByVenderId", false, true);
    
                            if ("1".equals(duccFacade.getConfigValueOrDefault("out.log.print","0"))) {
                                logger.error("queryShopBaseInfoByVenderId verderId:"+goodShopTagInfo.getVenderId().toString()+
                                                " queryAttributes:"+JsonUtils.toJSONString(queryAttributes)+
                                                "clientInfo:"+JsonUtils.toJSONString(outClientInfo));
                            }
                            QueryShopInfoResult result = shopInfoRpcService.queryShopBaseInfoByVenderId(goodShopTagInfo.getVenderId(),queryAttributes,outClientInfo);
                            if ("1".equals(duccFacade.getConfigValueOrDefault("out.log.print","0"))) {
                                logger.error("getShopAttributesByVenderId res:{}", JsonUtils.toJSONString(result));
                            }
                            //店铺类型
                            Pair pair = parseShopTypeResult(result);
                            if(pair!=null){
                                goodShopTagInfo.setShopType(pair.getKey());
                                if(pair.getKey() == ShopTypeEnum.SHOP_STAR.getCode()){
                                    goodShopTagInfo.setShopStar(pair.getValue());
                                }
                            }
    
                        } catch (Exception e) {
                            Profiler.functionError(info);
                            //判断出错后的异常处理
                            logger.error("queryShopBaseInfoByVenderId verderId:"+goodShopTagInfo.getVenderId().toString()+
                                    " queryAttributes:"+JsonUtils.toJSONString(queryAttributes)+
                                    "clientInfo:"+JsonUtils.toJSONString(outClientInfo),e);
                        } finally {
                            Profiler.registerInfoEnd(info);
                        }
    
                    },executorService);
                    completableFutures.add(completableFuture);
                }
        );
        return completableFutures;
    
    }

    waitFutureWithCatchException(batchQueryShopRelationInfoFuture, 500);
    printTimeLog(begin,"batchQueryShopRelationInfoFuture");
    for(CompletableFuture completableFuture:shopBaseInfoFutures){
        waitFutureWithCatchException(completableFuture, 500);
    }

    private void waitFutureWithCatchException(CompletableFuture completableFuture, long timeout) {
        if (completableFuture != null) {
            try {
                completableFuture.get(timeout, TimeUnit.MILLISECONDS);
            } catch (Exception ee) {
                logger.error("CompletableFuture.get unexpected exception", ee);
            }
        }
    }
  • 相关阅读:
    Nginx-03- Nginx+Keepalived高可用集群和基本原理
    算法提升:图的最小生成树算法-克鲁斯卡尔(Kruskal)
    redis的安装、基础命令及常用数据结构
    多表查询和连接查询
    使用Vitis HLS创建属于自己的IP
    【密评】商用密码应用安全性评估从业人员考核题库(五)
    介绍一下js的节流与防抖?
    [山东科技大学OJ]2414 Problem G: 倒排字符串
    文心一言(ERNIE Bot)初体验
    iOS调用文件app(file.app)选择文件和下载
  • 原文地址:https://blog.csdn.net/rrz634171/article/details/126001116