Java Code Examples for io.vertx.ext.mongo.MongoClient#createShared()

The following examples show how to use io.vertx.ext.mongo.MongoClient#createShared() . 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: MongoShellAuth.java    From vertx-shell with Apache License 2.0 6 votes vote down vote up
@Override
public AuthProvider create(Vertx vertx, JsonObject config) {
  final MongoAuthOptions options = new MongoAuthOptions(config);
  MongoClient client;
  if (options.getShared()) {
    String datasourceName = options.getDatasourceName();
    if (datasourceName != null) {
      client = MongoClient.createShared(vertx, options.getConfig(), datasourceName);
    } else {
      client = MongoClient.createShared(vertx, options.getConfig());
    }
  } else {
    client = MongoClient.create(vertx, options.getConfig());
  }

  JsonObject authConfig = new JsonObject();
  MongoAuthOptionsConverter.toJson(options, authConfig);
  return MongoAuth.create(client, authConfig);
}
 
Example 4
Source File: App.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MongoDBBenchmark(Vertx vertx, JsonObject config) {
  final JsonObject mongoConfig = config.copy();

  // mongo is configured without credentials
  mongoConfig.remove("username");
  mongoConfig.remove("password");

  this.database = MongoClient.createShared(vertx, mongoConfig);
  this.engine = RockerTemplateEngine.create();
}
 
Example 5
Source File: MongoBaseTest.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
private void initMongoClient() throws Exception {
  CountDownLatch latch = new CountDownLatch(1);
  System.out.println(getConfig().encode());
  mongoClient = MongoClient.createShared(vertx, getConfig());
  dropCollections(latch);
  awaitLatch(latch);
}
 
Example 6
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 7
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 8
Source File: InitMongoDB.java    From vxms 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 9
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 10
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 11
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 12
Source File: InitMongoDB.java    From vxms 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 13
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 14
Source File: InitMongoDB.java    From kube_vertx_demo 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 15
Source File: InitMongoDB.java    From kube_vertx_demo 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 16
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 17
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 18
Source File: AuthMongoExamples.java    From vertx-auth with Apache License 2.0 4 votes vote down vote up
public void example1(Vertx vertx, JsonObject mongoClientConfig) {
  MongoClient client = MongoClient.createShared(vertx, mongoClientConfig);
  MongoAuthenticationOptions options = new MongoAuthenticationOptions();
  MongoAuthentication authenticationProvider =
    MongoAuthentication.create(client, options);
}
 
Example 19
Source File: ApplicationConfig.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Gets a {@link MongoClient} instance for MongoDB interaction.
 *
 * @return An instance of the {@link MongoClient}.
 */
@Bean
@Scope("prototype")
public MongoClient mongoClient() {
    return MongoClient.createShared(vertx(), mongoDbConfigProperties().getMongoClientConfig());
}
 
Example 20
Source File: AnalyticsVerticle.java    From vertx-starter with Apache License 2.0 4 votes vote down vote up
private MongoClient mongoClient() {
  return MongoClient.createShared(vertx, config());
}