Skip to content

Commit abcda17

Browse files
committed
Merge branch 'release-2.9.x' into 2.10.x
Conflicts: build.properties pom.xml src/main/com/mongodb/DBPortPool.java src/main/com/mongodb/DBTCPConnector.java src/main/com/mongodb/Mongo.java src/main/com/mongodb/MongoOptions.java
2 parents 9a2ccc0 + 3672db1 commit abcda17

File tree

5 files changed

+93
-21
lines changed

5 files changed

+93
-21
lines changed

src/main/com/mongodb/DBPortPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ DBPortPool get( ServerAddress addr ){
103103
}
104104

105105
private DBPortPool createPool(final ServerAddress addr) {
106-
if (isJava5) {
106+
if (isJava5 || _options.isAlwaysUseMBeans()) {
107107
return new Java5MongoConnectionPool(addr, _options);
108108
} else {
109109
return new MongoConnectionPool(addr, _options);

src/main/com/mongodb/MongoClientOptions.java

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323

2424
/**
2525
* Various settings to control the behavior of a <code>MongoClient</code>.
26-
* <p>
26+
* <p/>
2727
* Note: This class is a replacement for {@code MongoOptions}, to be used with {@code MongoClient}. The main difference
2828
* in behavior is that the default write concern is {@code WriteConcern.ACKNOWLEDGED}.
29+
*
2930
* @see MongoClient
3031
* @since 2.10.0
3132
*/
@@ -54,6 +55,7 @@ public static class Builder {
5455
private WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
5556
private SocketFactory socketFactory = SocketFactory.getDefault();
5657
private boolean cursorFinalizerEnabled = true;
58+
private boolean alwaysUseMBeans = false;
5759

5860
/**
5961
* Sets the description.
@@ -86,7 +88,8 @@ public Builder connectionsPerHost(final int connectionsPerHost) {
8688
/**
8789
* Sets the multiplier for number of threads allowed to block waiting for a connection.
8890
*
89-
* @param threadsAllowedToBlockForConnectionMultiplier the multiplier
91+
* @param threadsAllowedToBlockForConnectionMultiplier
92+
* the multiplier
9093
* @return {@code this}
9194
* @throws IllegalArgumentException if <code>threadsAllowedToBlockForConnectionMultiplier < 1</code>
9295
* @see com.mongodb.MongoClientOptions#getThreadsAllowedToBlockForConnectionMultiplier()
@@ -106,7 +109,6 @@ public Builder threadsAllowedToBlockForConnectionMultiplier(final int threadsAll
106109
* @return {@code this}
107110
* @throws IllegalArgumentException if <code>maxWaitTime < 0</code>
108111
* @see com.mongodb.MongoClientOptions#getMaxWaitTime()
109-
*
110112
*/
111113
public Builder maxWaitTime(final int maxWaitTime) {
112114
if (maxWaitTime < 0) {
@@ -133,6 +135,7 @@ public Builder connectTimeout(final int connectTimeout) {
133135

134136
/**
135137
* Sets the socket timeout.
138+
*
136139
* @param socketTimeout the socket timeout
137140
* @return {@code this}
138141
* @see com.mongodb.MongoClientOptions#getSocketTimeout()
@@ -163,7 +166,6 @@ public Builder socketKeepAlive(final boolean socketKeepAlive) {
163166
* @param autoConnectRetry auto connect retry
164167
* @return {@code this}
165168
* @see MongoClientOptions#isAutoConnectRetry()
166-
*
167169
*/
168170
public Builder autoConnectRetry(final boolean autoConnectRetry) {
169171
this.autoConnectRetry = autoConnectRetry;
@@ -264,7 +266,6 @@ public Builder socketFactory(final SocketFactory socketFactory) {
264266
* Sets whether cursor finalizers are enabled.
265267
*
266268
* @param cursorFinalizerEnabled whether cursor finalizers are enabled.
267-
*
268269
* @return {@code this}
269270
* @see MongoClientOptions#isCursorFinalizerEnabled()
270271
*/
@@ -273,6 +274,20 @@ public Builder cursorFinalizerEnabled(final boolean cursorFinalizerEnabled) {
273274
return this;
274275
}
275276

277+
/**
278+
* Sets whether JMX beans registered by the driver should always be MBeans, regardless of whether the VM is
279+
* Java 6 or greater. If false, the driver will use MXBeans if the VM is Java 6 or greater, and use MBeans if
280+
* the VM is Java 5.
281+
*
282+
* @param alwaysUseMBeans true if driver should always use MBeans, regardless of VM version
283+
* @return this
284+
* @see MongoClientOptions#isAlwaysUseMBeans()
285+
*/
286+
public Builder alwaysUseMBeans(final boolean alwaysUseMBeans) {
287+
this.alwaysUseMBeans = alwaysUseMBeans;
288+
return this;
289+
}
290+
276291
/**
277292
* Sets defaults to be what they are in {@code MongoOptions}.
278293
*
@@ -297,8 +312,9 @@ public MongoClientOptions build() {
297312

298313
/**
299314
* Gets the description for this MongoClient, which is used in various places like logging and JMX.
300-
* <p>
315+
* <p/>
301316
* Default is null.
317+
*
302318
* @return the description
303319
*/
304320
public String getDescription() {
@@ -309,8 +325,9 @@ public String getDescription() {
309325
* The maximum number of connections allowed per host for this MongoClient instance.
310326
* Those connections will be kept in a pool when idle.
311327
* Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.
312-
* <p>
328+
* <p/>
313329
* Default is 100.
330+
*
314331
* @return the maximum size of the connection pool per host
315332
* @see MongoClientOptions#getThreadsAllowedToBlockForConnectionMultiplier()
316333
*/
@@ -323,8 +340,9 @@ public int getConnectionsPerHost() {
323340
* may be waiting for a connection to become available from the pool. All further threads will get an exception right
324341
* away. For example if connectionsPerHost is 10 and threadsAllowedToBlockForConnectionMultiplier is 5, then up to 50
325342
* threads can wait for a connection.
326-
* <p>
343+
* <p/>
327344
* Default is 5.
345+
*
328346
* @return the multiplier
329347
*/
330348
public int getThreadsAllowedToBlockForConnectionMultiplier() {
@@ -333,8 +351,9 @@ public int getThreadsAllowedToBlockForConnectionMultiplier() {
333351

334352
/**
335353
* The maximum wait time in milliseconds that a thread may wait for a connection to become available.
336-
* <p>
354+
* <p/>
337355
* Default is 120,000. A value of 0 means that it will not wait. A negative value means to wait indefinitely.
356+
*
338357
* @return the maximum wait time.
339358
*/
340359
public int getMaxWaitTime() {
@@ -344,8 +363,9 @@ public int getMaxWaitTime() {
344363
/**
345364
* The connection timeout in milliseconds. A value of 0 means no timeout.
346365
* It is used solely when establishing a new connection {@link java.net.Socket#connect(java.net.SocketAddress, int) }
347-
* <p>
366+
* <p/>
348367
* Default is 10,000.
368+
*
349369
* @return the socket connect timeout
350370
*/
351371
public int getConnectTimeout() {
@@ -355,8 +375,9 @@ public int getConnectTimeout() {
355375
/**
356376
* The socket timeout in milliseconds.
357377
* It is used for I/O socket read and write operations {@link java.net.Socket#setSoTimeout(int)}
358-
* <p>
378+
* <p/>
359379
* Default is 0 and means no timeout.
380+
*
360381
* @return the socket timeout
361382
*/
362383
public int getSocketTimeout() {
@@ -365,7 +386,7 @@ public int getSocketTimeout() {
365386

366387
/**
367388
* This flag controls the socket keep alive feature that keeps a connection alive through firewalls {@link java.net.Socket#setKeepAlive(boolean)}
368-
* <p>
389+
* <p/>
369390
* * Default is false.
370391
*
371392
* @return whether keep-alive is enabled on each socket
@@ -382,7 +403,7 @@ public boolean isSocketKeepAlive() {
382403
* Note that when using this flag:
383404
* - for a replica set, the driver will trying to connect to the old master for that time, instead of failing over to the new one right away
384405
* - this does not prevent exception from being thrown in read/write operations on the socket, which must be handled by application
385-
*
406+
* <p/>
386407
* Even if this flag is false, the driver already has mechanisms to automatically recreate broken connections and retry the read operations.
387408
* Default is false.
388409
*
@@ -404,8 +425,9 @@ public long getMaxAutoConnectRetryTime() {
404425

405426
/**
406427
* The read preference to use for queries, map-reduce, aggregation, and count.
407-
* <p>
428+
* <p/>
408429
* Default is {@code ReadPreference.primary()}.
430+
*
409431
* @return the read preference
410432
* @see com.mongodb.ReadPreference#primary()
411433
*/
@@ -433,8 +455,9 @@ public DBEncoderFactory getDbEncoderFactory() {
433455

434456
/**
435457
* The write concern to use.
436-
* <p>
458+
* <p/>
437459
* Default is {@code WriteConcern.ACKNOWLEDGED}.
460+
*
438461
* @return the write concern
439462
* @see WriteConcern#ACKNOWLEDGED
440463
*/
@@ -444,7 +467,7 @@ public WriteConcern getWriteConcern() {
444467

445468
/**
446469
* The socket factory for creating sockets to the mongo server.
447-
* <p>
470+
* <p/>
448471
* Default is SocketFactory.getDefault()
449472
*
450473
* @return the socket factory
@@ -454,9 +477,9 @@ public SocketFactory getSocketFactory() {
454477
}
455478

456479
/**
457-
* Sets whether there is a a finalize method created that cleans up instances of DBCursor that the client
480+
* Gets whether there is a a finalize method created that cleans up instances of DBCursor that the client
458481
* does not close. If you are careful to always call the close method of DBCursor, then this can safely be set to false.
459-
* <p>
482+
* <p/>
460483
* Default is true.
461484
*
462485
* @return whether finalizers are enabled on cursors
@@ -467,6 +490,18 @@ public boolean isCursorFinalizerEnabled() {
467490
return cursorFinalizerEnabled;
468491
}
469492

493+
/**
494+
* Gets whether JMX beans registered by the driver should always be MBeans, regardless of whether the VM is
495+
* Java 6 or greater. If false, the driver will use MXBeans if the VM is Java 6 or greater, and use MBeans if
496+
* the VM is Java 5.
497+
* <p>
498+
* Default is false.
499+
* </p>
500+
*/
501+
public boolean isAlwaysUseMBeans() {
502+
return alwaysUseMBeans;
503+
}
504+
470505
private MongoClientOptions(final Builder builder) {
471506
description = builder.description;
472507
connectionsPerHost = builder.connectionsPerHost;
@@ -483,6 +518,7 @@ private MongoClientOptions(final Builder builder) {
483518
writeConcern = builder.writeConcern;
484519
socketFactory = builder.socketFactory;
485520
cursorFinalizerEnabled = builder.cursorFinalizerEnabled;
521+
alwaysUseMBeans = builder.alwaysUseMBeans;
486522
}
487523

488524

@@ -501,4 +537,5 @@ private MongoClientOptions(final Builder builder) {
501537
private final WriteConcern writeConcern;
502538
private final SocketFactory socketFactory;
503539
private final boolean cursorFinalizerEnabled;
540+
private final boolean alwaysUseMBeans;
504541
}

src/main/com/mongodb/MongoOptions.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void reset(){
7373
socketFactory = SocketFactory.getDefault();
7474
description = null;
7575
cursorFinalizerEnabled = true;
76+
alwaysUseMBeans = false;
7677
}
7778

7879
public MongoOptions copy() {
@@ -97,6 +98,7 @@ public MongoOptions copy() {
9798
m.socketFactory = socketFactory;
9899
m.description = description;
99100
m.cursorFinalizerEnabled = cursorFinalizerEnabled;
101+
m.alwaysUseMBeans = alwaysUseMBeans;
100102
return m;
101103
}
102104

@@ -336,6 +338,16 @@ public int hashCode() {
336338
*/
337339
public WriteConcern writeConcern;
338340

341+
/**
342+
* Sets whether JMX beans registered by the driver should always be MBeans, regardless of whether the VM is
343+
* Java 6 or greater. If false, the driver will use MXBeans if the VM is Java 6 or greater, and use MBeans if
344+
* the VM is Java 5.
345+
* <p>
346+
* Default is false.
347+
* </p>
348+
*/
349+
public boolean alwaysUseMBeans;
350+
339351
public String toString(){
340352
StringBuilder buf = new StringBuilder();
341353
buf.append( "description=" ).append( description ).append( ", " );
@@ -359,7 +371,8 @@ public String toString(){
359371
buf.append( "wtimeout=" ).append( wtimeout ).append( ", " );
360372
buf.append( "fsync=" ).append( fsync ).append( ", " );
361373
buf.append( "j=" ).append(j).append( ", " );
362-
buf.append( "cursorFinalizerEnabled=").append( cursorFinalizerEnabled);
374+
buf.append( "cursorFinalizerEnabled=").append(cursorFinalizerEnabled).append( ", " );
375+
buf.append( "alwaysUseMBeans=").append(alwaysUseMBeans);
363376

364377
return buf.toString();
365378
}
@@ -678,4 +691,20 @@ public void setCursorFinalizerEnabled(final boolean cursorFinalizerEnabled) {
678691
this.cursorFinalizerEnabled = cursorFinalizerEnabled;
679692

680693
}
694+
695+
/**
696+
*
697+
* @return true if the driver should always use MBeans, regardless of VM
698+
*/
699+
public boolean isAlwaysUseMBeans() {
700+
return alwaysUseMBeans;
701+
}
702+
703+
/**
704+
*
705+
* @param alwaysUseMBeans sets whether the driver should always use MBeans, regardless of VM
706+
*/
707+
public void setAlwaysUseMBeans(final boolean alwaysUseMBeans) {
708+
this.alwaysUseMBeans = alwaysUseMBeans;
709+
}
681710
}

src/test/com/mongodb/MongoClientOptionsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void testBuilderDefaults() {
4141
Assert.assertEquals(false, options.isSocketKeepAlive());
4242
Assert.assertEquals(true, options.isCursorFinalizerEnabled());
4343
Assert.assertEquals(false, options.isAutoConnectRetry());
44+
Assert.assertEquals(false, options.isAlwaysUseMBeans());
4445
}
4546

4647
@Test
@@ -117,6 +118,7 @@ public void testBuilderBuild() {
117118
builder.threadsAllowedToBlockForConnectionMultiplier(1);
118119
builder.socketKeepAlive(true);
119120
builder.cursorFinalizerEnabled(true);
121+
builder.alwaysUseMBeans(true);
120122

121123
SocketFactory socketFactory = SSLSocketFactory.getDefault();
122124
builder.socketFactory(socketFactory);
@@ -147,6 +149,7 @@ public DBDecoder create() {
147149
Assert.assertEquals(1, options.getThreadsAllowedToBlockForConnectionMultiplier());
148150
Assert.assertEquals(true, options.isSocketKeepAlive());
149151
Assert.assertEquals(true, options.isCursorFinalizerEnabled());
152+
Assert.assertEquals(true, options.isAlwaysUseMBeans());
150153

151154
Assert.assertEquals(socketFactory, options.getSocketFactory());
152155
Assert.assertEquals(encoderFactory, options.getDbEncoderFactory());

src/test/com/mongodb/MongoOptionsTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void testCopy() throws Exception {
5151
options.description = "cool";
5252
options.readPreference = ReadPreference.secondary();
5353
options.cursorFinalizerEnabled = true;
54+
options.alwaysUseMBeans = true;
5455

5556
final MongoOptions copy = options.copy();
5657
assertEquals(options.connectionsPerHost, copy.connectionsPerHost);
@@ -72,7 +73,7 @@ public void testCopy() throws Exception {
7273
assertEquals(options.socketFactory, copy.socketFactory);
7374
assertEquals(options.description, copy.description);
7475
assertEquals(options.readPreference, copy.readPreference);
75-
assertEquals(options.cursorFinalizerEnabled, copy.cursorFinalizerEnabled);
76+
assertEquals(options.alwaysUseMBeans, copy.alwaysUseMBeans);
7677
}
7778

7879
@Test
@@ -99,6 +100,7 @@ public void testGetterSetters() throws Exception {
99100
options.setDescription("very cool");
100101
options.setReadPreference(ReadPreference.secondary());
101102
options.setCursorFinalizerEnabled(true);
103+
options.setAlwaysUseMBeans(true);
102104

103105
assertEquals(options.getConnectionsPerHost(), 100);
104106
assertEquals(options.getThreadsAllowedToBlockForConnectionMultiplier(), 101);
@@ -119,6 +121,7 @@ public void testGetterSetters() throws Exception {
119121
assertEquals(options.getDescription(), "very cool");
120122
assertEquals(options.getReadPreference(), ReadPreference.secondary());
121123
assertEquals(options.isCursorFinalizerEnabled(), true);
124+
assertEquals(options.isAlwaysUseMBeans(), true);
122125
}
123126

124127
@Test

0 commit comments

Comments
 (0)