@Override public void excelImportProgramAndDetail(String awardItemCode, InputStream excelInputStream, String fileName) { Preconditions.checkArgument(excelInputStream != null, "excel数据不能为空"); File configFile = new File(SalaryExcelConfig.CLASSPATH_EXCEL_PROPERTIES + SalaryExcelConfig.getPropertiesHolder().getString("cn.com.tisco.hr.salary.excel.json.cfg.awardDetail")); ExcelImporter excelImporter = ExcelImporter.excel(excelInputStream, fileName).forSheet(1); List<AwardProgramDetail> awardProgramDetailList = excelImporter.configSheet(configFile) .parseSheet() .validate() .getBeans(); // 当导入的单项奖明细数据在1000条以上时,采用多线程的方式分批导入数据。 if(awardProgramDetailList.size() <= 20000) { doImportProgramAndDetail(awardItemCode, excelImporter, awardProgramDetailList); } else { int threadCount = awardProgramDetailList.size() / 1000 + 1; ExecutorService executorService = Executors.newFixedThreadPool(threadCount); CompletionService<Boolean> completionService = new ExecutorCompletionService<>(executorService); List<Future<Boolean>> executionResults = new ArrayList<>(threadCount); for(int i = 0; i < awardProgramDetailList.size(); i++) { Future<Boolean> future = completionService.submit(makeProgramAndDetailImportThread( awardProgramDetailList.subList(i * 1000, i * 1000 + 1000), awardItemCode, excelImporter)); executionResults.add(future); } boolean successful = true; for(int i=0; i < executionResults.size(); i++) { try { Boolean result = completionService.take().get(); if(Boolean.FALSE.equals(result)) { successful = false; } } catch (Exception e) { logger.error(e.getMessage(), e); } } if(! successful) { throw new IllegalStateException("单项奖数据导入时发生错误。"); } } } private Callable<Boolean> makeProgramAndDetailImportThread( final List<AwardProgramDetail> awardProgramDetails, final String awardItemCode, final ExcelImporter excelImporter) { return new Callable<Boolean>() { @Override public Boolean call() throws Exception { try { doImportProgramAndDetail(awardItemCode, excelImporter, awardProgramDetails); return Boolean.TRUE; } catch (Exception e) { logger.error("导入单项奖[代码=" + awardItemCode + "]时发生异常。", e); excelImporter.getErrorBuilder().addSheetGlobalError(e.getMessage()); return Boolean.FALSE; } } }; }