public static MongoClient getMongoInstance() {
if (_mongoClient == null) {
try {
MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
MongoClientOptions.Builder options = new MongoClientOptions.Builder();
_mongoClient = new MongoClient(serverAddress, credential, options.build());
} catch (Exception e) {
log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
log.info("Exception : ", e);
}
}
return _mongoClient;
}
=====================================================
public static MongoClient getMongoInstance(int timeOut) {
MongoClient myMongoClient = null;
if(timeOut==-1) {
return getMongoInstance();
}else {
try {
MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
MongoClientOptions.Builder options = new MongoClientOptions.Builder();
options.socketTimeout(timeOut).connectTimeout(timeOut).maxWaitTime(timeOut);
ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
myMongoClient = new MongoClient(serverAddress, Arrays.asList(credential),options.build());
} catch (Exception e) {
log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
log.info("Exception : ", e);
}
}
return myMongoClient;
}
No comments:
Post a Comment