Example usage for com.mongodb.async.client MongoClientSettings builder

List of usage examples for com.mongodb.async.client MongoClientSettings builder

Introduction

In this page you can find the example usage for com.mongodb.async.client MongoClientSettings builder.

Prototype

public static Builder builder() 

Source Link

Document

Convenience method to create a Builder.

Usage

From source file:com.github.frapontillo.pulse.crowd.data.repository.Repository.java

License:Apache License

/**
 * Create a new Repository using the default configuration in `database.properties` and
 * overriding the db name/*from  w w w  .j  a  v a2  s  .  c  o  m*/
 * with the one in input.
 *
 * @param db The database name to use for this Repository instance.
 */
@SuppressWarnings({ "unchecked", "deprecation" })
public Repository(String db) {
    DBConfig config = new DBConfig(getClass(), db);

    MongoClient client = new MongoClient(config.getServerAddress(), config.getCredentials());

    // map all Morphia classes
    morphia = new Morphia();
    morphia.mapPackageFromClass(Message.class);

    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Collections.singletonList(config.getServerAddress())).build();
    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings)
            .credentialList(config.getCredentials()).build();
    com.mongodb.reactivestreams.client.MongoClient rxClient = MongoClients.create(settings);

    // create and/or get the datastore
    datastore = morphia.createDatastore(client, config.getDBName());
    // init the DAO
    initDAO(datastore);
    ensureIndexes();

    // create the reactive database
    rxDatastore = rxClient.getDatabase(config.getDBName());

}

From source file:com.github.krr.mongodb.aggregate.support.config.ReactiveMongoClientTestConfiguration.java

License:Apache License

@Bean
public MongoClient mongoClient() throws IOException {
    ServerAddress serverAddress = getServerAddress();
    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(ClusterSettings.builder()
            .hosts(singletonList(serverAddress)).requiredClusterType(STANDALONE).build()).build();
    return MongoClients.create(settings);
}

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

@Bean(name = "mongoClient")
public MongoClient mongoClient() {
    System.out.println("*******************" + mongoAddress);

    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Arrays.asList(new ServerAddress(mongoAddress))).build();
    MongoCredential credential = MongoCredential.createCredential(mongoUser, mydb, mongoPasswd.toCharArray());

    MongoClientSettings settings = MongoClientSettings.builder()
            .streamFactoryFactory(new NettyStreamFactoryFactory()).clusterSettings(clusterSettings)
            .credentialList(Arrays.asList(credential)).build();

    //        MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));

    MongoClient mongoClient = MongoClients.create(settings);
    return mongoClient;
}

From source file:io.gravitee.am.identityprovider.mongo.authentication.spring.MongoAuthenticationProviderConfiguration.java

License:Apache License

@Bean
public MongoClient mongoClient() {
    MongoClient mongoClient;/*from  w  ww.j  av a 2  s . c  o m*/
    if ((this.configuration.getUri() != null) && (!this.configuration.getUri().isEmpty())) {
        mongoClient = MongoClients.create(this.configuration.getUri());
    } else {
        ServerAddress serverAddress = new ServerAddress(this.configuration.getHost(),
                this.configuration.getPort());
        ClusterSettings clusterSettings = ClusterSettings.builder().hosts(asList(serverAddress)).build();
        MongoClientSettings.Builder settings = MongoClientSettings.builder().clusterSettings(clusterSettings);
        if (this.configuration.isEnableCredentials()) {
            MongoCredential credential = MongoCredential.createCredential(
                    this.configuration.getUsernameCredentials(), this.configuration.getDatabaseCredentials(),
                    this.configuration.getPasswordCredentials().toCharArray());
            settings.credential(credential);
        }
        mongoClient = MongoClients.create(settings.build());
    }
    return mongoClient;
}

From source file:io.gravitee.am.repository.mongodb.management.ManagementRepositoryTestConfiguration.java

License:Apache License

@Bean(name = "managementMongo")
public MongoClient mongo() {
    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Collections.singletonList(new ServerAddress("localhost", 12345))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));

    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings)
            .codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    return MongoClients.create(settings);
}

From source file:io.gravitee.am.repository.mongodb.oauth2.OAuth2RepositoryTestConfiguration.java

License:Apache License

@Bean(name = "oauth2Mongo")
public MongoClient mongo() {
    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder()
            .hosts(Collections.singletonList(new ServerAddress("localhost", 12346))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));

    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings)
            .codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    return MongoClients.create(settings);
}

From source file:org.fixtrading.timpani.securitydef.datastore.MongoDBSecurityDefinitionStore.java

License:Apache License

@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);
}

From source file:org.jnosql.diana.mongodb.document.MongoDBDocumentConfiguration.java

License:Open Source License

private com.mongodb.async.client.MongoClient getAsyncMongoClient(List<ServerAddress> servers) {
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(servers).build();
    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build();
    return MongoClients.create(settings);
}

From source file:org.jooby.mongodb.MongoRx.java

License:Apache License

static MongoClientSettings.Builder settings(final ConnectionString cstr, final Config conf) {
    MongoClientSettings.Builder settings = MongoClientSettings.builder();

    settings.clusterSettings(cluster(cstr, conf));
    settings.connectionPoolSettings(pool(cstr, conf));
    settings.heartbeatSocketSettings(socket("heartbeat", cstr, conf));

    withStr("readConcern", conf,
            v -> settings.readConcern(Match(v.toUpperCase())
                    .option(Case("DEFAULT", ReadConcern.DEFAULT), Case("LOCAL", ReadConcern.LOCAL),
                            Case("MAJORITY", ReadConcern.MAJORITY))
                    .getOrElseThrow(() -> new IllegalArgumentException("readConcern=" + v))));

    withStr("readPreference", conf, v -> settings.readPreference(ReadPreference.valueOf(v)));

    settings.serverSettings(server(conf));
    settings.socketSettings(socket("socket", cstr, conf));
    settings.sslSettings(ssl(cstr, conf));

    withStr("writeConcern", conf,
            v -> settings.writeConcern(Match(v.toUpperCase())
                    .option(Case("W1", WriteConcern.W1), Case("W2", WriteConcern.W2),
                            Case("W3", WriteConcern.W3), Case("ACKNOWLEDGED", WriteConcern.ACKNOWLEDGED),
                            Case("JOURNALED", WriteConcern.JOURNALED), Case("MAJORITY", WriteConcern.MAJORITY))
                    .getOrElseThrow(() -> new IllegalArgumentException("writeConcern=" + v))));

    return settings;
}

From source file:org.jooby.mongodb.MongoRx.java

License:Apache License

/**
 * Allow further configuration on the {@link MongoClientSettings}.
 *
 * @param configurer Configurer callback.
 * @return This module.//from  w ww.  j a v  a 2  s.c om
 */
public MongoRx doWith(final BiConsumer<MongoClientSettings.Builder, Config> configurer) {
    this.configurer = requireNonNull(configurer, "Configurer is required.");
    return this;
}