Skip to content

DATAMONGO-1620 - Add server-selection-timeout to XML MongoClientOptions config. #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1620-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1620-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1620-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1620-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1620-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1620-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2015 the original author or authors.
* Copyright 2011-2017 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.
Expand Down Expand Up @@ -129,6 +129,7 @@ public static boolean parseMongoClientOptions(Element element, BeanDefinitionBui
setPropertyValue(clientOptionsDefBuilder, optionsElement, "heartbeat-socket-timeout", "heartbeatSocketTimeout");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "ssl", "ssl");
setPropertyReference(clientOptionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory");
setPropertyValue(clientOptionsDefBuilder, optionsElement, "server-selection-timeout", "serverSelectionTimeout");

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2017 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.
Expand All @@ -18,8 +18,10 @@
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.util.MongoClientVersion;

import com.mongodb.DBDecoderFactory;
import com.mongodb.DBEncoderFactory;
Expand All @@ -30,7 +32,7 @@

/**
* A factory bean for construction of a {@link MongoClientOptions} instance.
*
*
* @author Christoph Strobl
* @author Oliver Gierke
* @since 1.7
Expand Down Expand Up @@ -62,13 +64,14 @@ public class MongoClientOptionsFactoryBean extends AbstractFactoryBean<MongoClie
private int heartbeatConnectTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatConnectTimeout();
private int heartbeatSocketTimeout = DEFAULT_MONGO_OPTIONS.getHeartbeatSocketTimeout();
private String requiredReplicaSetName = DEFAULT_MONGO_OPTIONS.getRequiredReplicaSetName();
private int serverSelectionTimeout = Integer.MIN_VALUE;

private boolean ssl;
private SSLSocketFactory sslSocketFactory;

/**
* Set the {@link MongoClient} description.
*
*
* @param description
*/
public void setDescription(String description) {
Expand All @@ -77,7 +80,7 @@ public void setDescription(String description) {

/**
* Set the minimum number of connections per host.
*
*
* @param minConnectionsPerHost
*/
public void setMinConnectionsPerHost(int minConnectionsPerHost) {
Expand All @@ -87,7 +90,7 @@ public void setMinConnectionsPerHost(int minConnectionsPerHost) {
/**
* Set the number of connections allowed per host. Will block if run out. Default is 10. System property
* {@code MONGO.POOLSIZE} can override
*
*
* @param connectionsPerHost
*/
public void setConnectionsPerHost(int connectionsPerHost) {
Expand All @@ -98,7 +101,7 @@ public void setConnectionsPerHost(int connectionsPerHost) {
* Set the multiplier for connectionsPerHost for # of threads that can block. Default is 5. If connectionsPerHost is
* 10, and threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an
* exception will be thrown.
*
*
* @param threadsAllowedToBlockForConnectionMultiplier
*/
public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedToBlockForConnectionMultiplier) {
Expand All @@ -107,7 +110,7 @@ public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedTo

/**
* Set the max wait time of a blocking thread for a connection. Default is 12000 ms (2 minutes)
*
*
* @param maxWaitTime
*/
public void setMaxWaitTime(int maxWaitTime) {
Expand All @@ -116,7 +119,7 @@ public void setMaxWaitTime(int maxWaitTime) {

/**
* The maximum idle time for a pooled connection.
*
*
* @param maxConnectionIdleTime
*/
public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {
Expand All @@ -125,7 +128,7 @@ public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {

/**
* Set the maximum life time for a pooled connection.
*
*
* @param maxConnectionLifeTime
*/
public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {
Expand All @@ -134,7 +137,7 @@ public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {

/**
* Set the connect timeout in milliseconds. 0 is default and infinite.
*
*
* @param connectTimeout
*/
public void setConnectTimeout(int connectTimeout) {
Expand All @@ -143,7 +146,7 @@ public void setConnectTimeout(int connectTimeout) {

/**
* Set the socket timeout. 0 is default and infinite.
*
*
* @param socketTimeout
*/
public void setSocketTimeout(int socketTimeout) {
Expand All @@ -152,7 +155,7 @@ public void setSocketTimeout(int socketTimeout) {

/**
* Set the keep alive flag, controls whether or not to have socket keep alive timeout. Defaults to false.
*
*
* @param socketKeepAlive
*/
public void setSocketKeepAlive(boolean socketKeepAlive) {
Expand All @@ -161,7 +164,7 @@ public void setSocketKeepAlive(boolean socketKeepAlive) {

/**
* Set the {@link ReadPreference}.
*
*
* @param readPreference
*/
public void setReadPreference(ReadPreference readPreference) {
Expand All @@ -171,7 +174,7 @@ public void setReadPreference(ReadPreference readPreference) {
/**
* Set the {@link WriteConcern} that will be the default value used when asking the {@link MongoDbFactory} for a DB
* object.
*
*
* @param writeConcern
*/
public void setWriteConcern(WriteConcern writeConcern) {
Expand All @@ -187,7 +190,7 @@ public void setSocketFactory(SocketFactory socketFactory) {

/**
* Set the frequency that the driver will attempt to determine the current state of each server in the cluster.
*
*
* @param heartbeatFrequency
*/
public void setHeartbeatFrequency(int heartbeatFrequency) {
Expand All @@ -197,7 +200,7 @@ public void setHeartbeatFrequency(int heartbeatFrequency) {
/**
* In the event that the driver has to frequently re-check a server's availability, it will wait at least this long
* since the previous check to avoid wasted effort.
*
*
* @param minHeartbeatFrequency
*/
public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {
Expand All @@ -206,7 +209,7 @@ public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {

/**
* Set the connect timeout for connections used for the cluster heartbeat.
*
*
* @param heartbeatConnectTimeout
*/
public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {
Expand All @@ -215,7 +218,7 @@ public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {

/**
* Set the socket timeout for connections used for the cluster heartbeat.
*
*
* @param heartbeatSocketTimeout
*/
public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {
Expand All @@ -224,7 +227,7 @@ public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {

/**
* Configures the name of the replica set.
*
*
* @param requiredReplicaSetName
*/
public void setRequiredReplicaSetName(String requiredReplicaSetName) {
Expand All @@ -233,7 +236,7 @@ public void setRequiredReplicaSetName(String requiredReplicaSetName) {

/**
* This controls if the driver should us an SSL connection. Defaults to |@literal false}.
*
*
* @param ssl
*/
public void setSsl(boolean ssl) {
Expand All @@ -243,24 +246,41 @@ public void setSsl(boolean ssl) {
/**
* Set the {@link SSLSocketFactory} to use for the {@literal SSL} connection. If none is configured here,
* {@link SSLSocketFactory#getDefault()} will be used.
*
*
* @param sslSocketFactory
*/
public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
this.sslSocketFactory = sslSocketFactory;
}

/*
/**
* Set the {@literal server selection timeout} in msec for a 3.x MongoDB Java driver. If not set the default value of
* 30 sec will be used. A value of 0 means that it will timeout immediately if no server is available. A negative
* value means to wait indefinitely.
*
* @param serverSelectionTimeout in msec.
*/
public void setServerSelectionTimeout(int serverSelectionTimeout) {
this.serverSelectionTimeout = serverSelectionTimeout;
}

/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
*/
@Override
protected MongoClientOptions createInstance() throws Exception {

SocketFactory socketFactoryToUse = ssl ? (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory
.getDefault()) : this.socketFactory;
SocketFactory socketFactoryToUse = ssl
? (sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()) : this.socketFactory;

MongoClientOptions.Builder builder = MongoClientOptions.builder();

if (MongoClientVersion.isMongo3Driver() && serverSelectionTimeout != Integer.MIN_VALUE) {
new DirectFieldAccessor(builder).setPropertyValue("serverSelectionTimeout", serverSelectionTimeout);
}

return MongoClientOptions.builder() //
return builder //
.alwaysUseMBeans(this.alwaysUseMBeans) //
.connectionsPerHost(this.connectionsPerHost) //
.connectTimeout(connectTimeout) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ http\://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd=org/sprin
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd=org/springframework/data/mongodb/config/spring-mongo-1.7.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd=org/springframework/data/mongodb/config/spring-mongo-1.8.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.10.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.10.2.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.2.xsd
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.10.2.xsd
Loading