多线程导入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)

java 复制代码
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());

excel导入

java 复制代码
/**
* 不动产抵押导入
* @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;
}

LoanItemRiskSafeguardService业务类

java 复制代码
/**
* 导入任务
* 实现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);
   }
}
相关推荐
小义_2 小时前
JDK 深度解析
java·linux·开发语言·python·面试
奈斯先生Vector2 小时前
从“能调用”到“可运营”:AI Agent 进入多模型时代后的架构升级
java·javascript·数据库·人工智能·算法·架构·aigc
Json____2 小时前
宿舍安全卫生检查系统:Node 全栈开发实战
java·前端·数据库·毕业设计·课程设计·毕设·wwwoop.com
pnoker2 小时前
36 个驱动模块:应对协议碎片化
java·物联网·modbus·工业互联网·opc ua
码上有光2 小时前
Linux:进程控制和进程替换
java·linux·服务器·进程控制·进程替换
默辨2 小时前
我用Java后端的视角读了一遍JoyAgent-JDGenie
java·开发语言
SQL-First布道者2 小时前
⚡ Spring JDBC 完整体系 · 第 5 讲 · Spring Boot Starter JDBC
java·spring boot·spring·mybatis·spring jdbc
宋哥转AI3 小时前
深入理解 AI Agent · 多 Agent 编排 #03:Supervisor 与 Orchestrator——Dream-SaaS 双层编排架构拆解
java·人工智能·ai
木井巳3 小时前
【JavaEE】Spring Web MVC 入门
java·spring boot·spring·servlet·java-ee
2601_962128643 小时前
【JavaWeb06】Tomcat基础入门:架构理解与基本配置指南
java·架构·tomcat