多线程调用外部接口
Posted rrz634171
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程调用外部接口相关的知识,希望对你有一定的参考价值。
private final ExecutorService executorService = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(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<CompletableFuture> shopBaseInfoFutures = batchQueryShopBaseInfoByVenderId(clientInfo, goodShopTagInfos);
private List<CompletableFuture> batchQueryShopBaseInfoByVenderId(com.jd.shop.findgoodshop.common.client.ClientInfo clientInfo, List<GoodShopTagInfo> goodShopTagInfos) com.jd.m.soa.shop.base.client.ClientInfo outClientInfo = convertShopBaseClientInfo(clientInfo); List<CompletableFuture> completableFutures = new ArrayList<>(); goodShopTagInfos.forEach( goodShopTagInfo -> // 启用多线程 CompletableFuture completableFuture = CompletableFuture.runAsync(() -> CallerInfo info = null; List<VenderAttributeEnum> 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<Integer,String> 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);
以上是关于多线程调用外部接口的主要内容,如果未能解决你的问题,请参考以下文章