1
lcq OP //获取全部数据
ResultBody<List<CouponCodeExchangeListVO>> resultBody = couponCodeFeignClientService.getListByRuleId(map.get("ruleId").toString()); List<CouponCodeExchangeListVO> records = resultBody.getData(); List<CouponCodeExchangeListExcelVO> couponCodeExchangeListExcelVOS = new ArrayList<>(); List<Future<List<CouponCodeExchangeListExcelVO>>> futures = new ArrayList<>(); if (CollectionUtil.isNotEmpty(records)) { int totalNum = records.size(); int batchNum; if (totalNum <= 1000){ batchNum = 200; }else { batchNum = 500; } int pageNum = totalNum % batchNum == 0 ? totalNum / batchNum : totalNum / batchNum + 1; List<CouponCodeExchangeListVO> subData; int toIndex, fromIndex; List<ExportCouponCodeTask> tasks = new ArrayList<>(); for (int i = 0; i < pageNum; i++) { fromIndex = i * batchNum; toIndex = Math.min(totalNum, fromIndex + batchNum); subData = records.subList(fromIndex, toIndex); tasks.add(new ExportCouponCodeTask(subData)); } //分批使用多线程处理 if(!exportCouponCodeThreadPoolExecutor.isShutdown()){ futures = exportCouponCodeThreadPoolExecutor.invokeAll(tasks); } } for (Future<List<CouponCodeExchangeListExcelVO>> future : futures) { List<CouponCodeExchangeListExcelVO> couponCodeExchangeListExcelVOS1 = future.get(); couponCodeExchangeListExcelVOS.addAll(couponCodeExchangeListExcelVOS1); } |
2
lcq OP List<CouponCodeExchangeListVO> list;
public ExportCouponCodeTask(List<CouponCodeExchangeListVO> list){ this.list = list; } @Override public List<CouponCodeExchangeListExcelVO> call() throws Exception { log.info("执行任务"); List<CouponCodeExchangeListExcelVO> couponCodeExchangeListExcelVOS = new ArrayList<>(); for (CouponCodeExchangeListVO couponCodeExchangeListVO : list) { CouponCodeExchangeListExcelVO couponCodeExchangeListExcelVO = new CouponCodeExchangeListExcelVO(); BeanUtils.copyProperties(couponCodeExchangeListVO, couponCodeExchangeListExcelVO); couponCodeExchangeListExcelVO.setByteArray(generateJpg(couponCodeExchangeListVO.getQrCodeUrl(), 500, 500)); if (StringUtils.isNotBlank(couponCodeExchangeListVO.getSuWxSubscribe())){ couponCodeExchangeListExcelVO.setSuWxSubscribe(couponCodeExchangeListVO.getSuWxSubscribe().split(":")[1]); } if (StringUtils.isNotBlank(couponCodeExchangeListVO.getEnterpriseWechat())){ couponCodeExchangeListExcelVO.setEnterpriseWechat(couponCodeExchangeListVO.getEnterpriseWechat().split(":")[1]); } couponCodeExchangeListExcelVOS.add(couponCodeExchangeListExcelVO); } return couponCodeExchangeListExcelVOS; } private static byte[] generateJpg(String content, int width, int height) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); generate(content, width, height, ImgUtil.IMAGE_TYPE_JPG, out); return out.toByteArray(); } |
3
wangyu17455 2021-12-13 16:36:37 +08:00 via Android
把线程池换成 computablefuture 试试?
|