From 59f9cfb8a60fc3dbcff8e037df43a2870adb078b Mon Sep 17 00:00:00 2001 From: Nasko Vasilev Date: Thu, 16 Jun 2016 14:50:13 +0200 Subject: [PATCH 1/2] Use missing MongoClientOptions in MongoProperties See gh-6176 --- .../boot/autoconfigure/mongo/MongoProperties.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index ae3f8b643c..8148ad3c3e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -39,6 +39,7 @@ import org.springframework.core.env.Environment; * @author Josh Long * @author Andy Wilkinson * @author EddĂș MelĂ©ndez + * @author Nasko Vasilev */ @ConfigurationProperties(prefix = "spring.data.mongodb") public class MongoProperties { @@ -278,6 +279,15 @@ public class MongoProperties { builder.threadsAllowedToBlockForConnectionMultiplier( options.getThreadsAllowedToBlockForConnectionMultiplier()); builder.writeConcern(options.getWriteConcern()); + builder.minConnectionsPerHost(options.getMinConnectionsPerHost()); + builder.maxConnectionIdleTime(options.getMaxConnectionIdleTime()); + builder.maxConnectionLifeTime(options.getMaxConnectionLifeTime()); + builder.heartbeatFrequency(options.getHeartbeatFrequency()); + builder.minHeartbeatFrequency(options.getMinHeartbeatFrequency()); + builder.heartbeatConnectTimeout(options.getHeartbeatConnectTimeout()); + builder.heartbeatSocketTimeout(options.getHeartbeatSocketTimeout()); + builder.localThreshold(options.getLocalThreshold()); + builder.requiredReplicaSetName(options.getRequiredReplicaSetName()); } return builder; } From 4e07003e1d5b33dc0e83db8c6047399c9b13b6e2 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 27 Jun 2016 15:14:24 +0200 Subject: [PATCH 2/2] Polish "Use missing MongoClientOptions in MongoProperties" Closes gh-6176 --- .../autoconfigure/mongo/MongoProperties.java | 18 +++---- .../mongo/MongoPropertiesTests.java | 51 ++++++++++++++++++- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index 8148ad3c3e..9cc12d94b4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -271,23 +271,23 @@ public class MongoProperties { builder.dbDecoderFactory(options.getDbDecoderFactory()); builder.dbEncoderFactory(options.getDbEncoderFactory()); builder.description(options.getDescription()); + builder.heartbeatConnectTimeout(options.getHeartbeatConnectTimeout()); + builder.heartbeatFrequency(options.getHeartbeatFrequency()); + builder.heartbeatSocketTimeout(options.getHeartbeatSocketTimeout()); + builder.localThreshold(options.getLocalThreshold()); + builder.minConnectionsPerHost(options.getMinConnectionsPerHost()); + builder.minHeartbeatFrequency(options.getMinHeartbeatFrequency()); + builder.maxConnectionIdleTime(options.getMaxConnectionIdleTime()); + builder.maxConnectionLifeTime(options.getMaxConnectionLifeTime()); builder.maxWaitTime(options.getMaxWaitTime()); builder.readPreference(options.getReadPreference()); + builder.requiredReplicaSetName(options.getRequiredReplicaSetName()); builder.socketFactory(options.getSocketFactory()); builder.socketKeepAlive(options.isSocketKeepAlive()); builder.socketTimeout(options.getSocketTimeout()); builder.threadsAllowedToBlockForConnectionMultiplier( options.getThreadsAllowedToBlockForConnectionMultiplier()); builder.writeConcern(options.getWriteConcern()); - builder.minConnectionsPerHost(options.getMinConnectionsPerHost()); - builder.maxConnectionIdleTime(options.getMaxConnectionIdleTime()); - builder.maxConnectionLifeTime(options.getMaxConnectionLifeTime()); - builder.heartbeatFrequency(options.getHeartbeatFrequency()); - builder.minHeartbeatFrequency(options.getMinHeartbeatFrequency()); - builder.heartbeatConnectTimeout(options.getHeartbeatConnectTimeout()); - builder.heartbeatSocketTimeout(options.getHeartbeatSocketTimeout()); - builder.localThreshold(options.getLocalThreshold()); - builder.requiredReplicaSetName(options.getRequiredReplicaSetName()); } return builder; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java index fd6f5dddeb..278ac934e8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.net.UnknownHostException; import java.util.List; import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import org.junit.Test; @@ -120,6 +121,54 @@ public class MongoPropertiesTests { assertMongoCredential(credentialsList.get(0), "user", "secret", "test"); } + @Test + public void allMongoClientOptionsCanBeSet() throws UnknownHostException { + MongoClientOptions mco = MongoClientOptions.builder() + .alwaysUseMBeans(true) + .connectionsPerHost(101) + .connectTimeout(10001) + .cursorFinalizerEnabled(false) + .description("test") + .maxWaitTime(120001) + .socketKeepAlive(true) + .socketTimeout(1000) + .threadsAllowedToBlockForConnectionMultiplier(6) + .minConnectionsPerHost(0) + .maxConnectionIdleTime(60000) + .maxConnectionLifeTime(60000) + .heartbeatFrequency(10001) + .minHeartbeatFrequency(501) + .heartbeatConnectTimeout(20001) + .heartbeatSocketTimeout(20001) + .localThreshold(20) + .requiredReplicaSetName("testReplicaSetName") + .build(); + + MongoProperties properties = new MongoProperties(); + MongoClient client = properties.createMongoClient(mco, null); + MongoClientOptions wrappedMco = client.getMongoClientOptions(); + + assertThat(wrappedMco.isAlwaysUseMBeans(), equalTo(mco.isAlwaysUseMBeans())); + assertThat(wrappedMco.getConnectionsPerHost(), equalTo(mco.getConnectionsPerHost())); + assertThat(wrappedMco.getConnectTimeout(), equalTo(mco.getConnectTimeout())); + assertThat(wrappedMco.isCursorFinalizerEnabled(), equalTo(mco.isCursorFinalizerEnabled())); + assertThat(wrappedMco.getDescription(), equalTo(mco.getDescription())); + assertThat(wrappedMco.getMaxWaitTime(), equalTo(mco.getMaxWaitTime())); + assertThat(wrappedMco.getSocketTimeout(), equalTo(mco.getSocketTimeout())); + assertThat(wrappedMco.isSocketKeepAlive(), equalTo(mco.isSocketKeepAlive())); + assertThat(wrappedMco.getThreadsAllowedToBlockForConnectionMultiplier(), equalTo( + mco.getThreadsAllowedToBlockForConnectionMultiplier())); + assertThat(wrappedMco.getMinConnectionsPerHost(), equalTo(mco.getMinConnectionsPerHost())); + assertThat(wrappedMco.getMaxConnectionIdleTime(), equalTo(mco.getMaxConnectionIdleTime())); + assertThat(wrappedMco.getMaxConnectionLifeTime(), equalTo(mco.getMaxConnectionLifeTime())); + assertThat(wrappedMco.getHeartbeatFrequency(), equalTo(mco.getHeartbeatFrequency())); + assertThat(wrappedMco.getMinHeartbeatFrequency(), equalTo(mco.getMinHeartbeatFrequency())); + assertThat(wrappedMco.getHeartbeatConnectTimeout(), equalTo(mco.getHeartbeatConnectTimeout())); + assertThat(wrappedMco.getHeartbeatSocketTimeout(), equalTo(mco.getHeartbeatSocketTimeout())); + assertThat(wrappedMco.getLocalThreshold(), equalTo(mco.getLocalThreshold())); + assertThat(wrappedMco.getRequiredReplicaSetName(), equalTo(mco.getRequiredReplicaSetName())); + } + private void assertServerAddress(ServerAddress serverAddress, String expectedHost, int expectedPort) { assertThat(serverAddress.getHost(), equalTo(expectedHost));