Skip to content

Commit 4b8d352

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1620 - Add server-selection-timeout to XML MongoClientOptions config.
We now allow server-selection-timeout attribute on MongoClientOptions XML configuration for a MongoDB 3.x client. Original pull request: #449.
1 parent b486fec commit 4b8d352

File tree

6 files changed

+969
-26
lines changed

6 files changed

+969
-26
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -129,6 +129,7 @@ public static boolean parseMongoClientOptions(Element element, BeanDefinitionBui
129129
setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-socket-timeout", "heartbeatSocketTimeout");
130130
setPropertyValue(clientOptionsDefBuilder, optionsElement, "ssl", "ssl");
131131
setPropertyReference(clientOptionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory");
132+
setPropertyValue(clientOptionsDefBuilder, optionsElement, "server-selection-timeout", "serverSelectionTimeout");
132133

133134
mongoClientBuilder.addPropertyValue("mongoClientOptions", clientOptionsDefBuilder.getBeanDefinition());
134135

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

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,9 +30,10 @@
3030

3131
/**
3232
* A factory bean for construction of a {@link MongoClientOptions} instance.
33-
*
33+
*
3434
* @author Christoph Strobl
3535
* @author Oliver Gierke
36+
* @author Mark Paluch
3637
* @since 1.7
3738
*/
3839
public class MongoClientOptionsFactoryBean extends AbstractFactoryBean<MongoClientOptions> {
@@ -62,13 +63,14 @@ public class MongoClientOptionsFactoryBean extends AbstractFactoryBean<MongoClie
6263
private int heartbeatConnectTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatConnectTimeout();
6364
private int heartbeatSocketTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatSocketTimeout();
6465
private String requiredReplicaSetName = DEFAULT_MONGO_OPTIONS.getRequiredReplicaSetName();
66+
private int serverSelectionTimeout = DEFAULT_MONGO_OPTIONS.getServerSelectionTimeout();
6567

6668
private boolean ssl;
6769
private SSLSocketFactory sslSocketFactory;
6870

6971
/**
7072
* Set the {@link MongoClient} description.
71-
*
73+
*
7274
* @param description
7375
*/
7476
public void setDescription(String description) {
@@ -77,7 +79,7 @@ public void setDescription(String description) {
7779

7880
/**
7981
* Set the minimum number of connections per host.
80-
*
82+
*
8183
* @param minConnectionsPerHost
8284
*/
8385
public void setMinConnectionsPerHost(int minConnectionsPerHost) {
@@ -87,7 +89,7 @@ public void setMinConnectionsPerHost(int minConnectionsPerHost) {
8789
/**
8890
* Set the number of connections allowed per host. Will block if run out. Default is 10. System property
8991
* {@code MONGO.POOLSIZE} can override
90-
*
92+
*
9193
* @param connectionsPerHost
9294
*/
9395
public void setConnectionsPerHost(int connectionsPerHost) {
@@ -98,7 +100,7 @@ public void setConnectionsPerHost(int connectionsPerHost) {
98100
* Set the multiplier for connectionsPerHost for # of threads that can block. Default is 5. If connectionsPerHost is
99101
* 10, and threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an
100102
* exception will be thrown.
101-
*
103+
*
102104
* @param threadsAllowedToBlockForConnectionMultiplier
103105
*/
104106
public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedToBlockForConnectionMultiplier) {
@@ -107,7 +109,7 @@ public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedTo
107109

108110
/**
109111
* Set the max wait time of a blocking thread for a connection. Default is 12000 ms (2 minutes)
110-
*
112+
*
111113
* @param maxWaitTime
112114
*/
113115
public void setMaxWaitTime(int maxWaitTime) {
@@ -116,7 +118,7 @@ public void setMaxWaitTime(int maxWaitTime) {
116118

117119
/**
118120
* The maximum idle time for a pooled connection.
119-
*
121+
*
120122
* @param maxConnectionIdleTime
121123
*/
122124
public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {
@@ -125,7 +127,7 @@ public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {
125127

126128
/**
127129
* Set the maximum life time for a pooled connection.
128-
*
130+
*
129131
* @param maxConnectionLifeTime
130132
*/
131133
public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {
@@ -134,7 +136,7 @@ public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {
134136

135137
/**
136138
* Set the connect timeout in milliseconds. 0 is default and infinite.
137-
*
139+
*
138140
* @param connectTimeout
139141
*/
140142
public void setConnectTimeout(int connectTimeout) {
@@ -143,7 +145,7 @@ public void setConnectTimeout(int connectTimeout) {
143145

144146
/**
145147
* Set the socket timeout. 0 is default and infinite.
146-
*
148+
*
147149
* @param socketTimeout
148150
*/
149151
public void setSocketTimeout(int socketTimeout) {
@@ -152,7 +154,7 @@ public void setSocketTimeout(int socketTimeout) {
152154

153155
/**
154156
* Set the keep alive flag, controls whether or not to have socket keep alive timeout. Defaults to false.
155-
*
157+
*
156158
* @param socketKeepAlive
157159
*/
158160
public void setSocketKeepAlive(boolean socketKeepAlive) {
@@ -161,7 +163,7 @@ public void setSocketKeepAlive(boolean socketKeepAlive) {
161163

162164
/**
163165
* Set the {@link ReadPreference}.
164-
*
166+
*
165167
* @param readPreference
166168
*/
167169
public void setReadPreference(ReadPreference readPreference) {
@@ -171,7 +173,7 @@ public void setReadPreference(ReadPreference readPreference) {
171173
/**
172174
* Set the {@link WriteConcern} that will be the default value used when asking the {@link MongoDbFactory} for a DB
173175
* object.
174-
*
176+
*
175177
* @param writeConcern
176178
*/
177179
public void setWriteConcern(WriteConcern writeConcern) {
@@ -187,7 +189,7 @@ public void setSocketFactory(SocketFactory socketFactory) {
187189

188190
/**
189191
* Set the frequency that the driver will attempt to determine the current state of each server in the cluster.
190-
*
192+
*
191193
* @param heartbeatFrequency
192194
*/
193195
public void setHeartbeatFrequency(int heartbeatFrequency) {
@@ -197,7 +199,7 @@ public void setHeartbeatFrequency(int heartbeatFrequency) {
197199
/**
198200
* In the event that the driver has to frequently re-check a server's availability, it will wait at least this long
199201
* since the previous check to avoid wasted effort.
200-
*
202+
*
201203
* @param minHeartbeatFrequency
202204
*/
203205
public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {
@@ -206,7 +208,7 @@ public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {
206208

207209
/**
208210
* Set the connect timeout for connections used for the cluster heartbeat.
209-
*
211+
*
210212
* @param heartbeatConnectTimeout
211213
*/
212214
public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {
@@ -215,7 +217,7 @@ public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {
215217

216218
/**
217219
* Set the socket timeout for connections used for the cluster heartbeat.
218-
*
220+
*
219221
* @param heartbeatSocketTimeout
220222
*/
221223
public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {
@@ -224,7 +226,7 @@ public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {
224226

225227
/**
226228
* Configures the name of the replica set.
227-
*
229+
*
228230
* @param requiredReplicaSetName
229231
*/
230232
public void setRequiredReplicaSetName(String requiredReplicaSetName) {
@@ -233,7 +235,7 @@ public void setRequiredReplicaSetName(String requiredReplicaSetName) {
233235

234236
/**
235237
* This controls if the driver should us an SSL connection. Defaults to |@literal false}.
236-
*
238+
*
237239
* @param ssl
238240
*/
239241
public void setSsl(boolean ssl) {
@@ -243,22 +245,32 @@ public void setSsl(boolean ssl) {
243245
/**
244246
* Set the {@link SSLSocketFactory} to use for the {@literal SSL} connection. If none is configured here,
245247
* {@link SSLSocketFactory#getDefault()} will be used.
246-
*
248+
*
247249
* @param sslSocketFactory
248250
*/
249251
public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
250252
this.sslSocketFactory = sslSocketFactory;
251253
}
252254

253-
/*
255+
/**
256+
* Set the {@literal server selection timeout} in msec for a 3.x MongoDB Java driver. If not set the default value of
257+
* 30 sec will be used.
258+
*
259+
* @param serverSelectionTimeout in msec.
260+
*/
261+
public void setServerSelectionTimeout(int serverSelectionTimeout) {
262+
this.serverSelectionTimeout = serverSelectionTimeout;
263+
}
264+
265+
/*
254266
* (non-Javadoc)
255267
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
256268
*/
257269
@Override
258270
protected MongoClientOptions createInstance() throws Exception {
259271

260-
SocketFactory socketFactoryToUse = ssl ? (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory
261-
.getDefault()) : this.socketFactory;
272+
SocketFactory socketFactoryToUse = ssl
273+
? (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()) : this.socketFactory;
262274

263275
return MongoClientOptions.builder() //
264276
.alwaysUseMBeans(this.alwaysUseMBeans) //
@@ -278,6 +290,7 @@ protected MongoClientOptions createInstance() throws Exception {
278290
.minHeartbeatFrequency(minHeartbeatFrequency) //
279291
.readPreference(readPreference) //
280292
.requiredReplicaSetName(requiredReplicaSetName) //
293+
.serverSelectionTimeout(serverSelectionTimeout) //
281294
.socketFactory(socketFactoryToUse) //
282295
.socketKeepAlive(socketKeepAlive) //
283296
.socketTimeout(socketTimeout) //

spring-data-mongodb/src/main/resources/META-INF/spring.schemas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ http\://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd=org/sprin
77
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd=org/springframework/data/mongodb/config/spring-mongo-1.7.xsd
88
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd=org/springframework/data/mongodb/config/spring-mongo-1.8.xsd
99
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.10.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.xsd
10-
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.xsd
10+
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.10.2.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.2.xsd
11+
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.2.xsd

0 commit comments

Comments
 (0)