老代码:
/**
* 以异步方式进行导入合同.
*
* @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();
}
}
});
}