private MongoClient createMongoClient() { try { logger.debug("Testing Mongo socket address : " + dbHost + " / " + dbPort); Socket testingSocket = new Socket(dbHost, dbPort); testingSocket.close(); } catch (Exception e) { logger.warn( "Cannot connect to " + dbHost + " / " + dbPort + ", No MongoDB running or wrong configuration : " + e.getMessage()); return null; } try { logger.debug("Creating new mongo client"); MongoClient mongoClient = null; MongoClientOptions.Builder options = new MongoClientOptions.Builder(); options.connectTimeout(dbTimeOut); mongoClient = new MongoClient(new ServerAddress(dbHost, dbPort), options.build()); return mongoClient; } catch (UnknownHostException exception) { logger.error("Cannot create MongoDB Client " + exception.getStackTrace()); return null; } }
public MongoDB() { try { mongo = new MongoClient(HOST, PORT); db = mongo.getDB(DB_NAME); table = db.getCollection(COLLECTION_NAME); dataLoader = new MongoDBDataLoader(table); } catch (final UnknownHostException unknownHostException) { System.err.println(unknownHostException.getMessage()); System.err.println(unknownHostException.getStackTrace()); } catch (final MongoException mongoException) { System.err.println(mongoException.getMessage()); System.err.println(mongoException.getStackTrace()); } }