io.vertx.ext.mongo.MongoClient Java Examples

The following examples show how to use io.vertx.ext.mongo.MongoClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongoHandle.java    From okapi with Apache License 2.0 6 votes vote down vote up
MongoHandle(Vertx vertx, JsonObject conf) {
  JsonObject opt = new JsonObject();
  String h = Config.getSysConf("mongo_host", "localhost", conf);
  if (!h.isEmpty()) {
    opt.put("host", h);
  }
  String p = Config.getSysConf("mongo_port", "27017", conf);
  if (!p.isEmpty()) {
    opt.put("port", Integer.parseInt(p));
  }
  String dbName = Config.getSysConf("mongo_db_name", "", conf);
  if (!dbName.isEmpty()) {
    opt.put("db_name", dbName);
  }
  Logger logger = OkapiLogger.get();
  logger.info("Using mongo backend at {} : {} / {}", h, p, dbName);
  this.cli = MongoClient.createShared(vertx, opt);
}
 
Example #2
Source File: MongoAuthOptions.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Override
public MongoAuth createProvider(Vertx vertx) {
  MongoClient client;
  if (shared) {
    if (datasourceName != null) {
      client = MongoClient.createShared(vertx, config, datasourceName);
    } else {
      client = MongoClient.createShared(vertx, config);
    }
  } else {
    client = MongoClient.create(vertx, config);
  }
  JsonObject authConfig = new JsonObject();
  MongoAuthOptionsConverter.toJson(this, authConfig);
  return MongoAuth.create(client, authConfig);
}
 
Example #3
Source File: MongoDbBasedCredentialsService.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a new service for configuration properties.
 *
 * @param vertx The vert.x instance to run on.
 * @param mongoClient The client for accessing the Mongo DB instance.
 * @param config The properties for configuring this service.
 * @param passwordEncoder The password encoder.
 * @throws NullPointerException if any of the parameters are {@code null}.
 */
public MongoDbBasedCredentialsService(
        final Vertx vertx,
        final MongoClient mongoClient,
        final MongoDbBasedCredentialsConfigProperties config,
        final HonoPasswordEncoder passwordEncoder) {

    super(Objects.requireNonNull(vertx), Objects.requireNonNull(passwordEncoder));

    Objects.requireNonNull(mongoClient);
    Objects.requireNonNull(config);

    this.mongoClient = mongoClient;
    this.config = config;
    this.passwordEncoder = passwordEncoder;
    this.mongoDbCallExecutor = new MongoDbCallExecutor(vertx, mongoClient);
}
 
Example #4
Source File: InitMongoDB.java    From kube_vertx_demo with Apache License 2.0 6 votes vote down vote up
private static void loadData(MongoClient db) {
    db.find("users", new JsonObject(), lookup -> {
        // error handling
        if (lookup.failed()) {
            dropAndCreate(db);
            return;
        }

        if(lookup.result().isEmpty()){
            dropAndCreate(db);
        }else {
            System.out.println("users already exists");
        }

    });

}
 
Example #5
Source File: InitMongoDB.java    From vxms with Apache License 2.0 6 votes vote down vote up
private static void loadData(MongoClient db) {
    db.find("users", new JsonObject(), lookup -> {
        // error handling
        if (lookup.failed()) {
            dropAndCreate(db);
            return;
        }

        if (lookup.result().isEmpty()) {
            dropAndCreate(db);
        } else {
            System.out.println("users already exists");
        }

    });

}
 
Example #6
Source File: MongoDataSourceExamples.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
public void example3(ServiceDiscovery discovery) {
  MongoDataSource.<JsonObject>getMongoClient(discovery,
    new JsonObject().put("name", "some-data-source-service"),
    new JsonObject().put("username", "clement").put("password", "*****"), // Some additional metadata
    ar -> {
      if (ar.succeeded()) {
        MongoClient client = ar.result();

        // ...

        // Dont' forget to release the service
        ServiceDiscovery.releaseServiceObject(discovery, client);

      }
    });
}
 
Example #7
Source File: InitMongoDB.java    From vxms with Apache License 2.0 5 votes vote down vote up
private static void dropAndCreate(MongoClient db) {
    db.dropCollection("users", drop -> {
        if (drop.failed()) {
            throw new RuntimeException(drop.cause());
        }

        List<JsonObject> users = new LinkedList<>();

        users.add(new JsonObject()
                .put("username", "pmlopes")
                .put("firstName", "Paulo")
                .put("lastName", "Lopes")
                .put("address", "The Netherlands"));

        users.add(new JsonObject()
                .put("username", "timfox")
                .put("firstName", "Tim")
                .put("lastName", "Fox")
                .put("address", "The Moon"));

        for (JsonObject user : users) {
            db.insert("users", user, res -> {
                System.out.println("inserted " + user.encode());
            });
        }
    });
}
 
Example #8
Source File: MongoAuthenticationImpl.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Provided for backward compatibility
 * @param mongoClient
 * @param legacyStrategy
 * @param options
 */
public MongoAuthenticationImpl(MongoClient mongoClient, HashStrategy legacyStrategy, String hashField, MongoAuthenticationOptions options) {
  this.mongoClient = mongoClient;
  this.options = options;
  this.legacyStrategy = legacyStrategy;
  this.hashField = hashField;
}
 
Example #9
Source File: MongoUtilTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsert(TestContext context) {
  MongoClient cli = new FakeMongoClient();
  MongoUtil<DeploymentDescriptor> util = new MongoUtil<>("collection", cli);
  util.insert(new DeploymentDescriptor(), "1", res -> {
    context.assertTrue(res.failed());
    context.assertEquals("insert failed", res.cause().getMessage());
  });
}
 
Example #10
Source File: InitMongoDB.java    From kube_vertx_demo with Apache License 2.0 5 votes vote down vote up
private static void dropAndCreate(MongoClient db) {
    db.dropCollection("users", drop -> {
        if (drop.failed()) {
            throw new RuntimeException(drop.cause());
        }

        List<JsonObject> users = new LinkedList<>();

        users.add(new JsonObject()
                .put("username", "pmlopes")
                .put("firstName", "Paulo")
                .put("lastName", "Lopes")
                .put("address", "The Netherlands"));

        users.add(new JsonObject()
                .put("username", "timfox")
                .put("firstName", "Tim")
                .put("lastName", "Fox")
                .put("address", "The Moon"));

        for (JsonObject user : users) {
            db.insert("users", user, res -> {
                System.out.println("inserted " + user.encode());
            });
        }
    });
}
 
Example #11
Source File: MongoDbTestUtils.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a new vert.x Mongo DB client.
 *
 * @param vertx The vert.x instance to run on.
 * @param dbName The name of the database to connect to.
 * @return The client.
 * @throws NullPointerException if any of the parameters are {@code null}.
 */
public static MongoClient getMongoClient(final Vertx vertx, final String dbName) {

    Objects.requireNonNull(vertx);
    Objects.requireNonNull(dbName);

    final MongoDbConfigProperties mongoDbConfig = new MongoDbConfigProperties()
            .setHost(MongoDbTestUtils.MONGO_DB_HOST)
            .setPort(MongoDbTestUtils.MONGO_DB_PORT)
            .setDbName(dbName);
    return MongoClient.createShared(vertx, mongoDbConfig.getMongoClientConfig());
}
 
Example #12
Source File: MongoUtilTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelete(TestContext context) {
  MongoClient cli = new FakeMongoClient();
  MongoUtil<String> util = new MongoUtil<>("collection", cli);
  util.delete("1", res -> {
    context.assertTrue(res.failed());
    context.assertEquals("removeDocument failed", res.cause().getMessage());
    context.assertEquals(ErrorType.INTERNAL, res.getType());
  });
}
 
Example #13
Source File: InitMongoDB.java    From vxms with Apache License 2.0 5 votes vote down vote up
public static MongoClient initMongoData(Vertx vertx, JsonObject config) {
    MongoClient mongo;
    // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
    String connectionUrl = connectionURL();
    boolean local = config.getBoolean("local", false);
    if (connectionUrl != null && !local) {
        String dbName = config.getString("dbname", "vxmsdemo");
        mongo = MongoClient.createShared(vertx, new JsonObject().put("connection_string", connectionUrl).put("db_name", dbName));
    } else {
        mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
    }
    // the load function just populates some data on the storage
    return mongo;
}
 
Example #14
Source File: ProcessorConnectKafka.java    From df_data_service with Apache License 2.0 5 votes vote down vote up
/**
 * This method first decode the REST POST request to DFJobPOPJ object. Then, it updates its job status and repack
 * for Kafka REST POST. After that, it forward the new POST to Kafka Connect.
 * Once REST API forward is successful, update data to the local repository.
 *
 * @param routingContext This is the contect from REST API
 * @param webClient This is vertx non-blocking rest client used for forwarding
 * @param mongoClient This is the client used to insert final data to repository - mongodb
 * @param mongoCOLLECTION This is mongodb collection name
 * @param kafkaConnectRestHost rest server host name
 * @param kafkaConnectRestPort rest server port number
 * @param dfJobResponsed This is the response object return to rest client or ui or mongo insert
 */
