修改以前寫的合同匯入CSV檔案程式碼
老程式碼:
/**
* 以非同步方式進行匯入合同.
*
* @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();
}
}
});
}
相關文章
- Mysql匯入csv檔案MySql
- office for Mac Excel匯入csv檔案亂碼MacExcel
- SQLServer匯入大CSV檔案SQLServer
- python 寫入CSV檔案Python
- csv檔案的寫入和讀取
- java匯出CSV檔案Java
- php百萬資料透過指令碼檔案寫入csvPHP指令碼
- PHP 匯出 CSV 格式檔案PHP
- java快速實現匯出生成csv檔案(含註釋程式碼)Java
- 將csv檔案匯入到neo4j中
- SQLite3 匯出 CSV 檔案SQLite
- 管理工具匯入CSV檔案,中文資料亂碼的解決辦法。
- 解決PHP匯出CSV檔案中文亂碼問題PHP
- 使用go語言對csv檔案進行解析處理,匯入匯出。Go
- PHP 匯出大資料 CSV 檔案PHP大資料
- ServiceNow如何匯出豎線分割的CSV檔案?
- java程式碼實現excel檔案資料匯入JavaExcel
- MySQL直接匯出CSV檔案,並解決中文亂碼的問題MySql
- 裝置CSV檔案匯入,型別轉換加try...catch...型別
- 從CSV檔案匯入資料到Analytics Cloud裡建立模型和StoryCloud模型
- 使用PHP原生匯出Excel和CSV檔案PHPExcel
- Python:Python中文寫入csv檔案出現亂碼問題的解決方案之一Python
- C語言讀取寫入CSV檔案 [一]基礎篇C語言
- neo4j匯入關係csv檔案建立知識圖譜
- 匯入sql檔案出現亂碼SQL
- linux 下MySQL命令列匯出csv檔案格式LinuxMySql命令列
- Excel/CSV 匯入匯出庫,支援大檔案,樣式設定,單元格合併Excel
- Colab pydrive 匯入匯出csv(pandas)
- 匯入excel檔案Excel
- Python-檔案的匯入Python
- 基於 java 註解的 csv 檔案讀寫框架Java框架
- win10 如何修改csv分隔符_win10怎麼修改csv檔案分隔符Win10
- EasyExcel完成excel檔案的匯入匯出Excel
- PHP匯出大量資料,儲存為CSV檔案PHP
- (十一)Electron 匯入匯出檔案
- 修改hosts檔案的小程式
- 【輪子】EasyExcel,簡易版匯入、匯入 Excel、CSVExcel
- Mysql匯入本地檔案MySql