Java 类com.mongodb.async.client.MongoClients 实例源码

项目:timpani    文件:MongoDBSecurityDefinitionStore.java   
@Override
public CompletableFuture<SecurityDefinitionStore> open() {
  List<ServerAddress> hostList =
      Arrays.stream(hosts).map(h -> new ServerAddress(h)).collect(Collectors.toList());
  ClusterSettings clusterSettings = ClusterSettings.builder().hosts(hostList).build();
  MongoClientSettings settings =
      MongoClientSettings.builder().clusterSettings(clusterSettings).build();
  mongoClient = MongoClients.create(settings);

  database = mongoClient.getDatabase(DATABASE_NAME);
  collection = database.getCollection(SECDEF_COLLECTION_NAME);

  // In the case of MongoDB, open is synchronous because it doesn't
  // actually communicate with the server until a query is invoked.
  return CompletableFuture.completedFuture(this);
}
项目:IKB4Stream    文件:DatabaseWriter.java   
/**
 * DataWriter constructor
 *
 * @throws IllegalStateException if database configuration is not set
 */
private DatabaseWriter() {
    try {
        final MongoClient mongoClient = MongoClients.create(PROPERTIES_MANAGER.getProperty("database.host"));
        final MongoDatabase mongoDatabase = mongoClient.getDatabase(PROPERTIES_MANAGER.getProperty("database.datasource"));
        this.mongoCollection = mongoDatabase.getCollection(PROPERTIES_MANAGER.getProperty("database.collection"));
    } catch (IllegalArgumentException e) {
        LOGGER.error(e.getMessage());
        throw new IllegalStateException(e.getMessage());
    }
    LOGGER.info("DatabaseWriter has been instantiate");
}
项目:georocket    文件:MongoDBStore.java   
/**
 * Get or create the MongoDB client
 * @return the MongoDB client
 */
private MongoClient getMongoClient() {
  if (mongoClient == null) {
    mongoClient = MongoClients.create(connectionString);
  }
  return mongoClient;
}
项目:georocket    文件:MongoDBChunkReadStreamTest.java   
/**
 * Create an asynchronous MongoDB client
 * @return the client
 */
private com.mongodb.async.client.MongoClient createAsyncClient() {
  ClusterSettings clusterSettings = ClusterSettings.builder()
      .hosts(Arrays.asList(mongoConnector.serverAddress))
      .build();
  MongoClientSettings settings = MongoClientSettings.builder()
      .clusterSettings(clusterSettings).build();
  return MongoClients.create(settings);
}
项目:mongiORM    文件:QueryAsync.java   
/**
 *
 * @param mongi
 */
public QueryAsync(Mongi mongi){

    this.mongi = mongi;
    mongiDb = mongi.getMongoDatabase();
    mongoClient = mongi.getMongoClient();
    // Use a Connection String
    mongoClientAsyn = MongoClients.create("mongodb://localhost");
    database = mongoClientAsyn.getDatabase("testingMe");
    coll = database.getCollection("test");

}
项目:mongowg    文件:MongoWGPlugin.java   
/**
 * {@inheritDoc}
 */
