Search in sources :

Example 26 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoClientSettingsFactoryBean method createInstance.

@Override
protected MongoClientSettings createInstance() {
    Builder builder = // 
    MongoClientSettings.builder().readPreference(// 
    readPreference).writeConcern(// 
    writeConcern).readConcern(// 
    readConcern).codecRegistry(// 
    codecRegistry).applicationName(// 
    applicationName).autoEncryptionSettings(// 
    autoEncryptionSettings).applyToClusterSettings((settings) -> {
        settings.serverSelectionTimeout(clusterServerSelectionTimeoutMS, TimeUnit.MILLISECONDS);
        if (clusterConnectionMode != null) {
            settings.mode(clusterConnectionMode);
        }
        settings.requiredReplicaSetName(clusterRequiredReplicaSetName);
        if (!CollectionUtils.isEmpty(clusterHosts)) {
            settings.hosts(clusterHosts);
        }
        settings.localThreshold(clusterLocalThresholdMS, TimeUnit.MILLISECONDS);
        // settings.maxWaitQueueSize(clusterMaxWaitQueueSize);
        settings.requiredClusterType(custerRequiredClusterType);
        if (StringUtils.hasText(clusterSrvHost)) {
            settings.srvHost(clusterSrvHost);
        }
    }).applyToConnectionPoolSettings((settings) -> {
        settings.minSize(poolMinSize);
        settings.maxSize(poolMaxSize);
        settings.maxConnectionIdleTime(poolMaxConnectionIdleTimeMS, TimeUnit.MILLISECONDS);
        settings.maxWaitTime(poolMaxWaitTimeMS, TimeUnit.MILLISECONDS);
        settings.maxConnectionLifeTime(poolMaxConnectionLifeTimeMS, TimeUnit.MILLISECONDS);
        // settings.maxWaitQueueSize(poolMaxWaitQueueSize);
        settings.maintenanceFrequency(poolMaintenanceFrequencyMS, TimeUnit.MILLISECONDS);
        settings.maintenanceInitialDelay(poolMaintenanceInitialDelayMS, TimeUnit.MILLISECONDS);
    }).applyToServerSettings((settings) -> {
        settings.minHeartbeatFrequency(serverMinHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
        settings.heartbeatFrequency(serverHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
    }).applyToSocketSettings((settings) -> {
        settings.connectTimeout(socketConnectTimeoutMS, TimeUnit.MILLISECONDS);
        settings.readTimeout(socketReadTimeoutMS, TimeUnit.MILLISECONDS);
        settings.receiveBufferSize(socketReceiveBufferSize);
        settings.sendBufferSize(socketSendBufferSize);
    }).applyToSslSettings((settings) -> {
        settings.enabled(sslEnabled);
        if (sslEnabled) {
            settings.invalidHostNameAllowed(sslInvalidHostNameAllowed);
            try {
                settings.context(StringUtils.hasText(sslProvider) ? SSLContext.getInstance(sslProvider) : SSLContext.getDefault());
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
        }
    });
    if (streamFactoryFactory != null) {
        builder = builder.streamFactoryFactory(streamFactoryFactory);
    }
    if (retryReads != null) {
        builder = builder.retryReads(retryReads);
    }
    if (retryWrites != null) {
        builder = builder.retryWrites(retryWrites);
    }
    if (uUidRepresentation != null) {
        builder = builder.uuidRepresentation(uUidRepresentation);
    }
    if (serverApi != null) {
        builder = builder.serverApi(serverApi);
    }
    return builder.build();
}
Also used : ReadPreference(com.mongodb.ReadPreference) ServerAddress(com.mongodb.ServerAddress) Arrays(java.util.Arrays) SSLContext(javax.net.ssl.SSLContext) ServerApi(com.mongodb.ServerApi) ClusterType(com.mongodb.connection.ClusterType) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) UuidRepresentation(org.bson.UuidRepresentation) AbstractFactoryBean(org.springframework.beans.factory.config.AbstractFactoryBean) TimeUnit(java.util.concurrent.TimeUnit) Builder(com.mongodb.MongoClientSettings.Builder) StreamFactoryFactory(com.mongodb.connection.StreamFactoryFactory) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Nullable(org.springframework.lang.Nullable) MongoClientSettings(com.mongodb.MongoClientSettings) ReadConcern(com.mongodb.ReadConcern) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Builder(com.mongodb.MongoClientSettings.Builder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 27 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoClientNamespaceTests method clientWithServerVersion.

// GH-3820
@Test
public void clientWithServerVersion() {
    assertThat(ctx.containsBean("client-with-server-api-settings")).isTrue();
    MongoClientFactoryBean factoryBean = ctx.getBean("&client-with-server-api-settings", MongoClientFactoryBean.class);
    MongoClientSettings settings = (MongoClientSettings) getField(factoryBean, "mongoClientSettings");
    assertThat(settings.getServerApi()).isNotNull().satisfies(it -> {
        assertThat(it.getVersion()).isEqualTo(ServerApiVersion.V1);
    });
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.Test)

Example 28 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoNamespaceTests method testMongoSingletonWithSslEnabledAndCustomSslSocketFactory.

// DATAMONGO-764
@Test
public void testMongoSingletonWithSslEnabledAndCustomSslSocketFactory() throws Exception {
    assertThat(ctx.containsBean("mongoSslWithCustomSslFactory")).isTrue();
    MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&mongoSslWithCustomSslFactory");
    MongoClientSettings options = (MongoClientSettings) getField(mfb, "mongoClientSettings");
    assertThat(options.getSslSettings().getContext().getSocketFactory() instanceof SSLSocketFactory).as("socketFactory should be a SSLSocketFactory").isTrue();
    assertThat(options.getSslSettings().getContext().getProvider().getName()).isEqualTo("SunJSSE");
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) MongoClientSettings(com.mongodb.MongoClientSettings) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 29 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoNamespaceTests method testMongoClientSingletonWithSslEnabled.

// DATAMONGO-1490
@Test
public void testMongoClientSingletonWithSslEnabled() {
    assertThat(ctx.containsBean("mongoClientSsl")).isTrue();
    MongoClientFactoryBean mfb = (MongoClientFactoryBean) ctx.getBean("&mongoClientSsl");
    MongoClientSettings options = (MongoClientSettings) getField(mfb, "mongoClientSettings");
    assertThat(options.getSslSettings().getContext().getSocketFactory() instanceof SSLSocketFactory).as("socketFactory should be a SSLSocketFactory").isTrue();
}
Also used : MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) MongoClientSettings(com.mongodb.MongoClientSettings) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 30 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project spring-data-mongodb by spring-projects.

the class MongoClientFactoryBeanUnitTests method hostPortParametersNotOverriddenByDefaults.

// DATAMONGO-2427
@Test
void hostPortParametersNotOverriddenByDefaults() {
    MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
    factoryBean.setPort(2500);
    factoryBean.setHost("db2.example.net");
    factoryBean.setReplicaSet("rs0");
    factoryBean.setMongoClientSettings(MongoClientSettings.builder().build());
    MongoClientSettings settings = factoryBean.computeClientSetting();
    assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("rs0");
    assertThat(settings.getClusterSettings().getHosts()).containsExactly(new ServerAddress("db2.example.net", 2500));
}
Also used : ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.jupiter.api.Test)

Aggregations

MongoClientSettings (com.mongodb.MongoClientSettings)75 Test (org.junit.jupiter.api.Test)24 BsonString (org.bson.BsonString)21 Map (java.util.Map)20 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 Document (org.bson.Document)19 BsonDocument (org.bson.BsonDocument)18 ServerAddress (com.mongodb.ServerAddress)17 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)14 Before (org.junit.Before)14 MongoNamespace (com.mongodb.MongoNamespace)13 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)12 ConnectionString (com.mongodb.ConnectionString)12 MongoClientFactoryBean (org.springframework.data.mongodb.core.MongoClientFactoryBean)10 Block (com.mongodb.Block)8 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)8 SecureRandom (java.security.SecureRandom)8 TimeUnit (java.util.concurrent.TimeUnit)8 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)7