Skip to content

Commit 2ee8c20

Browse files
committed
JAVA-666, JAVA-641: Created two subclasses of DBPortPool: MongoConnectionPool and Java5MongoConnectionPool. The former is an MBean and the latter is an MXBean. When creating a DBPortPool, conditionally create one or the other depending on whether java.version system property indicates that this is a 1.5 virtual machine. This should avoid JXM registration errors when running on Java 5 VMs. Also, by using MongoConnectionPool as the name of the MXBean, avoids incompatibilities in JMX implementations that require the MXBean name to be the same as the MXBean interface that it implements (minus the MXBean suffix).
1 parent 0f67736 commit 2ee8c20

File tree

5 files changed

+140
-5
lines changed

5 files changed

+140
-5
lines changed

src/main/com/mongodb/DBPortPool.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,16 @@
3636
/**
3737
* This class is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
3838
*/
39-
public class DBPortPool extends SimplePool<DBPort> implements MongoConnectionPoolMXBean {
39+
public class DBPortPool extends SimplePool<DBPort> {
4040

41-
@Override
4241
public String getHost() {
4342
return _addr.getHost();
4443
}
4544

46-
@Override
4745
public int getPort() {
4846
return _addr.getPort();
4947
}
5048

51-
@Override
5249
public synchronized ConnectionPoolStatisticsBean getStatistics() {
5350
return new ConnectionPoolStatisticsBean(getTotal(), getInUse(), getInUseConnections());
5451
}
@@ -82,7 +79,7 @@ DBPortPool get( ServerAddress addr ){
8279
return p;
8380
}
8481

85-
p = new DBPortPool( addr , _options );
82+
p = createPool(addr);
8683
_pools.put( addr , p);
8784

8885
try {
@@ -102,6 +99,14 @@ DBPortPool get( ServerAddress addr ){
10299
return p;
103100
}
104101

102+
private DBPortPool createPool(final ServerAddress addr) {
103+
if (isJava5) {
104+
return new Java5MongoConnectionPool(addr, _options);
105+
} else {
106+
return new MongoConnectionPool(addr, _options);
107+
}
108+
}
109+
105110
void close(){
106111
synchronized ( _pools ){
107112
for ( DBPortPool p : _pools.values() ){
@@ -126,12 +131,17 @@ private String createObjectName( ServerAddress addr ) {
126131
return name;
127132
}
128133

134+
static {
135+
isJava5 = System.getProperty("java.version").startsWith("1.5");
136+
}
137+
129138
final MongoOptions _options;
130139
final Map<ServerAddress,DBPortPool> _pools = Collections.synchronizedMap( new HashMap<ServerAddress,DBPortPool>() );
131140
final int _serial = nextSerial.incrementAndGet();
132141

133142
// we use this to give each Holder a different mbean name
134143
static AtomicInteger nextSerial = new AtomicInteger(0);
144+
static final boolean isJava5;
135145
}
136146

137147
// ----
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb;
19+
20+
/**
21+
* This class exists only so that on Java 5 the driver can create instances of a standard MBean,
22+
* therefore keeping compatibility with the JMX implementation in the Java 5 JMX class libraries.
23+
*/
24+
class Java5MongoConnectionPool extends DBPortPool implements Java5MongoConnectionPoolMBean {
25+
26+
Java5MongoConnectionPool(ServerAddress addr, MongoOptions options) {
27+
super(addr, options);
28+
}
29+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
/**
20+
* A standard MBean interface for a Mongo connection pool, for use on Java 5 virtual machines.
21+
* <p>
22+
* This interface is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
23+
*/
24+
public interface Java5MongoConnectionPoolMBean {
25+
/**
26+
* Gets the name of the pool.
27+
*
28+
* @return the name of the pool
29+
*/
30+
String getName();
31+
32+
/**
33+
* Gets the host that this connection pool is connecting to.
34+
*
35+
* @return the host
36+
*/
37+
String getHost();
38+
39+
/**
40+
* Gets the port that this connection pool is connecting to.
41+
*
42+
* @return the port
43+
*/
44+
int getPort();
45+
46+
/**
47+
* Gets the total number of pool members, including idle and and in-use members.
48+
*
49+
* @return total number of members
50+
*/
51+
int getTotal();
52+
53+
/**
54+
* Gets the number of pool members that are currently in use.
55+
*
56+
* @return number of in-use members
57+
*/
58+
int getInUse();
59+
60+
/**
61+
* Gets the maximum allowed size of the pool, including idle and in-use members.
62+
*
63+
* @return the maximum size
64+
*/
65+
int getMaxSize();
66+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb;
19+
20+
/**
21+
* This class exists only so that, on Java 6 and above, the driver can create instances of an MXBean.
22+
*/
23+
class MongoConnectionPool extends DBPortPool implements MongoConnectionPoolMXBean {
24+
25+
MongoConnectionPool(ServerAddress addr, MongoOptions options) {
26+
super(addr, options);
27+
}
28+
}

src/main/com/mongodb/MongoConnectionPoolMXBean.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.mongodb.util.ConnectionPoolStatisticsBean;
2020

2121
/**
22+
* A standard MXBean interface for a Mongo connection pool, for use on Java 6 and above virtual machines.
23+
* <p>
2224
* This interface is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
2325
*/
2426
public interface MongoConnectionPoolMXBean {

0 commit comments

Comments
 (0)