生成二位码后导出到 execl 缓慢,大佬有什么解决方法

2021-12-13 16:25:44 +08:00
 lcq
1.表里有 1W 条数据
2.循环给里面的链接生成二维码
3.导出到 execl


注:已使用多线程跑,但还是很缓慢,接口响应直接到了 50s
1336 次点击
所在节点    Java
3 条回复
lcq
2021-12-13 16:27:04 +08:00
//获取全部数据
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);
}
lcq
2021-12-13 16:27:39 +08:00
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();
}
wangyu17455
2021-12-13 16:36:37 +08:00
把线程池换成 computablefuture 试试?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/821904

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX