修改以前寫的合同匯入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
- mysql匯入csv格式檔案MySql
- python 寫入CSV檔案Python
- csv檔案的寫入和讀取
- java匯出CSV檔案Java
- php百萬資料透過指令碼檔案寫入csvPHP指令碼
- PHP 匯出 CSV 格式檔案PHP
- oralce 匯出csv格式檔案
- 將csv檔案匯入到neo4j中
- Java實現CSV檔案的匯出Java
- java快速實現匯出生成csv檔案(含註釋程式碼)Java
- 如何把 .csv 的檔案匯入資料庫SQL SERVER 中!資料庫SQLServer
- 哪位有jsp匯入匯出xml檔案的程式碼JSXML
- SQL loader 匯入csv格式的表格檔案的一個例子SQL
- SQLite3 匯出 CSV 檔案SQLite
- 批次匯出csv檔案的基本嘗試
- 批量匯出csv檔案的基本嘗試
- 管理工具匯入CSV檔案,中文資料亂碼的解決辦法。
- 把csv檔案的資料匯入到oracle資料庫中Oracle資料庫
- 解決PHP匯出CSV檔案中文亂碼問題PHP
- 使用go語言對csv檔案進行解析處理,匯入匯出。Go
- PHP 匯出大資料 CSV 檔案PHP大資料
- Servlet實現匯出下載csv檔案Servlet
- C# 匯出DataTable至csv檔案C#
- 如何將 JSON, Text, XML, CSV 資料檔案匯入 MySQLJSONXMLMySql
- 用 PostgreSQL 的 COPY 匯入匯出 CSVSQL
- MySQL匯入匯出檔案檔案MySql
- 裝置CSV檔案匯入,型別轉換加try...catch...型別
- MySQL直接匯出CSV檔案,並解決中文亂碼的問題MySql
- 使用PHP原生匯出Excel和CSV檔案PHPExcel
- java程式碼實現excel檔案資料匯入JavaExcel
- ServiceNow如何匯出豎線分割的CSV檔案?
- C語言讀取寫入CSV檔案 [一]基礎篇C語言
- 從CSV檔案匯入資料到Analytics Cloud裡建立模型和StoryCloud模型
- neo4j匯入關係csv檔案建立知識圖譜
- Mysql匯入大表檔案時注意修改引數MySql