public static void forwardPOSTAsAddOne(RoutingContext routingContext, WebClient webClient,
                                       MongoClient mongoClient, String mongoCOLLECTION,
                                       String kafkaConnectRestHost, int kafkaConnectRestPort,
                                       DFJobPOPJ dfJobResponsed) {
    // Create REST Client for Kafka Connect REST Forward
    webClient.post(kafkaConnectRestPort, kafkaConnectRestHost, ConstantApp.KAFKA_CONNECT_REST_URL)
            .putHeader(ConstantApp.HTTP_HEADER_CONTENT_TYPE, ConstantApp.HTTP_HEADER_APPLICATION_JSON_CHARSET)
            .sendJsonObject(dfJobResponsed.toKafkaConnectJson(),
                    ar -> {
                        if (ar.succeeded()) {
                            // Once REST API forward is successful, add the record to the local repository
                            mongoClient.insert(mongoCOLLECTION, dfJobResponsed.toJson(), r ->
                                    HelpFunc.responseCorsHandleAddOn(routingContext.response())
                                            .setStatusCode(ConstantApp.STATUS_CODE_OK_CREATED)
                                            .end(Json.encodePrettily(dfJobResponsed)));
                            LOG.info(DFAPIMessage.logResponseMessage(1000, dfJobResponsed.getId()));
                        } else {
                            // If response is failed, repose df ui and still keep the task
                            HelpFunc.responseCorsHandleAddOn(routingContext.response())
                                    .setStatusCode(ConstantApp.STATUS_CODE_BAD_REQUEST)
                                    .end(DFAPIMessage.getResponseMessage(9037));
                            LOG.info(DFAPIMessage.logResponseMessage(9037, dfJobResponsed.getId()));
                        }
                    }
            );
}
 
Example #15
Source File: InitMongoDB.java    From kube_vertx_demo with Apache License 2.0 5 votes vote down vote up
public static void initMongoData(Vertx vertx,JsonObject config) {
    MongoClient mongo;
    // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
    String connectionUrl = connectionURL();
    boolean local = config.getBoolean("local", false);
    if (connectionUrl != null && !local) {
        String dbName = config.getString("dbname", "vxmsdemo");
        mongo = MongoClient.createShared(vertx, new JsonObject().put("connection_string", connectionUrl).put("db_name", dbName));
    } else {
        mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
    }
    // the load function just populates some data on the storage
    loadData(mongo);
}
 
Example #16
Source File: InitMongoDB.java    From vxms with Apache License 2.0 5 votes vote down vote up
public static MongoClient initMongoData(Vertx vertx, JsonObject config) {
    MongoClient mongo;
    // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
    String connectionUrl = connectionURL();
    boolean local = config.getBoolean("local", false);
    if (connectionUrl != null && !local) {
        String dbName = config.getString("dbname", "vxmsdemo");
        mongo = MongoClient.createShared(vertx, new JsonObject().put("connection_string", connectionUrl).put("db_name", dbName));
    } else {
        mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
    }
    // the load function just populates some data on the storage
    return mongo;
}
 
Example #17
Source File: InitMongoDB.java    From vxms with Apache License 2.0 5 votes vote down vote up
public static MongoClient initMongoData(Vertx vertx, JsonObject config) {
    MongoClient mongo;
    // Create a mongo client using all defaults (connect to localhost and default port) using the database name "demo".
    String connectionUrl = connectionURL();
    boolean local = config.getBoolean("local", false);
    if (connectionUrl != null && !local) {
        String dbName = config.getString("dbname", "vxmsdemo");
        mongo = MongoClient.createShared(vertx, new JsonObject().put("connection_string", connectionUrl).put("db_name", dbName));
    } else {
        mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
    }
    // the load function just populates some data on the storage
    return mongo;
}
 
Example #18
Source File: HelpFunc.java    From df_data_service with Apache License 2.0 5 votes vote down vote up
/**
 * Helper class to update mongodb status
 * @param mongo
 * @param COLLECTION
 * @param updateJob
 * @param LOG
 */
