Skip to content

Commit 923dfeb

Browse files
committed
JAVA-1117: Reverting to 2.11.x behavior for registration of JMX MBeans: logging at INFO if a duplicate is found, and adding the MongoClient description to the MBean name to provide a way for the application to remove the duplication.
1 parent 6d8a132 commit 923dfeb

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/main/com/mongodb/DefaultClusterableServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ClusterableServer create(final ServerAddress serverAddress) {
4949
.build();
5050
return new DefaultServer(serverAddress, settings,
5151
new PooledConnectionProvider(clusterId, serverAddress, new DBPortFactory(options), connectionPoolSettings,
52-
new JMXConnectionPoolListener()),
52+
new JMXConnectionPoolListener(mongo.getMongoOptions().getDescription())),
5353
scheduledExecutorService, mongo);
5454
}
5555

src/main/com/mongodb/JMXConnectionPoolListener.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.mongodb.util.management.MBeanServerFactory;
2020

21-
import java.net.URI;
2221
import java.util.concurrent.ConcurrentHashMap;
2322
import java.util.concurrent.ConcurrentMap;
2423

@@ -30,15 +29,26 @@
3029
class JMXConnectionPoolListener implements ConnectionPoolListener {
3130
private final ConcurrentMap<ClusterIdServerAddressPair, ConnectionPoolStatistics> map =
3231
new ConcurrentHashMap<ClusterIdServerAddressPair, ConnectionPoolStatistics>();
32+
private final String clusterDescription;
33+
34+
public JMXConnectionPoolListener(final String clusterDescription) {
35+
this.clusterDescription = clusterDescription;
36+
}
3337

3438
public String getMBeanObjectName(final String clusterId, final ServerAddress serverAddress) {
3539
// we could do a url encode, but since : is the only invalid character in an object name, then
3640
// we'll simply do it.
3741
String adjustedClusterId = clusterId.replace(":", "%3A");
3842
String adjustedHost = serverAddress.getHost().replace(":", "%3A");
3943

40-
return format("org.mongodb.driver:type=ConnectionPool,clusterId=%s,host=%s,port=%s", adjustedClusterId, adjustedHost,
41-
serverAddress.getPort());
44+
String objectName = format("org.mongodb.driver:type=ConnectionPool,clusterId=%s,host=%s,port=%s", adjustedClusterId, adjustedHost,
45+
serverAddress.getPort());
46+
if (clusterDescription != null) {
47+
String adjustedClusterDescription = clusterDescription.replace(":", "%3A");
48+
objectName = objectName + format(",description=%s", adjustedClusterDescription);
49+
}
50+
51+
return objectName;
4252
}
4353

4454
public ConnectionPoolStatisticsMBean getMBean(final String clusterId, final ServerAddress serverAddress) {

src/main/com/mongodb/util/management/jmx/JMXMBeanServer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818

1919
import com.mongodb.util.management.MBeanServer;
2020

21+
import javax.management.InstanceAlreadyExistsException;
2122
import javax.management.MalformedObjectNameException;
2223
import javax.management.ObjectName;
2324
import java.lang.management.ManagementFactory;
2425
import java.util.logging.Level;
2526
import java.util.logging.Logger;
2627

28+
import static java.lang.String.format;
29+
2730
/**
2831
* This class is NOT part of the public API. It may change at any time without notification.
2932
*
30-
* @deprecated This class will be removed in 3.x versions of the driver,
31-
* so please remove it from your compile time dependencies.
33+
* @deprecated This class will be removed in 3.x versions of the driver, so please remove it from your compile time dependencies.
3234
*/
3335
@Deprecated
3436
public class JMXMBeanServer implements MBeanServer {
@@ -57,6 +59,8 @@ public void unregisterMBean(String mBeanName) {
5759
public void registerMBean(Object mBean, String mBeanName) {
5860
try {
5961
server.registerMBean(mBean, createObjectName(mBeanName));
62+
} catch (InstanceAlreadyExistsException e) {
63+
LOGGER.log(Level.INFO, format("A JMX MBean with the name '%s' already exists", mBeanName));
6064
} catch (Exception e) {
6165
LOGGER.log(Level.WARNING, "Unable to register MBean " + mBeanName, e);
6266
}

src/test/com/mongodb/JMXConnectionPoolListenerSpecification.groovy

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package com.mongodb
2020

2121
import spock.lang.Specification
22-
import spock.lang.Subject
2322

2423
import javax.management.ObjectName
2524
import java.lang.management.ManagementFactory
@@ -30,14 +29,12 @@ class JMXConnectionPoolListenerSpecification extends Specification {
3029
private static final String CLUSTER_ID = '42'
3130
private static final ServerAddress SERVER_ADDRESS = new ServerAddress()
3231

33-
@Subject
34-
private final JMXConnectionPoolListener jmxListener = new JMXConnectionPoolListener()
35-
3632
private final openEvent = new ConnectionPoolOpenedEvent(CLUSTER_ID, SERVER_ADDRESS,
3733
ConnectionPoolSettings.builder().maxSize(5).maxWaitTime(1, SECONDS).build())
3834

3935
def 'statistics should reflect values from the provider'() {
4036
given:
37+
def jmxListener = new JMXConnectionPoolListener(null)
4138
def event = new ConnectionEvent(CLUSTER_ID, SERVER_ADDRESS)
4239

4340
when:
@@ -70,6 +67,7 @@ class JMXConnectionPoolListenerSpecification extends Specification {
7067

7168
def 'should remove MBean'() {
7269
given:
70+
def jmxListener = new JMXConnectionPoolListener(null)
7371
jmxListener.connectionPoolOpened(openEvent);
7472

7573
when:
@@ -82,6 +80,7 @@ class JMXConnectionPoolListenerSpecification extends Specification {
8280

8381
def 'should create a valid ObjectName for hostname'() {
8482
given:
83+
def jmxListener = new JMXConnectionPoolListener(null)
8584
String beanName = jmxListener.getMBeanObjectName(CLUSTER_ID, new ServerAddress('localhost'));
8685

8786
when:
@@ -93,6 +92,7 @@ class JMXConnectionPoolListenerSpecification extends Specification {
9392

9493
def 'should create a valid ObjectName for ipv4 addresses'() {
9594
given:
95+
def jmxListener = new JMXConnectionPoolListener(null)
9696
String beanName = jmxListener.getMBeanObjectName(CLUSTER_ID, new ServerAddress('127.0.0.1'))
9797

9898
when:
@@ -104,6 +104,7 @@ class JMXConnectionPoolListenerSpecification extends Specification {
104104

105105
def 'should create a valid ObjectName for ipv6 address'() {
106106
given:
107+
def jmxListener = new JMXConnectionPoolListener(null)
107108
String beanName = jmxListener.getMBeanObjectName(CLUSTER_ID, new ServerAddress('[::1]'))
108109

109110
when:
@@ -115,6 +116,7 @@ class JMXConnectionPoolListenerSpecification extends Specification {
115116

116117
def 'should create a valid ObjectName when cluster id has a :'() {
117118
given:
119+
def jmxListener = new JMXConnectionPoolListener(null)
118120
String beanName = jmxListener.getMBeanObjectName('kd:dk', new ServerAddress())
119121

120122
when:
@@ -123,4 +125,30 @@ class JMXConnectionPoolListenerSpecification extends Specification {
123125
then:
124126
objectName.toString() == 'org.mongodb.driver:type=ConnectionPool,clusterId=kd%3Adk,host=127.0.0.1,port=27017'
125127
}
128+
129+
def 'should include a non-null cluster description in the object name'() {
130+
given:
131+
def jmxListener = new JMXConnectionPoolListener('cluster description 1')
132+
String beanName = jmxListener.getMBeanObjectName('1', new ServerAddress())
133+
134+
when:
135+
ObjectName objectName = new ObjectName(beanName)
136+
137+
then:
138+
objectName.toString() ==
139+
'org.mongodb.driver:type=ConnectionPool,clusterId=1,host=127.0.0.1,port=27017,description=cluster description 1'
140+
}
141+
142+
def 'should replace colon in cluster description'() {
143+
given:
144+
def jmxListener = new JMXConnectionPoolListener('description: 1')
145+
String beanName = jmxListener.getMBeanObjectName('1', new ServerAddress())
146+
147+
when:
148+
ObjectName objectName = new ObjectName(beanName)
149+
150+
then:
151+
objectName.toString() ==
152+
'org.mongodb.driver:type=ConnectionPool,clusterId=1,host=127.0.0.1,port=27017,description=description%3A 1'
153+
}
126154
}

0 commit comments

Comments
 (0)