V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lcq
V2EX  ›  Java

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

  •  
  •   lcq · 2021-12-13 16:25:44 +08:00 · 1335 次点击
    这是一个创建于 1077 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1.表里有 1W 条数据
    2.循环给里面的链接生成二维码
    3.导出到 execl


    注:已使用多线程跑,但还是很缓慢,接口响应直接到了 50s
    lcq
        1
    lcq  
    OP
       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
        2
    lcq  
    OP
       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
        3
    wangyu17455  
       2021-12-13 16:36:37 +08:00 via Android
    把线程池换成 computablefuture 试试?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   910 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:20 · PVG 04:20 · LAX 12:20 · JFK 15:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.