private List<CiDto> getAllCmdbDataByCode(String code) {
int pageSize = 2000;
List<CiDto> res = new ArrayList<>();
//1.先查询10个,获取total,
//2.根据total和每页要查询数量pagesize相除,获得要执行多少次循环查询
CISearchDto ciSearchDto = new CISearchDto();
ciSearchDto.setBussinessGroupId(1);
ciSearchDto.setPageSize(10);
Result<PageInfo<CiDto>> list = cmdbService.listCI(code, ciSearchDto);
long tolal = Optional.ofNullable(list).map(Result::getRes).map(PageSerializable::getTotal).orElse(0l);
List<CompletableFuture<List<CiDto>>> futures = new ArrayList<>();
for (int i = 1; i < tolal / pageSize + 1; i++) {
int finalI = i;
log.info("第" + finalI + "次!");
CompletableFuture<List<CiDto>> future = CompletableFuture.supplyAsync(() -> {
CISearchDto ciSearchDtoTemp = new CISearchDto();
ciSearchDtoTemp.setBussinessGroupId(1);
ciSearchDtoTemp.setPageSize(pageSize);
ciSearchDtoTemp.setPageNum(finalI);
Result<PageInfo<CiDto>> listTemp = cmdbService.listCI(code, ciSearchDtoTemp);
List<CiDto> ciDtosList = Optional.ofNullable(listTemp).map(Result::getRes).map(PageInfo::getList).orElse(Lists.newArrayList());
return ciDtosList;
}, executor);
futures.add(future);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
try {
futures.stream().map(CompletableFuture::join).forEach(e1 -> {
res.addAll(e1);
});
} catch (Exception e) {
log.error("sync cmdb data to cache error", e);
}
return res;
}
CompletableFuture应用
最新推荐文章于 2024-07-26 07:00:00 发布