X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=bindings%2Fjava%2Forg%2Fcollectd%2Fjava%2FGenericJMXConfConnection.java;h=887c289562ac09ed17a3aac76efcea88394b1393;hp=ea0f2fa2e54a1a3eabe7ca65517ac336c60ca40d;hb=master;hpb=abc30f241619b3eb66c801c324390e1e9e6e001f diff --git a/bindings/java/org/collectd/java/GenericJMXConfConnection.java b/bindings/java/org/collectd/java/GenericJMXConfConnection.java index ea0f2fa2..887c2895 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,74 @@ 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 + { + if (this._jmx_connector != null) { + 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 +237,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 +255,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 */