@Override
public void onEnable() {
    saveDefaultConfig();

    CodecRegistry codecRegistry = createCodecRegistry();
    MongoClientSettings settings = MongoClientSettings.builder()
            .clusterSettings(ClusterSettings.builder().applyConnectionString(new ConnectionString(getConfig().getString("mongodb.uri"))).build())
            .codecRegistry(codecRegistry)
            .build();
    client = MongoClients.create(settings);
    MongoDatabase database = client.getDatabase(getConfig().getString("mongodb.database"));
    if (!testConnection(database))
        return;
    RegionStorageAdapter storageAdapter = new RegionStorageAdapter(database);
    MongoRegionDriver driver = new MongoRegionDriver(getServer(), storageAdapter);

    WorldGuardPlugin wgPlugin = WorldGuardPlugin.inst();
    if (getConfig().getBoolean("mongodb.use_oplog")) {
        getLogger().info("OpLog usage enabled.");
        WorldGuardOpLogHandler opLogHandler = new WorldGuardOpLogHandler(codecRegistry.get(ProcessingProtectedRegion.class), storageAdapter, wgPlugin);
        getServer().getScheduler().runTaskAsynchronously(this, new OpLogRetriever(
                OpLogUtils.getCollection(client),
                new OpLogParser(opLogHandler),
                getConfig().getString("mongodb.database") + "." + RegionStorageAdapter.COLLECTION_NAME
        ));
        storageAdapter.setListener(opLogHandler);
    }

    ConfigurationManager config = wgPlugin.getGlobalStateManager();
    RegionContainer container = wgPlugin.getRegionContainer();
    InjectionUtils.injectRegionDriver(container, driver);
    InjectionUtils.callUnload(container);
    InjectionUtils.callLoadWorlds(container);
    config.selectedRegionStoreDriver = driver;
}
项目:vertx-mongo-client    文件:MongoClientImpl.java   
synchronized com.mongodb.async.client.MongoClient mongo() {
  if (mongo == null) {
    MongoClientOptionsParser parser = new MongoClientOptionsParser(config);
    mongo = MongoClients.create(parser.settings());
    db = mongo.getDatabase(parser.database());
  }
  return mongo;
}
项目:vertx-mongo-client    文件:MongoClientTest.java   
@Override
public void setUp() throws Exception {
  super.setUp();
  JsonObject config = getConfig();
  mongoClient = MongoClient.createNonShared(vertx, config);
  CountDownLatch latch = new CountDownLatch(1);
  dropCollections(mongoClient, latch);
  awaitLatch(latch);


  actualMongo = MongoClients.create("mongodb://localhost:27018");
  db = actualMongo.getDatabase(io.vertx.ext.mongo.MongoClient.DEFAULT_DB_NAME);
}
项目:kevoree-library    文件:MongoChan.java   
@Start
public void start() {
    final ClusterSettings clusterSettings = ClusterSettings.builder().hosts(asList(new ServerAddress(host, port))).build();
    final MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build();
    mongoClient = MongoClients.create(settings);
    db = mongoClient.getDatabase(this.database);
    launchConsumers();
}
项目:activecheck    文件:MongodbReporter.java   
private void connect() throws ActivecheckReporterException {
    if (mongoClient == null) {
        logger.debug("Cannot run query. MongoDB is not connected. Trying to (re)connect.");
        try {
            // configure credentials
            List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
            String username = properties
                    .getString("mongodb.username", null);
            String password = properties
                    .getString("mongodb.password", null);
            if (username != null && password != null) {
                credentialsList.add(MongoCredential.createPlainCredential(
                        username, "*", password.toCharArray()));
            }

            // configure server addresses
            List<ServerAddress> addressList = new ArrayList<ServerAddress>();
            String socketPath = properties.getString("socket", null);
            if (socketPath != null) {
                addressList.add(new ServerAddress(new AFUNIXSocketAddress(
                        new File(socketPath))));
            } else {
                String url = properties.getString("url",
                        ServerAddress.defaultHost());
                int port = ServerAddress.defaultPort();
                String[] urlParts = url.split(":");
                if (urlParts.length > 1) {
                    port = Integer.parseInt(urlParts[1]);
                }
                addressList.add(new ServerAddress(urlParts[0], port));
            }
            ServerSelector serverSelector = new ReadPreferenceServerSelector(
                    MONGO_READ_PREFERENCE);
            ClusterSettings clusterSettings = ClusterSettings.builder()
                    .hosts(addressList).serverSelector(serverSelector)
                    .build();

            // actually configure and (re)create mongoClient
            ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings
                    .builder().maxSize(MONGO_POOL_SIZE).build();
            MongoClientSettings settings = MongoClientSettings.builder()
                    .readPreference(MONGO_READ_PREFERENCE)
                    .credentialList(credentialsList)
                    .clusterSettings(clusterSettings)
                    .connectionPoolSettings(connectionPoolSettings).build();
            mongoClient = MongoClients.create(settings);
        } catch (Exception e) {
            mongoClient = null;
            String errorMessage = "MongodbReporter Configuration Error for service '"
                    + getOverallServiceName() + "': " + e.getMessage();

            logger.error(errorMessage);
            logger.trace(e.getMessage(), e);

            // set report and status
            setOverallServiceReport(NagiosServiceStatus.CRITICAL,
                    errorMessage);
            throw new ActivecheckReporterException(e);
        }
    }
}