Search in sources :

Example 1 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.

the class Fixture method getMongoClientBuilderFromConnectionString.

public static MongoClientSettings.Builder getMongoClientBuilderFromConnectionString() {
    SslSettings.Builder sslSettingsBuilder = SslSettings.builder().applyConnectionString(getConnectionString());
    if (System.getProperty("java.version").startsWith("1.6.")) {
        sslSettingsBuilder.invalidHostNameAllowed(true);
    }
    ClusterSettings clusterSettings = ClusterSettings.builder().applyConnectionString(getConnectionString()).build();
    ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings.builder().applyConnectionString(getConnectionString()).build();
    SocketSettings socketSettings = SocketSettings.builder().applyConnectionString(getConnectionString()).build();
    return MongoClientSettings.builder().clusterSettings(clusterSettings).connectionPoolSettings(connectionPoolSettings).serverSettings(ServerSettings.builder().build()).credentialList(getConnectionString().getCredentialList()).sslSettings(sslSettingsBuilder.build()).socketSettings(socketSettings);
}
Also used : SslSettings(com.mongodb.connection.SslSettings) ClusterSettings(com.mongodb.connection.ClusterSettings) SocketSettings(com.mongodb.connection.SocketSettings) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings)

Example 2 with ClusterSettings

use of com.mongodb.connection.ClusterSettings 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 3 with ClusterSettings

use of com.mongodb.connection.ClusterSettings 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)

Example 4 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project spring-boot by spring-projects.

the class ReactiveMongoClientFactoryTests method extractServerAddresses.

private List<ServerAddress> extractServerAddresses(MongoClient client) {
    MongoClientSettings settings = client.getSettings();
    ClusterSettings clusterSettings = settings.getClusterSettings();
    List<ServerAddress> allAddresses = clusterSettings.getHosts();
    return allAddresses;
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.async.client.MongoClientSettings)

Example 5 with ClusterSettings

use of com.mongodb.connection.ClusterSettings in project spring-boot by spring-projects.

the class MongoClientFactoryTests method extractServerAddresses.

private List<ServerAddress> extractServerAddresses(MongoClient client) {
    Cluster cluster = (Cluster) ReflectionTestUtils.getField(client, "cluster");
    ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils.getField(cluster, "settings");
    List<ServerAddress> allAddresses = clusterSettings.getHosts();
    return allAddresses;
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) Cluster(com.mongodb.connection.Cluster)

Aggregations

ClusterSettings (com.mongodb.connection.ClusterSettings)24 ServerAddress (com.mongodb.ServerAddress)18 ClusterId (com.mongodb.connection.ClusterId)15 RepeatedTest (org.junit.jupiter.api.RepeatedTest)12 Test (org.junit.jupiter.api.Test)10 FutureResultCallback (com.mongodb.async.FutureResultCallback)6 ServerSelector (com.mongodb.selector.ServerSelector)6 MongoTimeoutException (com.mongodb.MongoTimeoutException)5 ConnectionString (com.mongodb.ConnectionString)4 MongoClientSettings (com.mongodb.async.client.MongoClientSettings)4 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)3 ServerSettings (com.mongodb.connection.ServerSettings)3 SocketSettings (com.mongodb.connection.SocketSettings)3 ArrayList (java.util.ArrayList)3 MongoClientException (com.mongodb.MongoClientException)2 MongoConfigurationException (com.mongodb.MongoConfigurationException)2 MongoCredential (com.mongodb.MongoCredential)2 Builder (com.mongodb.async.client.MongoClientSettings.Builder)2 SslSettings (com.mongodb.connection.SslSettings)2 Duration (java.time.Duration)2