public static void updateRepoWithLogging(MongoClient mongo, String COLLECTION, DFJobPOPJ updateJob, Logger LOG) {
    mongo.updateCollection(COLLECTION, new JsonObject().put("_id", updateJob.getId()),
            // The update syntax: {$set, the json object containing the fields to update}
            new JsonObject().put("$set", updateJob.toJson()), v -> {
                if (v.failed()) {
                    LOG.error(DFAPIMessage.logResponseMessage(9003, updateJob.getId() + "cause:" + v.cause()));
                } else {
                    LOG.info(DFAPIMessage.logResponseMessage(1021, updateJob.getId()));
                }
            }
    );
}
 
Example #19
Source File: MongoUtilTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testInit(TestContext context) {
  MongoClient cli = new FakeMongoClient();
  MongoUtil<String> util = new MongoUtil<>("collection", cli);
  util.init(true, res -> {
    context.assertTrue(res.failed());
    context.assertEquals("dropCollection failed", res.cause().getMessage());
  });
}
 
Example #20
Source File: MyFirstVerticle.java    From my-vertx-first-app with Apache License 2.0 5 votes vote down vote up
/**
 * This method is called when the verticle is deployed. It creates a HTTP server and registers a simple request
 * handler.
 * <p/>
 * Notice the `listen` method. It passes a lambda checking the port binding result. When the HTTP server has been
 * bound on the port, it call the `complete` method to inform that the starting has completed. Else it reports the
 * error.
 *
 * @param fut the future
 */
@Override
public void start(Future<Void> fut) {

  // Create a Mongo client
  mongo = MongoClient.createShared(vertx, config());


  createSomeData(
      (nothing) -> startWebApp(
          (http) -> completeStartup(http, fut)
      ), fut);
}
 
Example #21
Source File: MongoDataSourceImpl.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
@Override
public MongoClient retrieve() {
  JsonObject result = record().getMetadata().copy();
  result.mergeIn(record().getLocation());

  if (config != null) {
    result.mergeIn(config);
  }

  if (result.getBoolean("shared", false)) {
    return MongoClient.createShared(vertx, result);
  } else {
    return MongoClient.create(vertx, result);
  }
}
 
Example #22
Source File: MongoClientImpl.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
synchronized com.mongodb.reactivestreams.client.MongoClient mongo(Vertx vertx) {
  if (mongo == null) {
    MongoClientOptionsParser parser = new MongoClientOptionsParser(vertx, config);
    mongo = MongoClients.create(parser.settings());
    db = mongo.getDatabase(parser.database());
  }
  return mongo;
}
 
Example #23
Source File: MongoAuthImpl.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance
 *
 * @param mongoClient
 *          the {@link MongoClient} to be used
 * @param config
 *          the config for configuring the new instance
 * @see MongoAuth#create(MongoClient, JsonObject)
 */
public MongoAuthImpl(MongoClient mongoClient, JsonObject config) {
  this.mongoClient = mongoClient;
  this.config = config;
  this.mongoAuthenticationOptions = new MongoAuthenticationOptions();
  this.mongoAuthorizationOptions = new MongoAuthorizationOptions();
  init();
  this.mongoAuthentication = MongoAuthentication.create(mongoClient, getHashStrategy(), mongoAuthenticationOptions);
  this.mongoAuthorization = MongoAuthorization.create(PROVIDER_ID, mongoClient, mongoAuthorizationOptions);
}
 
Example #24
Source File: MongoDataSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a Mongo datasource source and provides the configured {@link io.vertx.ext.mongo.MongoClient}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery             The service discovery instance
 * @param filter                The filter, optional
 * @param consumerConfiguration the consumer configuration
 * @param resultHandler         the result handler
 */
static void getMongoClient(ServiceDiscovery discovery, JsonObject filter, JsonObject consumerConfiguration,
                           Handler<AsyncResult<MongoClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(
        discovery.getReferenceWithConfiguration(ar.result(), consumerConfiguration).get()));
    }
  });
}
 
Example #25
Source File: ProcessorTransformSpark.java    From df_data_service with Apache License 2.0 5 votes vote down vote up
/**
 * This method is to update a task by creating livy session and resubmit the statement to livy.
 * Update should always use current session. If it is busy, the statement is in waiting state.
 * If the sesseion is closed or session id is not available, use new/idle session.
 * @param vertx  response for rest client
 * @param webClient vertx web client for rest
 * @param sparkRestHost flinbk rest hostname
 * @param sparkRestPort flink rest port number
 * @param mongoClient repo handler
 * @param taskCollection collection to keep data
 */
