Skip to content

Commit 3207751

Browse files
committed
DATAMONGO-1620 - Polishing.
Use Integer.MIN_VALUE as placeholder for unset values to allow setting zero (immediately) and -1 (indefinite wait) server selection timeouts. Fix test method name. Add JavaDoc. Original pull request: #449.
1 parent 7f2dcee commit 3207751

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoClientOptionsFactoryBean.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* A factory bean for construction of a {@link MongoClientOptions} instance.
35-
*
35+
*
3636
* @author Christoph Strobl
3737
* @author Oliver Gierke
3838
* @since 1.7
@@ -64,14 +64,14 @@ public class MongoClientOptionsFactoryBean extends AbstractFactoryBean<MongoClie
6464
private int heartbeatConnectTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatConnectTimeout();
6565
private int heartbeatSocketTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatSocketTimeout();
6666
private String requiredReplicaSetName = DEFAULT_MONGO_OPTIONS.getRequiredReplicaSetName();
67-
private int serverSelectionTimeout = -1;
67+
private int serverSelectionTimeout = Integer.MIN_VALUE;
6868

6969
private boolean ssl;
7070
private SSLSocketFactory sslSocketFactory;
7171

7272
/**
7373
* Set the {@link MongoClient} description.
74-
*
74+
*
7575
* @param description
7676
*/
7777
public void setDescription(String description) {
@@ -80,7 +80,7 @@ public void setDescription(String description) {
8080

8181
/**
8282
* Set the minimum number of connections per host.
83-
*
83+
*
8484
* @param minConnectionsPerHost
8585
*/
8686
public void setMinConnectionsPerHost(int minConnectionsPerHost) {
@@ -90,7 +90,7 @@ public void setMinConnectionsPerHost(int minConnectionsPerHost) {
9090
/**
9191
* Set the number of connections allowed per host. Will block if run out. Default is 10. System property
9292
* {@code MONGO.POOLSIZE} can override
93-
*
93+
*
9494
* @param connectionsPerHost
9595
*/
9696
public void setConnectionsPerHost(int connectionsPerHost) {
@@ -101,7 +101,7 @@ public void setConnectionsPerHost(int connectionsPerHost) {
101101
* Set the multiplier for connectionsPerHost for # of threads that can block. Default is 5. If connectionsPerHost is
102102
* 10, and threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an
103103
* exception will be thrown.
104-
*
104+
*
105105
* @param threadsAllowedToBlockForConnectionMultiplier
106106
*/
107107
public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedToBlockForConnectionMultiplier) {
@@ -110,7 +110,7 @@ public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedTo
110110

111111
/**
112112
* Set the max wait time of a blocking thread for a connection. Default is 12000 ms (2 minutes)
113-
*
113+
*
114114
* @param maxWaitTime
115115
*/
116116
public void setMaxWaitTime(int maxWaitTime) {
@@ -119,7 +119,7 @@ public void setMaxWaitTime(int maxWaitTime) {
119119

120120
/**
121121
* The maximum idle time for a pooled connection.
122-
*
122+
*
123123
* @param maxConnectionIdleTime
124124
*/
125125
public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {
@@ -128,7 +128,7 @@ public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {
128128

129129
/**
130130
* Set the maximum life time for a pooled connection.
131-
*
131+
*
132132
* @param maxConnectionLifeTime
133133
*/
134134
public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {
@@ -137,7 +137,7 @@ public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {
137137

138138
/**
139139
* Set the connect timeout in milliseconds. 0 is default and infinite.
140-
*
140+
*
141141
* @param connectTimeout
142142
*/
143143
public void setConnectTimeout(int connectTimeout) {
@@ -146,7 +146,7 @@ public void setConnectTimeout(int connectTimeout) {
146146

147147
/**
148148
* Set the socket timeout. 0 is default and infinite.
149-
*
149+
*
150150
* @param socketTimeout
151151
*/
152152
public void setSocketTimeout(int socketTimeout) {
@@ -155,7 +155,7 @@ public void setSocketTimeout(int socketTimeout) {
155155

156156
/**
157157
* Set the keep alive flag, controls whether or not to have socket keep alive timeout. Defaults to false.
158-
*
158+
*
159159
* @param socketKeepAlive
160160
*/
161161
public void setSocketKeepAlive(boolean socketKeepAlive) {
@@ -164,7 +164,7 @@ public void setSocketKeepAlive(boolean socketKeepAlive) {
164164

165165
/**
166166
* Set the {@link ReadPreference}.
167-
*
167+
*
168168
* @param readPreference
169169
*/
170170
public void setReadPreference(ReadPreference readPreference) {
@@ -174,7 +174,7 @@ public void setReadPreference(ReadPreference readPreference) {
174174
/**
175175
* Set the {@link WriteConcern} that will be the default value used when asking the {@link MongoDbFactory} for a DB
176176
* object.
177-
*
177+
*
178178
* @param writeConcern
179179
*/
180180
public void setWriteConcern(WriteConcern writeConcern) {
@@ -190,7 +190,7 @@ public void setSocketFactory(SocketFactory socketFactory) {
190190

191191
/**
192192
* Set the frequency that the driver will attempt to determine the current state of each server in the cluster.
193-
*
193+
*
194194
* @param heartbeatFrequency
195195
*/
196196
public void setHeartbeatFrequency(int heartbeatFrequency) {
@@ -200,7 +200,7 @@ public void setHeartbeatFrequency(int heartbeatFrequency) {
200200
/**
201201
* In the event that the driver has to frequently re-check a server's availability, it will wait at least this long
202202
* since the previous check to avoid wasted effort.
203-
*
203+
*
204204
* @param minHeartbeatFrequency
205205
*/
206206
public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {
@@ -209,7 +209,7 @@ public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {
209209

210210
/**
211211
* Set the connect timeout for connections used for the cluster heartbeat.
212-
*
212+
*
213213
* @param heartbeatConnectTimeout
214214
*/
215215
public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {
@@ -218,7 +218,7 @@ public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {
218218

219219
/**
220220
* Set the socket timeout for connections used for the cluster heartbeat.
221-
*
221+
*
222222
* @param heartbeatSocketTimeout
223223
*/
224224
public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {
@@ -227,7 +227,7 @@ public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {
227227

228228
/**
229229
* Configures the name of the replica set.
230-
*
230+
*
231231
* @param requiredReplicaSetName
232232
*/
233233
public void setRequiredReplicaSetName(String requiredReplicaSetName) {
@@ -236,7 +236,7 @@ public void setRequiredReplicaSetName(String requiredReplicaSetName) {
236236

237237
/**
238238
* This controls if the driver should us an SSL connection. Defaults to |@literal false}.
239-
*
239+
*
240240
* @param ssl
241241
*/
242242
public void setSsl(boolean ssl) {
@@ -246,7 +246,7 @@ public void setSsl(boolean ssl) {
246246
/**
247247
* Set the {@link SSLSocketFactory} to use for the {@literal SSL} connection. If none is configured here,
248248
* {@link SSLSocketFactory#getDefault()} will be used.
249-
*
249+
*
250250
* @param sslSocketFactory
251251
*/
252252
public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
@@ -255,7 +255,8 @@ public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
255255

256256
/**
257257
* Set the {@literal server selection timeout} in msec for a 3.x MongoDB Java driver. If not set the default value of
258-
* 30 sec will be used.
258+
* 30 sec will be used. A value of 0 means that it will timeout immediately if no server is available. A negative
259+
* value means to wait indefinitely.
259260
*
260261
* @param serverSelectionTimeout in msec.
261262
*/
@@ -275,7 +276,7 @@ protected MongoClientOptions createInstance() throws Exception {
275276

276277
MongoClientOptions.Builder builder = MongoClientOptions.builder();
277278

278-
if (MongoClientVersion.isMongo3Driver() && serverSelectionTimeout > 0) {
279+
if (MongoClientVersion.isMongo3Driver() && serverSelectionTimeout != Integer.MIN_VALUE) {
279280
new DirectFieldAccessor(builder).setPropertyValue("serverSelectionTimeout", serverSelectionTimeout);
280281
}
281282

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoClientParserIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
/**
4141
* Integration tests for {@link MongoClientParser}.
42-
*
42+
*
4343
* @author Christoph Strobl
4444
*/
4545
public class MongoClientParserIntegrationTests {
@@ -118,7 +118,7 @@ public void createsMongoClientWithCredentialsCorrectly() {
118118
}
119119

120120
@Test // DATAMONGO-1620
121-
public void createsMongoClinetWithServerSelectionTimeoutCorrectly() {
121+
public void createsMongoClientWithServerSelectionTimeoutCorrectly() {
122122

123123
assumeThat(MongoClientVersion.isMongo3Driver(), is(true));
124124

0 commit comments

Comments
 (0)