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);
}
}
}