Skip to content

Commit

Permalink
Forgot to close the JMXConnectors...
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Barringer committed Dec 17, 2014
1 parent fc867ff commit 98c8de5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/com/paessler/prtg/jmx/sensors/CustomJMXSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public DataResponse go() {
if (attributeList == null)
return null;
DataResponse response = null;
JMXConnector jmxc = null;
try {
response = new DataResponse(sensorId, mBean);
String[] creds = {rmiUsername, rmiPassword};
Map env = new HashMap<String, String[]>();
env.put(JMXConnector.CREDENTIALS, creds);
JMXServiceURL serviceURL = new JMXServiceURL(rmiString);
JMXConnector jmxc = JMXConnectorFactory.connect(serviceURL, env);
jmxc = JMXConnectorFactory.connect(serviceURL, env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName bean = new ObjectName(mBean);

Expand Down Expand Up @@ -103,6 +104,14 @@ public DataResponse go() {
error.setError("Exception");
error.setMessage(e.getMessage() + " (Service URL: " + rmiString + ")");
response = error;
} finally {
if (jmxc != null) {
try {
jmxc.close();
} catch (Exception e) {
// Ignore
}
}
}
return response;
}
Expand Down
12 changes: 10 additions & 2 deletions src/com/paessler/prtg/jmx/sensors/VMHealthSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public class VMHealthSensor extends Sensor {
@Override
public DataResponse go() {
DataResponse response = null;

JMXConnector jmxc = null;
try {
String[] creds = {rmiUsername, rmiPassword};
Map env = new HashMap<String, String[]>();
env.put(JMXConnector.CREDENTIALS, creds);
JMXServiceURL serviceURL = new JMXServiceURL(rmiString);

JMXConnector jmxc = JMXConnectorFactory.connect(serviceURL, env);
jmxc = JMXConnectorFactory.connect(serviceURL, env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName memoryBeanName = new ObjectName("java.lang:type=Memory");
CompositeData heapMemoryUsage = (CompositeData) mbsc.getAttribute(memoryBeanName, "HeapMemoryUsage");
Expand Down Expand Up @@ -111,6 +111,14 @@ public DataResponse go() {
error.setError("Exception");
error.setMessage(e.getMessage() + " (Service URL: " + rmiString + ")");
response = error;
} finally {
if (jmxc != null) {
try {
jmxc.close();
} catch (Exception e) {
// Ignore
}
}
}

return response;
Expand Down

0 comments on commit 98c8de5

Please sign in to comment.