0
回答
spring ThreadPoolTaskExecutor 多线程里使用注解问题
【腾讯云】校园拼团福利,1核2G服务器10元/月!>>>   

有一个需要,将excel导入时,由于我的架构使用的都是spring注解方式注入,excel里有多个sheet工作表,将excel导入到数据库里时由于太慢采用多线程,但是将@Resource注解时会报空异常,请高手帮帮忙,线程代码如下:

public class TaskExecutorExample {

	private class MessagePrinterTask implements Runnable {
		
		private Sheet sheet;
		private boolean importAddFlag;

		public MessagePrinterTask(Sheet sheet, boolean importAddFlag) {
			this.sheet = sheet;
			this.importAddFlag = importAddFlag;
		}

		public void run() {
			addSheetName(sheet, importAddFlag);
		}
		
		private void addSheetName(Sheet sheet, boolean importAddFlag) {
			SheetName sheetNameObj = sheetManage.querySheetBySheetName(sheet
					.getSheetName());
			if (sheetNameObj != null && !importAddFlag) {
				sheetManage.deleteSheetName(sheetNameObj.getId());
			}
			sheetNameObj = new SheetName();
			sheetNameObj.setSheetName(sheet.getSheetName());
			sheetManage.saveSheetName(sheetNameObj);

			addSheetColumn(sheet, sheetNameObj, importAddFlag);
		}

		private void addSheetColumn(Sheet sheet, SheetName sheetNameObj,
				boolean importAddFlag) {
			List<String> sheetColumnNameList = new ArrayList<String>();
			Row row = sheet.getRow(1);
			for (Cell cell : row) {
				sheetColumnNameList
						.add(ExcelUtil.getCellValue((HSSFCell) cell));
			}
			List<SheetColumn> sheetColumnList = sheetManage
					.querySheetColumnsBySheetName(sheetNameObj.getId());
			if (sheetColumnList != null && !importAddFlag) {
				sheetManage.batchDeleteSheetColumns(sheetColumnList);
			}
			sheetColumnList.clear();
			for (int i = 0; i < sheetColumnNameList.size(); i++) {
				SheetColumn sheetColumn = new SheetColumn();
				sheetColumn.setColumnName(sheetColumnNameList.get(i));
				sheetColumn.setSheetSequence(i + 1);
				sheetColumn.setSheetName(sheetNameObj);
				sheetColumnList.add(sheetColumn);
			}
			sheetManage.batchAddSheetColumns(sheetColumnList);

			addSheetColumnValue(sheet, sheetColumnList, importAddFlag);
		}

		private void addSheetColumnValue(Sheet sheet,
				List<SheetColumn> sheetColumnList, boolean importAddFlag) {
			if (!importAddFlag) {
				sheetManage.batchDeleteSheetColumnValues(sheetColumnList);
			}
			for (int i = 2; i <= sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i);
				List<SheetColumnValue> sheetColumnValueList = new ArrayList<SheetColumnValue>();
				String rowFlag = UUID.randomUUID().toString();
				for (int j = 0; j < row.getLastCellNum(); j++) {
					SheetColumnValue sheetColumnValue = new SheetColumnValue();
					sheetColumnValue.setColumnVal(ExcelUtil
							.getCellValue((HSSFCell) row.getCell(j)));
					sheetColumnValue.setRowFlag(rowFlag);
					sheetColumnValue.setSheetColumn(sheetColumnList.get(j));
					sheetColumnValueList.add(sheetColumnValue);
				}
				sheetManage.batchAddSheetColumnValues(sheetColumnValueList);
			}
		}
	}

	private TaskExecutor taskExecutor;
	private SheetManage sheetManage;

	public TaskExecutorExample(TaskExecutor taskExecutor, SheetManage sheetManage) {
		this.taskExecutor = taskExecutor;
		this.sheetManage = sheetManage;
	}

	public void printMessages(String importExcelFile) {
		try {
			InputStream inputStream = new FileInputStream(importExcelFile);
			Workbook workbook = WorkbookFactory.create(inputStream);
			for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
				Sheet sheet = workbook.getSheetAt(i);
				taskExecutor.execute(new MessagePrinterTask(sheet, false));
			}
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

举报
符朝辉
发帖于5年前 0回/4K+阅
顶部