/** * Get a list of all collections in the database. * @param resultHandler will be called with a list of collections. * @return */ public io.vertx.rxjava.ext.mongo.MongoClient getCollections(Handler<AsyncResult<List<String>>> resultHandler) { delegate.getCollections(resultHandler); return this; }
/** * Get a list of all collections in the database. * @param resultHandler will be called with a list of collections. * @return */ public io.vertx.rxjava.ext.mongo.MongoClient getCollections(Handler<AsyncResult<List<String>>> resultHandler) { delegate.getCollections(resultHandler); return this; }
@Override public MongoService getCollections(Handler<AsyncResult<List<String>>> resultHandler) { client.getCollections(resultHandler); return this; }
private void initMongoClient(Handler<AsyncResult<Void>> handler) { try { Vertx vertx = getVertx(); JsonObject config = getConfig(); LOGGER.info("STARTING MONGO CLIENT with config " + config); mongoClient = shared ? MongoClient.createShared(vertx, config) : MongoClient.createNonShared(vertx, config); if (mongoClient == null) { handler.handle(Future.failedFuture(new InitException("No MongoClient created"))); } else { mongoClient.getCollections(resultHandler -> { if (resultHandler.failed()) { LOGGER.error("", resultHandler.cause()); handler.handle(Future.failedFuture(resultHandler.cause())); } else { LOGGER.info(String.format("found %d collections", resultHandler.result().size())); handler.handle(Future.succeededFuture()); } }); } } catch (Exception e) { handler.handle(Future.failedFuture(new InitException(e))); } }
/** * Method drops all collections which are starting with the prefix {@link #TABLE_PREFIX} * * @param latch * the latch to be used */ protected void dropCollections(CountDownLatch latch) { // Drop all the collections in the db mongoClient.getCollections(onSuccess(list -> { AtomicInteger collCount = new AtomicInteger(); List<String> toDrop = getOurCollections(list); int count = toDrop.size(); if (!toDrop.isEmpty()) { for (String collection : toDrop) { mongoClient.dropCollection(collection, onSuccess(v -> { if (collCount.incrementAndGet() == count) { latch.countDown(); } })); } } else { latch.countDown(); } })); }
@Test public void testWithSugar() throws InterruptedException { Record record = MongoDataSource.createRecord("some-mongo-db", new JsonObject().put("connection_string", "mongodb://localhost:12345"), new JsonObject().put("database", "some-raw-data")); discovery.publish(record, r -> { }); await().until(() -> record.getRegistration() != null); AtomicBoolean success = new AtomicBoolean(); MongoDataSource.getMongoClient(discovery, new JsonObject().put("name", "some-mongo-db"), ar -> { MongoClient client = ar.result(); client.getCollections(coll -> { client.close(); success.set(coll.succeeded()); }); }); await().untilAtomic(success, is(true)); } }
@Test public void testWithSugar() throws InterruptedException { Record record = MongoDataSource.createRecord("some-mongo-db", new JsonObject().put("connection_string", "mongodb://localhost:12345"), new JsonObject().put("database", "some-raw-data")); discovery.publish(record, r -> { }); await().until(() -> record.getRegistration() != null); AtomicBoolean success = new AtomicBoolean(); MongoDataSource.getMongoClient(discovery, new JsonObject().put("name", "some-mongo-db"), ar -> { MongoClient client = ar.result(); client.getCollections(coll -> { client.close(); success.set(coll.succeeded()); }); }); await().untilAtomic(success, is(true)); } }
@Test public void test() throws InterruptedException { Record record = MongoDataSource.createRecord("some-mongo-db", new JsonObject().put("connection_string", "mongodb://localhost:12345"), new JsonObject().put("database", "some-raw-data")); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); AtomicReference<Record> found = new AtomicReference<>(); discovery.getRecord(new JsonObject().put("name", "some-mongo-db"), ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); ServiceReference service = discovery.getReference(found.get()); MongoClient client = service.get(); AtomicBoolean success = new AtomicBoolean(); client.getCollections(ar -> { success.set(ar.succeeded()); }); await().untilAtomic(success, is(true)); service.release(); // Just there to be sure we can call it twice service.release(); }
@Test public void test() throws InterruptedException { Record record = MongoDataSource.createRecord("some-mongo-db", new JsonObject().put("connection_string", "mongodb://localhost:12345"), new JsonObject().put("database", "some-raw-data")); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); AtomicReference<Record> found = new AtomicReference<>(); discovery.getRecord(new JsonObject().put("name", "some-mongo-db"), ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); ServiceReference service = discovery.getReference(found.get()); MongoClient client = service.get(); AtomicBoolean success = new AtomicBoolean(); client.getCollections(ar -> { success.set(ar.succeeded()); }); await().untilAtomic(success, is(true)); service.release(); // Just there to be sure we can call it twice service.release(); }