1
1
/*
2
- * Copyright 2015 the original author or authors.
2
+ * Copyright 2015-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
30
30
31
31
/**
32
32
* A factory bean for construction of a {@link MongoClientOptions} instance.
33
- *
33
+ *
34
34
* @author Christoph Strobl
35
35
* @author Oliver Gierke
36
+ * @author Mark Paluch
36
37
* @since 1.7
37
38
*/
38
39
public class MongoClientOptionsFactoryBean extends AbstractFactoryBean <MongoClientOptions > {
@@ -62,13 +63,14 @@ public class MongoClientOptionsFactoryBean extends AbstractFactoryBean<MongoClie
62
63
private int heartbeatConnectTimeout = DEFAULT_MONGO_OPTIONS .getHeartbeatConnectTimeout ();
63
64
private int heartbeatSocketTimeout = DEFAULT_MONGO_OPTIONS .getHeartbeatSocketTimeout ();
64
65
private String requiredReplicaSetName = DEFAULT_MONGO_OPTIONS .getRequiredReplicaSetName ();
66
+ private int serverSelectionTimeout = DEFAULT_MONGO_OPTIONS .getServerSelectionTimeout ();
65
67
66
68
private boolean ssl ;
67
69
private SSLSocketFactory sslSocketFactory ;
68
70
69
71
/**
70
72
* Set the {@link MongoClient} description.
71
- *
73
+ *
72
74
* @param description
73
75
*/
74
76
public void setDescription (String description ) {
@@ -77,7 +79,7 @@ public void setDescription(String description) {
77
79
78
80
/**
79
81
* Set the minimum number of connections per host.
80
- *
82
+ *
81
83
* @param minConnectionsPerHost
82
84
*/
83
85
public void setMinConnectionsPerHost (int minConnectionsPerHost ) {
@@ -87,7 +89,7 @@ public void setMinConnectionsPerHost(int minConnectionsPerHost) {
87
89
/**
88
90
* Set the number of connections allowed per host. Will block if run out. Default is 10. System property
89
91
* {@code MONGO.POOLSIZE} can override
90
- *
92
+ *
91
93
* @param connectionsPerHost
92
94
*/
93
95
public void setConnectionsPerHost (int connectionsPerHost ) {
@@ -98,7 +100,7 @@ public void setConnectionsPerHost(int connectionsPerHost) {
98
100
* Set the multiplier for connectionsPerHost for # of threads that can block. Default is 5. If connectionsPerHost is
99
101
* 10, and threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an
100
102
* exception will be thrown.
101
- *
103
+ *
102
104
* @param threadsAllowedToBlockForConnectionMultiplier
103
105
*/
104
106
public void setThreadsAllowedToBlockForConnectionMultiplier (int threadsAllowedToBlockForConnectionMultiplier ) {
@@ -107,7 +109,7 @@ public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedTo
107
109
108
110
/**
109
111
* Set the max wait time of a blocking thread for a connection. Default is 12000 ms (2 minutes)
110
- *
112
+ *
111
113
* @param maxWaitTime
112
114
*/
113
115
public void setMaxWaitTime (int maxWaitTime ) {
@@ -116,7 +118,7 @@ public void setMaxWaitTime(int maxWaitTime) {
116
118
117
119
/**
118
120
* The maximum idle time for a pooled connection.
119
- *
121
+ *
120
122
* @param maxConnectionIdleTime
121
123
*/
122
124
public void setMaxConnectionIdleTime (int maxConnectionIdleTime ) {
@@ -125,7 +127,7 @@ public void setMaxConnectionIdleTime(int maxConnectionIdleTime) {
125
127
126
128
/**
127
129
* Set the maximum life time for a pooled connection.
128
- *
130
+ *
129
131
* @param maxConnectionLifeTime
130
132
*/
131
133
public void setMaxConnectionLifeTime (int maxConnectionLifeTime ) {
@@ -134,7 +136,7 @@ public void setMaxConnectionLifeTime(int maxConnectionLifeTime) {
134
136
135
137
/**
136
138
* Set the connect timeout in milliseconds. 0 is default and infinite.
137
- *
139
+ *
138
140
* @param connectTimeout
139
141
*/
140
142
public void setConnectTimeout (int connectTimeout ) {
@@ -143,7 +145,7 @@ public void setConnectTimeout(int connectTimeout) {
143
145
144
146
/**
145
147
* Set the socket timeout. 0 is default and infinite.
146
- *
148
+ *
147
149
* @param socketTimeout
148
150
*/
149
151
public void setSocketTimeout (int socketTimeout ) {
@@ -152,7 +154,7 @@ public void setSocketTimeout(int socketTimeout) {
152
154
153
155
/**
154
156
* Set the keep alive flag, controls whether or not to have socket keep alive timeout. Defaults to false.
155
- *
157
+ *
156
158
* @param socketKeepAlive
157
159
*/
158
160
public void setSocketKeepAlive (boolean socketKeepAlive ) {
@@ -161,7 +163,7 @@ public void setSocketKeepAlive(boolean socketKeepAlive) {
161
163
162
164
/**
163
165
* Set the {@link ReadPreference}.
164
- *
166
+ *
165
167
* @param readPreference
166
168
*/
167
169
public void setReadPreference (ReadPreference readPreference ) {
@@ -171,7 +173,7 @@ public void setReadPreference(ReadPreference readPreference) {
171
173
/**
172
174
* Set the {@link WriteConcern} that will be the default value used when asking the {@link MongoDbFactory} for a DB
173
175
* object.
174
- *
176
+ *
175
177
* @param writeConcern
176
178
*/
177
179
public void setWriteConcern (WriteConcern writeConcern ) {
@@ -187,7 +189,7 @@ public void setSocketFactory(SocketFactory socketFactory) {
187
189
188
190
/**
189
191
* Set the frequency that the driver will attempt to determine the current state of each server in the cluster.
190
- *
192
+ *
191
193
* @param heartbeatFrequency
192
194
*/
193
195
public void setHeartbeatFrequency (int heartbeatFrequency ) {
@@ -197,7 +199,7 @@ public void setHeartbeatFrequency(int heartbeatFrequency) {
197
199
/**
198
200
* In the event that the driver has to frequently re-check a server's availability, it will wait at least this long
199
201
* since the previous check to avoid wasted effort.
200
- *
202
+ *
201
203
* @param minHeartbeatFrequency
202
204
*/
203
205
public void setMinHeartbeatFrequency (int minHeartbeatFrequency ) {
@@ -206,7 +208,7 @@ public void setMinHeartbeatFrequency(int minHeartbeatFrequency) {
206
208
207
209
/**
208
210
* Set the connect timeout for connections used for the cluster heartbeat.
209
- *
211
+ *
210
212
* @param heartbeatConnectTimeout
211
213
*/
212
214
public void setHeartbeatConnectTimeout (int heartbeatConnectTimeout ) {
@@ -215,7 +217,7 @@ public void setHeartbeatConnectTimeout(int heartbeatConnectTimeout) {
215
217
216
218
/**
217
219
* Set the socket timeout for connections used for the cluster heartbeat.
218
- *
220
+ *
219
221
* @param heartbeatSocketTimeout
220
222
*/
221
223
public void setHeartbeatSocketTimeout (int heartbeatSocketTimeout ) {
@@ -224,7 +226,7 @@ public void setHeartbeatSocketTimeout(int heartbeatSocketTimeout) {
224
226
225
227
/**
226
228
* Configures the name of the replica set.
227
- *
229
+ *
228
230
* @param requiredReplicaSetName
229
231
*/
230
232
public void setRequiredReplicaSetName (String requiredReplicaSetName ) {
@@ -233,7 +235,7 @@ public void setRequiredReplicaSetName(String requiredReplicaSetName) {
233
235
234
236
/**
235
237
* This controls if the driver should us an SSL connection. Defaults to |@literal false}.
236
- *
238
+ *
237
239
* @param ssl
238
240
*/
239
241
public void setSsl (boolean ssl ) {
@@ -243,22 +245,32 @@ public void setSsl(boolean ssl) {
243
245
/**
244
246
* Set the {@link SSLSocketFactory} to use for the {@literal SSL} connection. If none is configured here,
245
247
* {@link SSLSocketFactory#getDefault()} will be used.
246
- *
248
+ *
247
249
* @param sslSocketFactory
248
250
*/
249
251
public void setSslSocketFactory (SSLSocketFactory sslSocketFactory ) {
250
252
this .sslSocketFactory = sslSocketFactory ;
251
253
}
252
254
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
+ /*
254
266
* (non-Javadoc)
255
267
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
256
268
*/
257
269
@ Override
258
270
protected MongoClientOptions createInstance () throws Exception {
259
271
260
- SocketFactory socketFactoryToUse = ssl ? ( sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory
261
- .getDefault ()) : this .socketFactory ;
272
+ SocketFactory socketFactoryToUse = ssl
273
+ ? ( sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory .getDefault ()) : this .socketFactory ;
262
274
263
275
return MongoClientOptions .builder () //
264
276
.alwaysUseMBeans (this .alwaysUseMBeans ) //
@@ -278,6 +290,7 @@ protected MongoClientOptions createInstance() throws Exception {
278
290
.minHeartbeatFrequency (minHeartbeatFrequency ) //
279
291
.readPreference (readPreference ) //
280
292
.requiredReplicaSetName (requiredReplicaSetName ) //
293
+ .serverSelectionTimeout (serverSelectionTimeout ) //
281
294
.socketFactory (socketFactoryToUse ) //
282
295
.socketKeepAlive (socketKeepAlive ) //
283
296
.socketTimeout (socketTimeout ) //
0 commit comments