Search in sources :

Example 1 with Builder

use of com.mongodb.async.client.MongoClientSettings.Builder in project spring-boot by spring-projects.

the class ReactiveMongoClientFactory method createNetworkMongoClient.

private MongoClient createNetworkMongoClient(MongoClientSettings settings) {
    if (hasCustomAddress() || hasCustomCredentials()) {
        if (this.properties.getUri() != null) {
            throw new IllegalStateException("Invalid mongo configuration, " + "either uri or host/port/credentials must be specified");
        }
        Builder builder = builder(settings);
        if (hasCustomCredentials()) {
            List<MongoCredential> credentials = new ArrayList<>();
            String database = this.properties.getAuthenticationDatabase() == null ? this.properties.getMongoClientDatabase() : this.properties.getAuthenticationDatabase();
            credentials.add(MongoCredential.createCredential(this.properties.getUsername(), database, this.properties.getPassword()));
            builder.credentialList(credentials);
        }
        String host = this.properties.getHost() == null ? "localhost" : this.properties.getHost();
        int port = this.properties.getPort() != null ? this.properties.getPort() : MongoProperties.DEFAULT_PORT;
        ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(host, port))).build();
        builder.clusterSettings(clusterSettings);
        return MongoClients.create(builder.build());
    }
    ConnectionString connectionString = new ConnectionString(this.properties.determineUri());
    return MongoClients.create(createBuilder(settings, connectionString).build());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) MongoCredential(com.mongodb.MongoCredential) Builder(com.mongodb.async.client.MongoClientSettings.Builder) ArrayList(java.util.ArrayList) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Example 2 with Builder

use of com.mongodb.async.client.MongoClientSettings.Builder in project spring-boot by spring-projects.

the class ReactiveMongoClientFactory method createEmbeddedMongoClient.

private MongoClient createEmbeddedMongoClient(MongoClientSettings settings, int port) {
    Builder builder = builder(settings);
    String host = this.properties.getHost() == null ? "localhost" : this.properties.getHost();
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(host, port))).build();
    builder.clusterSettings(clusterSettings);
    return MongoClients.create(builder.build());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) Builder(com.mongodb.async.client.MongoClientSettings.Builder) ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString)

Aggregations

ConnectionString (com.mongodb.ConnectionString)2 ServerAddress (com.mongodb.ServerAddress)2 Builder (com.mongodb.async.client.MongoClientSettings.Builder)2 ClusterSettings (com.mongodb.connection.ClusterSettings)2 MongoCredential (com.mongodb.MongoCredential)1 ArrayList (java.util.ArrayList)1