背景
在上一篇博客中,将300个二维码进行导出,因为数据量大,所以导出时间长。
解决方案
将二维码数据分页使用多线程进行并行处理
上代码
@Override public String findAllUnexportedQrCode(String isExportQRCode ) { try { //查询出所有未导出二维码信息并将二维码的物品名称物品id放入list集合中(一个循环) List<NewPdfPositionPojo> pdfPositionPojoList= storePdfByItemInfo(isExportQRCode); //将list集合每40条取一页以键值对key为页,value为数据(两个两层循环) Map<Integer,List<NewPdfPositionPojo>> fortyMap=generatedEveryForty(pdfPositionPojoList); //导出生成pdf文件(两个循环) return storePdfFlie(fortyMap); }catch (Exception e){ log.error("批量导出二维码异常"+e); } return null; } private String storePdfFlie(Map<Integer,List<NewPdfPositionPojo>> integerListMap){ try { final CountDownLatch latch = new CountDownLatch(integerListMap.size()); ExecutorService excutorService = Executors.newFixedThreadPool(CommonConstants.EIGHT_INT); PdfDocument pdf = new PdfDocument(); Iterator entrieSecond = integerListMap.entrySet().iterator(); while (entrieSecond.hasNext()) { Map.Entry entrySecond = (Map.Entry) entrieSecond.next(); List<NewPdfPositionPojo> pdfPositionPojoList = (List<NewPdfPositionPojo>) entrySecond.getValue(); excutorService.submit(new PdfPage(pdfPositionPojoList,pdf,latch)); } try { latch.await(); }catch (InterruptedException e){ throw new RuntimeException("二维码进入页面多线程处理异常",e); } excutorService.shutdown(); long threadId=Thread.currentThread().getId(); pdf.saveToFile(CommonConstants.PDF_LINUX_PATH+CommonConstants.PDF_FILENAME+threadId+CommonConstants.PDF_POINT_TYPE); pdf.dispose(); return CommonConstants.PDF_FILENAME+threadId+CommonConstants.PDF_POINT_TYPE; }catch (Exception e){ log.error("导出二维码失败"+e); } return null; } private Map<Integer,List<NewPdfPositionPojo>> generatedEveryForty(List<NewPdfPositionPojo> pdfPositionPojoList){ try { //listSize为集合长度 int listSize = pdfPositionPojoList.size(); //每次取40条 int index = CommonConstants.FORTY; //用map存起来新的分组后数据 Map map = new HashMap(); int keyToken = CommonConstants.ZERO_INT; List<PdfPositionPojo> pdfPositionPojoListResult=storePosition(); for (int i = CommonConstants.ZERO_INT; i < pdfPositionPojoList.size(); i+=CommonConstants.FORTY) { //作用为Index最后没有1000条数据,则剩余的条数newList中就装几条 if (i + CommonConstants.FORTY > listSize) { index = listSize - i; } //使用subList方法,keyToken用来记录循环了多少次或者每个map数据的键值 List newList = pdfPositionPojoList.subList(i, i + index); List<NewPdfPositionPojo> newPdfPositionPojoList=new ArrayList<>(); for (int j = CommonConstants.ZERO_INT; j < newList.size(); j++) { NewPdfPositionPojo newPdfPositionPojo= (NewPdfPositionPojo)newList.get(j); newPdfPositionPojo.setLateralPosition(pdfPositionPojoListResult.get(j).getLateralPosition()); newPdfPositionPojo.setLongitudinalPosition(pdfPositionPojoListResult.get(j).getLongitudinalPosition()); newPdfPositionPojoList.add(newPdfPositionPojo); } //每取一次放到map集合里,然后 map.put(keyToken, newPdfPositionPojoList); keyToken++; } return map; }catch (Exception e){ log.error("批量拿出40条数据"+e); } return null; } /** * @Author:RenYaBing * @Description:获取未导出的二维码信息并赋值到list集合中 * @CreateTime: 2023/2/16 8:29 * @param: [isExportQRCode] * @return: java.util.List<com.tfjybj.itemqrcode.pojo.NewPdfPositionPojo> **/ private List<NewPdfPositionPojo> storePdfByItemInfo(String isExportQRCode ){ try { List<QrcodePojo> itemQRCodeEntityList =itemQRCodeMapper.findQRCode(isExportQRCode); List<NewPdfPositionPojo> newPdfPositionPojoArrayList =new ArrayList<>(); for (int i = CommonConstants.ZERO_INT; i < itemQRCodeEntityList.size(); i++) { String itemId=itemQRCodeEntityList.get(i).getItemId(); String itemName=itemQRCodeEntityList.get(i).getItemName(); NewPdfPositionPojo newPdfPositionPojo=new NewPdfPositionPojo(); newPdfPositionPojo.setItemId(itemId); newPdfPositionPojo.setItemName(itemName); newPdfPositionPojoArrayList.add(newPdfPositionPojo); } return newPdfPositionPojoArrayList; }catch (Exception e){ log.error("存储二维码信息异常"+e); } return null; } /** * @Author:RenYaBing * @Description:生成pdf二维码参数位置 * @CreateTime: 2023/2/16 8:25 * @param: [] * @return: java.util.List<com.tfjybj.itemqrcode.pojo.PdfPositionPojo> **/ private List<PdfPositionPojo> storePosition(){ try { List<PdfPositionPojo> pdfPositionPojoList=new ArrayList<>(); int i = CommonConstants.ZERO_INT; int j = CommonConstants.ZERO_INT; int count = CommonConstants.ZERO_INT; while (j < CommonConstants.EIGHTHUNDRED) { while (i <= CommonConstants.FOUR_HUNDRED_AND_FROUR && count < CommonConstants.FORTY) { PdfPositionPojo pdfPositionPojo=new PdfPositionPojo(); pdfPositionPojo.setLateralPosition(i); pdfPositionPojo.setLongitudinalPosition(j); pdfPositionPojoList.add(pdfPositionPojo); i += CommonConstants.HUNDRED_INT; count++; } j += CommonConstants.HUNDRED_INT; i = CommonConstants.ZERO_INT; } return pdfPositionPojoList; }catch (Exception e){ log.error("插入数值错误"+e); } return null; }
多线程使用类
package com.tfjybj.itemqrcode.service.multithreading; import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.PdfImage; import com.tfjybj.itemqrcode.pojo.NewPdfPositionPojo; import com.tfjybj.itemqrcode.service.file.GenerateCode; import com.tfjybj.utils.CommonConstants; import java.time.Duration; import java.time.LocalDateTime; import java.util.List; import java.util.concurrent.CountDownLatch; /** * @BelongsProject: ias-backend-5.0 * @BelongsPackage: com.tfjybj.itemqrcode.service.multithreading * @Author:RenYaBing * @Description: pdf文件实现 * @CreateTime: 2023-02-16 14:40 * @Version: 1.0 */ public class PdfPage implements Runnable { //pdf文件列表 private List<NewPdfPositionPojo> newPdfPositionPojoList; //pdf文件 private PdfDocument pdf; //锁存器 CountDownLatch latch; public PdfPage(List<NewPdfPositionPojo> newPdfPositionPojoList,PdfDocument pdf,CountDownLatch latch) { this.newPdfPositionPojoList = newPdfPositionPojoList; this.pdf = pdf; this.latch=latch; } /** * @Author:RenYaBing * @Description:执行无参多线程方法 * @CreateTime: 2023/2/16 20:15 * @param: [] * @return: void **/ @Override public void run() { try { this.GeneratePdfPages(); latch.countDown(); }catch (Exception e){ e.printStackTrace(); } } /** * @Author:RenYaBing * @Description:pdf生成一页方法 * @CreateTime: 2023/2/16 20:19 * @param: [] * @return: void **/ private void GeneratePdfPages() throws InterruptedException{ try { LocalDateTime beginTime = LocalDateTime.now(); //实例化生成二维码类 GenerateCode generateCode=new GenerateCode(); PdfPageBase page= pdf.getPages().add(); List<NewPdfPositionPojo> pdfPositionPojoList = newPdfPositionPojoList; for (int i = CommonConstants.ZERO_INT; i < pdfPositionPojoList.size(); i++) { int lateralPosition=pdfPositionPojoList.get(i).getLateralPosition(); int longitudinalPosition=pdfPositionPojoList.get(i).getLongitudinalPosition(); PdfImage pdfImage= generateCode.getSinglePdfList(pdfPositionPojoList.get(i).getItemId(),pdfPositionPojoList.get(i).getItemName()); page.getCanvas().drawImage(pdfImage,lateralPosition,longitudinalPosition); } Long opetime = Duration.between(beginTime,LocalDateTime.now()).toMillis(); System.out.println("单页二维码执行时间"+opetime); }catch (Exception e){ e.printStackTrace(); } } }
文章知识点与官方知识档案匹配,可进一步学习相关知识