修改以前寫的合同匯入CSV檔案程式碼

ZHOU_VIP發表於2018-10-17

老程式碼:

/**
 * 以非同步方式進行匯入合同.
 * 
 * @param excelFile
 * @param randomStr
 * @param extParaMap
 * @throws Exception
 */
private void addOsmContracts(String randomStr, List<OsmContract> deveuiList) throws Exception {
	logger.info("Begin process Contract: totalNum="+deveuiList.size());
	final User u = this.getLoginUser();
	ExecutorService execSvr = Executors.newSingleThreadExecutor();
	execSvr.submit(new Runnable(){
		@Override
		public void run() {
			try {
				StringBuffer errBuf = new StringBuffer();
				int totalNum = deveuiList.size();
				totalNum = (totalNum == 0) ? 1 : totalNum;					
				double stepVal = 100.0 / totalNum;
				int cc = 0;
				for(OsmContract osmContract : deveuiList){
					cc++; 
					try{
						if(osmContract != null){
							String contractid = osmContract.getContractid();
							if(contractid !=null && contractid.trim().equals("")){
								throw new Exception("合同編號為空,請檢查");
							}
							OsmContract contract = contractService.queryContractByContractid(contractid);
							if(contract != null){
								throw new Exception("合同編號重複:"+contractid);
							}
						}
						contractService.addContract(osmContract);
						setProgress(randomStr, (int)(cc*stepVal), true, "匯入合同資訊成功");
						logger.info("addOsmContracts OK: "+osmContract.getContractid());
					} catch(Exception e){
						errBuf.append(osmContract.getContractid()).append("匯入合同資訊失敗:"+e.getMessage()).append("; ");
						logger.error("addOsmContracts failed: "+osmContract.getContractid()+", err="+e.getMessage(), e);
						setProgress(randomStr, 100, false, e.getMessage());
					}
				}
			}catch(Exception ex){
				logger.error(ex.getMessage(), ex);
				setProgress(randomStr, 100, false, ex.getMessage());
			}catch(Throwable err){
				logger.error(err.getMessage(), err);
				setProgress(randomStr, 100, false, err.getMessage());
			}finally {
				// 停掉執行緒池.
				execSvr.shutdown();
				checkProgressMap(); 
			}
		}
	});		
}

以上程式碼有問題,寫反了啊

String contractid = osmContract.getContractid();
if(contractid == null || contractid.trim().equals("")){
	throw new Exception("合同編號為空,請檢查");
}

新程式碼:

/**
 * 以非同步方式進行匯入合同.
 * 
 * @param excelFile
 * @param randomStr
 * @param extParaMap
 * @throws Exception
 */
private void addOsmContracts(String randomStr, List<OsmContract> deveuiList) throws Exception {
	logger.info("Begin process Contract: totalNum=" + deveuiList.size());
	final User u = this.getLoginUser();
	ExecutorService execSvr = Executors.newSingleThreadExecutor();
	execSvr.submit(new Runnable() {
		@Override
		public void run() {
			try {
				StringBuffer errBuf = new StringBuffer();
				int totalNum = deveuiList.size();
				totalNum = (totalNum == 0) ? 1 : totalNum;
				double stepVal = 100.0 / totalNum;
				int cc = 0;
				for (OsmContract osmContract : deveuiList) {
					cc++;
					if (osmContract == null) {
						continue;
					}
					try {
						String contractid = osmContract.getContractid();
						if (contractid == null || contractid.trim().equals("")) {
							continue;
						}
						OsmContract contract = contractService.queryContractByContractid(contractid);
						if (contract != null) {
							//throw new Exception("合同編號重複:" + contractid);
							logger.info("合同編號重複:" + contractid);
						}
						contractService.addContract(osmContract);
						setProgress(randomStr, (int) (cc * stepVal), true, "匯入合同資訊成功");
						logger.info("addOsmContracts OK: " + osmContract.getContractid());
					} catch (Exception e) {
						errBuf.append(osmContract.getContractid()).append("匯入合同資訊失敗:" + e.getMessage())
								.append("; ");
						logger.error("addOsmContracts failed: " + osmContract.getContractid() + ", err="
								+ e.getMessage(), e);
						setProgress(randomStr, 100, false, e.getMessage());
					}
				}
			} catch (Exception ex) {
				logger.error(ex.getMessage(), ex);
				setProgress(randomStr, 100, false, ex.getMessage());
			} catch (Throwable err) {
				logger.error(err.getMessage(), err);
				setProgress(randomStr, 100, false, err.getMessage());
			} finally {
				// 停掉執行緒池.
				execSvr.shutdown();
				checkProgressMap();
			}
		}
	});
}

也不需要拋異常的,try...catch在for迴圈裡面,如果有合同編號重複的,也不會因為異常整個兒跳出,打個日誌就行了。

如果注掉這段程式碼,就不行了啊,一旦有異常就跳出,導致所有資料都匯入不了

/**
 * 以非同步方式進行匯入合同.
 * 
 * @param excelFile
 * @param randomStr
 * @param extParaMap
 * @throws Exception
 */
private void addOsmContracts(String randomStr, List<OsmContract> deveuiList) throws Exception {
	logger.info("Begin process Contract: totalNum=" + deveuiList.size());
	final User u = this.getLoginUser();
	ExecutorService execSvr = Executors.newSingleThreadExecutor();
	execSvr.submit(new Runnable() {
		@Override
		public void run() {
			try {
				StringBuffer errBuf = new StringBuffer();
				int totalNum = deveuiList.size();
				totalNum = (totalNum == 0) ? 1 : totalNum;
				double stepVal = 100.0 / totalNum;
				int cc = 0;
				for (OsmContract osmContract : deveuiList) {
					cc++;
					if (osmContract == null) {
						continue;
					}
					/*try {*/
						String contractid = osmContract.getContractid();
						if (contractid == null || contractid.trim().equals("")) {
							continue;
						}
						OsmContract contract = contractService.queryContractByContractid(contractid);
						if (contract != null) {
							throw new Exception("合同編號重複:" + contractid);
							//logger.info("合同編號重複:" + contractid);
						}
						contractService.addContract(osmContract);
						setProgress(randomStr, (int) (cc * stepVal), true, "匯入合同資訊成功");
						logger.info("addOsmContracts OK: " + osmContract.getContractid());
					/*} catch (Exception e) {
						errBuf.append(osmContract.getContractid()).append("匯入合同資訊失敗:" + e.getMessage())
								.append("; ");
						logger.error("addOsmContracts failed: " + osmContract.getContractid() + ", err="
								+ e.getMessage(), e);
						setProgress(randomStr, 100, false, e.getMessage());
					}*/
				}
			} catch (Exception ex) {
				logger.error(ex.getMessage(), ex);
				setProgress(randomStr, 100, false, ex.getMessage());
			} catch (Throwable err) {
				logger.error(err.getMessage(), err);
				setProgress(randomStr, 100, false, err.getMessage());
			} finally {
				// 停掉執行緒池.
				execSvr.shutdown();
				checkProgressMap();
			}
		}
	});
}

相關文章