public static void forwardPutAsUpdateOne(Vertx vertx, WebClient webClient,
                                         DFJobPOPJ dfJob, MongoClient mongoClient,
                                         String taskCollection, String sparkRestHost, int sparkRestPort) {

    // When session id is not available, use new/idle session to add new statement
    if(dfJob.getJobConfig() == null ||
            (dfJob.getJobConfig() != null && !dfJob.getJobConfig().containsKey(ConstantApp.PK_LIVY_STATEMENT_ID))) {
        // Submit new task using new/idle session and statement
        forwardPostAsAddOne(
                vertx, webClient, dfJob,
                mongoClient, taskCollection,
                sparkRestHost, sparkRestPort, ""
        );
    } else {
        String sessionId = dfJob.getJobConfig().get(ConstantApp.PK_LIVY_STATEMENT_ID);
        webClient.get(sparkRestPort, sparkRestHost,
                ConstantApp.LIVY_REST_URL_SESSIONS + "/" + sessionId + "/state")
                .putHeader(ConstantApp.HTTP_HEADER_CONTENT_TYPE,
                        ConstantApp.HTTP_HEADER_APPLICATION_JSON_CHARSET)
                .send(sar -> { // If session is active, we always wait
                            if (sar.succeeded() && sar.result().statusCode() == ConstantApp.STATUS_CODE_OK) {
                                addStatementToSession(
                                        webClient, dfJob,
                                        sparkRestHost, sparkRestPort, mongoClient, taskCollection, sessionId, ""
                                );
                            } else { // session is closed
                                // Submit new task using new/idle session and statement
                                forwardPostAsAddOne(
                                        vertx, webClient, dfJob,
                                        mongoClient, taskCollection,
                                        sparkRestHost, sparkRestPort, ""
                                );
                            }
                });
    }
}
 
Example #26
Source File: MongoUtilTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd(TestContext context) {
  MongoClient cli = new FakeMongoClient();
  MongoUtil<DeploymentDescriptor> util = new MongoUtil<>("collection", cli);
  util.add(new DeploymentDescriptor(), "1", res -> {
    context.assertTrue(res.failed());
    context.assertEquals("updateCollectionWithOptions failed", res.cause().getMessage());
  });
}
 
Example #27
Source File: MongoClientImpl.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
synchronized com.mongodb.reactivestreams.client.MongoClient mongo(Vertx vertx, MongoClientSettings settings) {
  if (mongo == null) {
    MongoClientOptionsParser parser = new MongoClientOptionsParser(vertx, config);
    mongo = MongoClients.create(settings);
    db = mongo.getDatabase(parser.database());
  }
  return mongo;
}
 
Example #28
Source File: MongoUtilTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAll(TestContext context) {
  MongoClient cli = new FakeMongoClient();
  MongoUtil<DeploymentDescriptor> util = new MongoUtil<>("collection", cli);
  util.getAll(DeploymentDescriptor.class, res -> {
    context.assertTrue(res.failed());
    context.assertEquals("find failed", res.cause().getMessage());
  });
}
 
Example #29
Source File: MongoDbBasedTenantService.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a new service for configuration properties.
 *
 * @param vertx The vert.x instance to run on.
 * @param mongoClient The client for accessing the Mongo DB instance.
 * @param config The properties for configuring this service.
 * @throws NullPointerException if any of the parameters are {@code null}.
 */
public MongoDbBasedTenantService(
        final Vertx vertx,
        final MongoClient mongoClient,
        final MongoDbBasedTenantsConfigProperties config) {

    Objects.requireNonNull(vertx);
    Objects.requireNonNull(mongoClient);
    Objects.requireNonNull(config);

    this.mongoClient = mongoClient;
    this.mongoDbCallExecutor = new MongoDbCallExecutor(vertx, mongoClient);
    this.config = Objects.requireNonNull(config);
}
 
Example #30
Source File: MongoClientImpl.java    From vertx-mongo-client with Apache License 2.0 4 votes vote down vote up
@Override
public MongoClient bulkWriteWithOptions(String collection, List<BulkOperation> operations, BulkWriteOptions bulkWriteOptions, Handler<AsyncResult<MongoClientBulkWriteResult>> resultHandler) {
  Future<MongoClientBulkWriteResult> future = bulkWriteWithOptions(collection, operations, bulkWriteOptions);
  setHandler(future, resultHandler);
  return this;
}