• 多线程导入excel


    设置线程池参数,创建线程池

    • corePoolSize要保留在池中的线程数,即使它们是空闲的,除非{@code - allowCoreThreadTimeOut}被设置
    • maximumPoolSize允许在池中的最大线程数
    • keepAliveTime当线程数大于核心时,这是多余的空闲线程将在终止前等待新任务的最大时间
    • unit {@code keepAliveTime}参数的时间单位
    • workQueue用于在执行之前保存任务的队列。这个队列将只保存由{@code execute}方法提交的{@code Runnable}任务
    • threadFactory是执行器创建新线程时使用的工厂
    • handler是执行被阻塞时使用的处理程序,因为线程边界和队列容量达到了

    public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

    private static int corePoolSize = 50;//初始大小
    private static int maximumPoolSize = 200; //最大值
    //使用线程池管理
    private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("RiskSafeguard-pool-%d").build();
    private static ExecutorService pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    excel导入

    /**
    * 不动产抵押导入
    * @param file
    * @param createBy
    * @return
    */
    public boolean importData(MultipartFile file, Long createBy, Long itemApplyId){
       // 创建内部类的实例对象
       LoanItemRiskSafeguardService.ImportDataTask importDataTask = new LoanItemRiskSafeguardService.ImportDataTask(file, createBy, itemApplyId);
       pool.execute(importDataTask);
       return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    LoanItemRiskSafeguardService业务类

    /**
    * 导入任务
    * 实现Runable接口的内部类,重写run()
    */
    public class ImportDataTask implements Runnable{
    
        MultipartFile file = null;
        Long createBy = null;
        Long itemApplyId = null;
        public ImportDataTask(MultipartFile file, Long createBy, Long itemApplyId){
            this.file = file;
            this.createBy = createBy;
            this.itemApplyId = itemApplyId;
        }
    
        @Override
        public void run() {
            doImportDataTask(file, createBy, itemApplyId);
        }
    }
    
    
    /**
    * excel字段匹配
    * @param file
    * @param createBy
    */
    public void doImportDataTask(MultipartFile file, Long createBy, Long itemApplyId){
    
       /**
        * excel数据的keys
        */
       try{
           String[] keys = new String[]{ // 读取excel后,每个列对应一个key
                   "riskSafeguardName",    // 登记权属人
                  // ....
           };
    
           // 导入数据
           List<Map<String, Object>> excelData = ImportUtil.importExcel(file, keys, 0); // 读取excel数据
           if (!excelData.isEmpty()) {
               Map<String, java.lang.Object> excelMap = null;
               int success = 0,failed = 0;
               for(int i=0;i<excelData.size();i++) {
                   excelMap = excelData.get(i);
    
                   // 设置担保措施对象值,插入数据库  --- start
                   LoanItemRiskSafeguard riskSafeguard = new LoanItemRiskSafeguard();
                   // 从excelMap中根据key获取到value,设置到对象中,后续存表
                   // 登记权属人
                   String riskSafeguardName = excelMap.get("riskSafeguardName")==null?"":excelMap.get("riskSafeguardName").toString();
                   riskSafeguard.setRiskSafeguardName(riskSafeguardName);
                   // ...
    			   
                   try {
                       if(failed<=i){
                           dao.insert("add",riskSafeguard);
                       }
                   }catch (Exception e){
                       failed=i+1;
                       e.printStackTrace();
                   }
                   if(failed  <= i){
                       success = i + 1;
                   }else{
                       Long riskSafeguardId = riskSafeguard.getRiskSafeguardId();
                       dao.delById(riskSafeguardId);
                   }
                   // 设置担保措施对象值,插入数据库  --- end
               }
           }
       } catch (BizException be) {
           logger.info("后台导入不动产抵押物excel失败", be);
       } catch (Exception e) {
           logger.error("后台导入不动产抵押物excel失败", e);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    手把手教你如何搭建性能测试环境
    Linux查看日志命令
    什么是漂亮排序算法:一顿操作很装逼,一看性能二点七
    中国机器人产业崛起,德国市场面临30%的份额挑战
    linux命令汇总
    Pandas数据结构
    设计模式(十三)----结构型模式之桥接模式
    SpringBoot集成Kafka+Kafka优化问题
    MatrixOne 实战系列回顾 | 建模与多租户
    ORB-SLAM3复现过程中遇到的问题及解决办法
  • 原文地址:https://blog.csdn.net/Ai_Answer_zr/article/details/136439899