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

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

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


    注:已使用多线程跑,但还是很缓慢,接口响应直接到了 50s
    lcq
        1
    lcq  
    OP
       298 天前
    //获取全部数据
    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
       298 天前
    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  
       298 天前 via Android
    把线程池换成 computablefuture 试试?
    关于   ·   帮助文档   ·   API   ·   FAQ   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2013 人在线   最高记录 5497   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 16:05 · PVG 00:05 · LAX 09:05 · JFK 12:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.