Skip to content

Commit 3af2d81

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 a4c7544 commit 3af2d81

File tree

6 files changed

+957
-7
lines changed

6 files changed

+957
-7
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: 24 additions & 5 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.
@@ -18,8 +18,10 @@
1818
import javax.net.SocketFactory;
1919
import javax.net.ssl.SSLSocketFactory;
2020

21+
import org.springframework.beans.DirectFieldAccessor;
2122
import org.springframework.beans.factory.config.AbstractFactoryBean;
2223
import org.springframework.data.mongodb.MongoDbFactory;
24+
import org.springframework.data.mongodb.util.MongoClientVersion;
2325

2426
import com.mongodb.DBDecoderFactory;
2527
import com.mongodb.DBEncoderFactory;
@@ -62,6 +64,7 @@ public class MongoClientOptionsFactoryBean extends AbstractFactoryBean<MongoClie
6264
private int heartbeatConnectTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatConnectTimeout();
6365
private int heartbeatSocketTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatSocketTimeout();
6466
private String requiredReplicaSetName = DEFAULT_MONGO_OPTIONS.getRequiredReplicaSetName();
67+
private int serverSelectionTimeout = -1;
6568

6669
private boolean ssl;
6770
private SSLSocketFactory sslSocketFactory;
@@ -250,17 +253,33 @@ public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
250253
this.sslSocketFactory = sslSocketFactory;
251254
}
252255

253-
/*
256+
/**
257+
* 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.
259+
*
260+
* @param serverSelectionTimeout in msec.
261+
*/
262+
public void setServerSelectionTimeout(int serverSelectionTimeout) {
263+
this.serverSelectionTimeout = serverSelectionTimeout;
264+
}
265+
266+
/*
254267
* (non-Javadoc)
255268
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
256269
*/
257270
@Override
258271
protected MongoClientOptions createInstance() throws Exception {
259272

260-
SocketFactory socketFactoryToUse = ssl ? (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory
261-
.getDefault()) : this.socketFactory;
273+
SocketFactory socketFactoryToUse = ssl
274+
? (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()) : this.socketFactory;
275+
276+
MongoClientOptions.Builder builder = MongoClientOptions.builder();
277+
278+
if (MongoClientVersion.isMongo3Driver() && serverSelectionTimeout > 0) {
279+
new DirectFieldAccessor(builder).setPropertyValue("serverSelectionTimeout", serverSelectionTimeout);
280+
}
262281

263-
return MongoClientOptions.builder() //
282+
return builder //
264283
.alwaysUseMBeans(this.alwaysUseMBeans) //
265284
.connectionsPerHost(this.connectionsPerHost) //
266285
.connectTimeout(connectTimeout) //

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)