/** * Returns the hostname of the used server reported by MongoDB. * * @return the reported hostname can also be an IP address. * @throws UnknownHostException */ @ManagedOperation(description = "Server host name") public String getHostName() throws UnknownHostException { /* * UnknownHostException is not necessary anymore, but clients could have * called this method in a try..catch(UnknownHostException) already */ return getMongoClient().getAddress().getHost(); }
try { primaryClient.executeBlocking("conn", mongoClient -> { ServerAddress currentPrimary = mongoClient.getAddress(); address.set(currentPrimary); });
/** * Use the given primary to read the oplog. * * @param primary the connection to the replica set's primary node; may not be null */ protected void readOplog(MongoClient primary) { BsonTimestamp oplogStart = source.lastOffsetTimestamp(replicaSet.replicaSetName()); logger.info("Reading oplog for '{}' primary {} starting at {}", replicaSet, primary.getAddress(), oplogStart); // Include none of the cluster-internal operations and only those events since the previous timestamp ... MongoCollection<Document> oplog = primary.getDatabase("local").getCollection("oplog.rs"); Bson filter = Filters.and(Filters.gt("ts", oplogStart), // start just after our last position Filters.exists("fromMigrate", false)); // skip internal movements across shards FindIterable<Document> results = oplog.find(filter) .sort(new Document("$natural", 1)) // force forwards collection scan .oplogReplay(true) // tells Mongo to not rely on indexes .cursorType(CursorType.TailableAwait); // tail and await new data // Read as much of the oplog as we can ... ServerAddress primaryAddress = primary.getAddress(); try (MongoCursor<Document> cursor = results.iterator()) { while (running.get() && cursor.hasNext()) { if (!handleOplogEvent(primaryAddress, cursor.next())) { // Something happened, and we're supposed to stop reading return; } } } }
/** * Returns the hostname of the used server reported by MongoDB. * * @return the reported hostname can also be an IP address. * @throws UnknownHostException */ @ManagedOperation(description = "Server host name") public String getHostName() throws UnknownHostException { /* * UnknownHostException is not necessary anymore, but clients could have * called this method in a try..catch(UnknownHostException) already */ return getMongoClient().getAddress().getHost(); }
MongoClient mongoClient = new MongoClient("localhost", 27017); ServerAddress serverAdress = mongoClient.getAddress()
Builder o = MongoClientOptions.builder().connectTimeout(3000); MongoClient mongo = new MongoClient(new ServerAddress("192.168.0.1", 3001), o.build()); try { mongo.getAddress(); } catch (Exception e) { System.out.println("Mongo is down"); mongo.close(); return; }
/** * @return 'true' if working, 'false' if not * */ public boolean testMongoDb() { String connectString = LGProperties.get("database.mongo.hostname").toString() +":"+ LGProperties.get("database.mongo.port").toString(); try { MongoClient mongoClient = new MongoClient(connectString); mongoClient.getAddress(); mongoClient.close(); } catch (Exception e) { return false; } return true; }
@Override public void connect(String host, Integer port) { MongoConnection.log.info("Connecting to MongoDB at {}:{}", host, port); try { this.mongo = new MongoClient(host, port); this.mongo.getConnector().getDBPortPool(mongo.getAddress()).get() .ensureOpen(); } catch (UnknownHostException e) { MongoConnection.log.error("Invalid MongoDB host"); } catch (Exception e) { MongoConnection.log.error("Error connecting to database"); } }
@Override public boolean isConnected() { try { mongoClient.getConnector().getDBPortPool(mongoClient.getAddress()).get().ensureOpen(); } catch (Exception ex) { return false; } return true; } }
@Override public boolean isConnected() { try { mongoClient.getConnector().getDBPortPool(mongoClient.getAddress()).get().ensureOpen(); } catch (Exception ex) { return false; } return true; } }
coll = db.getCollection(collName); if(mongo == null || db == null || mongo.getAddress() == null || coll == null) logger.error("Mongo is down.");
operationalInfo.put("mongoServerAddress", client.getAddress().toString()); operationalInfo.put("mongoDatabaseName", dbName); operationalInfo.put("mongoUser", user);
private static Configuration getConf() throws IOException { // MongoDBIndexingConfigBuilder builder = MongoIndexingConfiguration.builder() // .setUseMockMongo(USE_MOCK).setUseInference(USE_INFER).setAuths("U"); MongoDBIndexingConfigBuilder builder = MongoIndexingConfiguration.builder() .setUseMockMongo(USE_EMBEDDED_MONGO).setUseInference(true).setAuths("U"); if (USE_EMBEDDED_MONGO) { final MongoClient c = EmbeddedMongoFactory.newFactory().newMongoClient(); final ServerAddress address = c.getAddress(); final String url = address.getHost(); final String port = Integer.toString(address.getPort()); c.close(); builder.setMongoHost(url).setMongoPort(port); } else { // User name and password must be filled in: builder = builder.setMongoUser(MongoUserName) .setMongoPassword(MongoUserPswd) .setMongoHost(MONGO_INSTANCE_URL) .setMongoPort(MONGO_INSTANCE_PORT); } return builder.setMongoDBName(MONGO_DB) .setMongoCollectionPrefix(MONGO_COLL_PREFIX) .setUseMongoFreetextIndex(true) .setMongoFreeTextPredicates(RDFS.LABEL.stringValue()).build(); }