From edba412f8ac4cc2b73a25b58dba4e3ebca20f398 Mon Sep 17 00:00:00 2001 From: Frank de Jong Date: Wed, 2 Dec 2015 11:55:26 +0100 Subject: [PATCH] GenericJMX plugin: memory leak, indent and compile warn fixes - call close() on JMXConnector if JMX connection fails; fixes memory leak - fix indentation errors - fix "unchecked" compile warnings - some refactoring --- .../collectd/java/GenericJMXConfConnection.java | 109 ++++++++++++--------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/bindings/java/org/collectd/java/GenericJMXConfConnection.java b/bindings/java/org/collectd/java/GenericJMXConfConnection.java index ea0f2fa2..aced54b4 100644 --- a/bindings/java/org/collectd/java/GenericJMXConfConnection.java +++ b/bindings/java/org/collectd/java/GenericJMXConfConnection.java @@ -52,7 +52,8 @@ class GenericJMXConfConnection private String _host = null; private String _instance_prefix = null; private String _service_url = null; - private MBeanServerConnection _jmx_connection = null; + private JMXConnector _jmx_connector = null; + private MBeanServerConnection _mbean_connection = null; private List _mbeans = null; /* @@ -92,55 +93,72 @@ class GenericJMXConfConnection return Collectd.getHostname(); } /* }}} String getHost */ -private void connect () /* {{{ */ -{ - JMXServiceURL service_url; - JMXConnector connector; - Map environment; + private void connect () /* {{{ */ + { + JMXServiceURL service_url; + Map environment; - if (_jmx_connection != null) - return; + // already connected + if (this._jmx_connector != null) { + return; + } - environment = null; - if (this._password != null) - { - String[] credentials; + environment = null; + if (this._password != null) + { + String[] credentials; - if (this._username == null) - this._username = new String ("monitorRole"); + if (this._username == null) + this._username = new String ("monitorRole"); - credentials = new String[] { this._username, this._password }; + credentials = new String[] { this._username, this._password }; - environment = new HashMap (); - environment.put (JMXConnector.CREDENTIALS, credentials); - environment.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, this.getClass().getClassLoader()); - } + environment = new HashMap (); + environment.put (JMXConnector.CREDENTIALS, credentials); + environment.put (JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, this.getClass().getClassLoader()); + } - try - { - service_url = new JMXServiceURL (this._service_url); - connector = JMXConnectorFactory.connect (service_url, environment); - _jmx_connection = connector.getMBeanServerConnection (); - } - catch (Exception e) + try + { + service_url = new JMXServiceURL (this._service_url); + this._jmx_connector = JMXConnectorFactory.connect (service_url, environment); + this._mbean_connection = _jmx_connector.getMBeanServerConnection (); + } + catch (Exception e) + { + Collectd.logError ("GenericJMXConfConnection: " + + "Creating MBean server connection failed: " + e); + disconnect (); + return; + } + } /* }}} void connect */ + + private void disconnect () /* {{{ */ { - Collectd.logError ("GenericJMXConfConnection: " - + "Creating MBean server connection failed: " + e); - return; - } -} /* }}} void connect */ + try + { + this._jmx_connector.close(); + } + catch (Exception e) + { + // It's fine if close throws an exception + } -/* - * public methods - * - * - * Host "tomcat0.mycompany" - * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi" - * Collect "java.lang:type=GarbageCollector,name=Copy" - * Collect "java.lang:type=Memory" - * - * - */ + this._jmx_connector = null; + this._mbean_connection = null; + } /* }}} void disconnect */ + + /* + * public methods + * + * + * Host "tomcat0.mycompany" + * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi" + * Collect "java.lang:type=GarbageCollector,name=Copy" + * Collect "java.lang:type=Memory" + * + * + */ public GenericJMXConfConnection (OConfigItem ci) /* {{{ */ throws IllegalArgumentException { @@ -217,9 +235,10 @@ private void connect () /* {{{ */ { PluginData pd; + // try to connect connect (); - if (this._jmx_connection == null) + if (this._mbean_connection == null) return; Collectd.logDebug ("GenericJMXConfConnection.query: " @@ -234,11 +253,11 @@ private void connect () /* {{{ */ { int status; - status = this._mbeans.get (i).query (this._jmx_connection, pd, + status = this._mbeans.get (i).query (this._mbean_connection, pd, this._instance_prefix); if (status != 0) { - this._jmx_connection = null; + disconnect (); return; } } /* for */ -- 2.11.0