optimize code

This commit is contained in:
怀谷 2022-07-22 16:47:28 +08:00
parent fac8aa0a7c
commit b7390ab288
2 changed files with 16 additions and 6 deletions

View File

@ -7,15 +7,24 @@ import com.alibaba.datax.common.spi.ErrorCode;
*/ */
public enum MongoDBReaderErrorCode implements ErrorCode { public enum MongoDBReaderErrorCode implements ErrorCode {
/**
* 参数不合法
*/
ILLEGAL_VALUE("ILLEGAL_PARAMETER_VALUE","参数不合法"), ILLEGAL_VALUE("ILLEGAL_PARAMETER_VALUE","参数不合法"),
/**
* 不合法的Mongo地址
*/
ILLEGAL_ADDRESS("ILLEGAL_ADDRESS","不合法的Mongo地址"), ILLEGAL_ADDRESS("ILLEGAL_ADDRESS","不合法的Mongo地址"),
UNEXCEPT_EXCEPTION("UNEXCEPT_EXCEPTION","未知异常"); /**
* 未知异常
*/
UNEXPECTED_EXCEPTION("UNEXPECTED_EXCEPTION","未知异常");
private final String code; private final String code;
private final String description; private final String description;
private MongoDBReaderErrorCode(String code,String description) { MongoDBReaderErrorCode(String code,String description) {
this.code = code; this.code = code;
this.description = description; this.description = description;
} }

View File

@ -3,6 +3,7 @@ package com.alibaba.datax.plugin.reader.mongodbreader.util;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import com.alibaba.datax.common.exception.DataXException; import com.alibaba.datax.common.exception.DataXException;
@ -33,7 +34,7 @@ public class MongoUtil {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_VALUE,"不合法参数"); throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_VALUE,"不合法参数");
} catch (Exception e) { } catch (Exception e) {
throw DataXException.asDataXException(MongoDBReaderErrorCode.UNEXCEPT_EXCEPTION,"未知异常"); throw DataXException.asDataXException(MongoDBReaderErrorCode.UNEXPECTED_EXCEPTION,"未知异常");
} }
} }
@ -45,14 +46,14 @@ public class MongoUtil {
} }
try { try {
MongoCredential credential = MongoCredential.createCredential(userName, database, password.toCharArray()); MongoCredential credential = MongoCredential.createCredential(userName, database, password.toCharArray());
return new MongoClient(parseServerAddress(addressList), Arrays.asList(credential)); return new MongoClient(parseServerAddress(addressList), Collections.singletonList(credential));
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_ADDRESS,"不合法的地址"); throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_ADDRESS,"不合法的地址");
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_VALUE,"不合法参数"); throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_VALUE,"不合法参数");
} catch (Exception e) { } catch (Exception e) {
throw DataXException.asDataXException(MongoDBReaderErrorCode.UNEXCEPT_EXCEPTION,"未知异常"); throw DataXException.asDataXException(MongoDBReaderErrorCode.UNEXPECTED_EXCEPTION,"未知异常");
} }
} }
/** /**
@ -79,7 +80,7 @@ public class MongoUtil {
for(Object address : rawAddressList) { for(Object address : rawAddressList) {
String[] tempAddress = ((String)address).split(":"); String[] tempAddress = ((String)address).split(":");
try { try {
ServerAddress sa = new ServerAddress(tempAddress[0],Integer.valueOf(tempAddress[1])); ServerAddress sa = new ServerAddress(tempAddress[0],Integer.parseInt(tempAddress[1]));
addressList.add(sa); addressList.add(sa);
} catch (Exception e) { } catch (Exception e) {
throw new UnknownHostException(); throw new UnknownHostException();