@@ -52,7 +52,8 @@ class GenericJMXConfConnection
52
52
private String _host = null ;
53
53
private String _instance_prefix = null ;
54
54
private String _service_url = null ;
55
- private MBeanServerConnection _jmx_connection = null ;
55
+ private JMXConnector _jmx_connector = null ;
56
+ private MBeanServerConnection _mbean_connection = null ;
56
57
private List <GenericJMXConfMBean > _mbeans = null ;
57
58
58
59
/*
@@ -92,55 +93,72 @@ private String getHost () /* {{{ */
92
93
return Collectd .getHostname ();
93
94
} /* }}} String getHost */
94
95
95
- private void connect () /* {{{ */
96
- {
97
- JMXServiceURL service_url ;
98
- JMXConnector connector ;
99
- Map environment ;
96
+ private void connect () /* {{{ */
97
+ {
98
+ JMXServiceURL service_url ;
99
+ Map <String ,Object > environment ;
100
100
101
- if (_jmx_connection != null )
102
- return ;
101
+ // already connected
102
+ if (this ._jmx_connector != null ) {
103
+ return ;
104
+ }
103
105
104
- environment = null ;
105
- if (this ._password != null )
106
- {
107
- String [] credentials ;
106
+ environment = null ;
107
+ if (this ._password != null )
108
+ {
109
+ String [] credentials ;
108
110
109
- if (this ._username == null )
110
- this ._username = new String ("monitorRole" );
111
+ if (this ._username == null )
112
+ this ._username = new String ("monitorRole" );
111
113
112
- credentials = new String [] { this ._username , this ._password };
114
+ credentials = new String [] { this ._username , this ._password };
113
115
114
- environment = new HashMap ();
115
- environment .put (JMXConnector .CREDENTIALS , credentials );
116
- environment .put (JMXConnectorFactory .PROTOCOL_PROVIDER_CLASS_LOADER , this .getClass ().getClassLoader ());
117
- }
116
+ environment = new HashMap < String , Object > ();
117
+ environment .put (JMXConnector .CREDENTIALS , credentials );
118
+ environment .put (JMXConnectorFactory .PROTOCOL_PROVIDER_CLASS_LOADER , this .getClass ().getClassLoader ());
119
+ }
118
120
119
- try
120
- {
121
- service_url = new JMXServiceURL (this ._service_url );
122
- connector = JMXConnectorFactory .connect (service_url , environment );
123
- _jmx_connection = connector .getMBeanServerConnection ();
124
- }
125
- catch (Exception e )
121
+ try
122
+ {
123
+ service_url = new JMXServiceURL (this ._service_url );
124
+ this ._jmx_connector = JMXConnectorFactory .connect (service_url , environment );
125
+ this ._mbean_connection = _jmx_connector .getMBeanServerConnection ();
126
+ }
127
+ catch (Exception e )
128
+ {
129
+ Collectd .logError ("GenericJMXConfConnection: "
130
+ + "Creating MBean server connection failed: " + e );
131
+ disconnect ();
132
+ return ;
133
+ }
134
+ } /* }}} void connect */
135
+
136
+ private void disconnect () /* {{{ */
126
137
{
127
- Collectd .logError ("GenericJMXConfConnection: "
128
- + "Creating MBean server connection failed: " + e );
129
- return ;
130
- }
131
- } /* }}} void connect */
138
+ try
139
+ {
140
+ this ._jmx_connector .close ();
141
+ }
142
+ catch (Exception e )
143
+ {
144
+ // It's fine if close throws an exception
145
+ }
132
146
133
- /*
134
- * public methods
135
- *
136
- * <Connection>
137
- * Host "tomcat0.mycompany"
138
- * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
139
- * Collect "java.lang:type=GarbageCollector,name=Copy"
140
- * Collect "java.lang:type=Memory"
141
- * </Connection>
142
- *
143
- */
147
+ this ._jmx_connector = null ;
148
+ this ._mbean_connection = null ;
149
+ } /* }}} void disconnect */
150
+
151
+ /*
152
+ * public methods
153
+ *
154
+ * <Connection>
155
+ * Host "tomcat0.mycompany"
156
+ * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
157
+ * Collect "java.lang:type=GarbageCollector,name=Copy"
158
+ * Collect "java.lang:type=Memory"
159
+ * </Connection>
160
+ *
161
+ */
144
162
public GenericJMXConfConnection (OConfigItem ci ) /* {{{ */
145
163
throws IllegalArgumentException
146
164
{
@@ -217,9 +235,10 @@ public void query () /* {{{ */
217
235
{
218
236
PluginData pd ;
219
237
238
+ // try to connect
220
239
connect ();
221
240
222
- if (this ._jmx_connection == null )
241
+ if (this ._mbean_connection == null )
223
242
return ;
224
243
225
244
Collectd .logDebug ("GenericJMXConfConnection.query: "
@@ -234,11 +253,11 @@ public void query () /* {{{ */
234
253
{
235
254
int status ;
236
255
237
- status = this ._mbeans .get (i ).query (this ._jmx_connection , pd ,
256
+ status = this ._mbeans .get (i ).query (this ._mbean_connection , pd ,
238
257
this ._instance_prefix );
239
258
if (status != 0 )
240
259
{
241
- this . _jmx_connection = null ;
260
+ disconnect () ;
242
261
return ;
243
262
}
244
263
} /* for */
0 commit comments