@Override public Boolean check() throws Exception { try { getDB().getStats(); return true; } catch (Exception e) { return false; } }
public static void main(String[] args) { MongoClient mongoClient = null; MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("admin", "admin", "admin123".toCharArray()); mongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(mongoCredential)); DB db = mongoClient.getDB("testdb"); System.out.println(db.getStats()); System.out.println(db.getCollectionNames()); mongoClient.close(); }
/** * Returns the MongoDB statistics for the given database. * * @param mongoDatabase * * @return * * @throws NullPointerException if database instance provided is <code>null</code>. */ public static MongoDBStats getDatabaseStatistics(DB mongoDatabase) { CommandResult commandResult = mongoDatabase.getStats(); MongoDBStats stats = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create().fromJson(commandResult.toString(), MongoDBStats.class); return stats; }
/** * Returns the MongoDB statistics for the given database. * * @param mongoDatabase * the {@link DB} to get statistics from * * @return the {@link MongoDBStats} * * @throws NullPointerException * if database instance provided is <code>null</code>. */ public static MongoDBStats getDatabaseStatistics(DB mongoDatabase) { CommandResult commandResult = mongoDatabase.getStats(); MongoDBStats stats = GsonUtils.getGson().fromJson(commandResult.toString(), MongoDBStats.class); return stats; }
public boolean assertThatConnectionIsPossible(String host, int port) throws InterruptedException, UnknownHostException, MongoException { int currentRetry = 0; boolean connectionIsPossible = false; Mongo server = null; try { do { TimeUnit.SECONDS.sleep(WAIT_TIME); server = new MongoClient(host, port); DB db = server.getDB("admin"); try { db.getStats(); connectionIsPossible = true; } catch (MongoException e) { currentRetry++; } } while (!connectionIsPossible && currentRetry <= MAX_RETRIES); } finally { if(server!=null) server.close(); } return connectionIsPossible; }
@Override public Map<String, Object> call() { Map<String, Object> resultMap = new HashMap<String, Object>(); List<String> databaseNames = client.getDatabaseNames(); for (String databaseName : databaseNames) { DB db = client.getDB(databaseName); if (db != null) { CommandResult cr = db.getStats(); if (cr != null) { Object dataSize = cr.get("dataSize"); resultMap.put(databaseName, dataSize); } } } return resultMap; } });