Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Initial implementation to close JMXConnector when MBeanServerConnection usage is complete #27

Closed
wants to merge 1 commit into from

2 participants

Tommy Tynjä Aslak Knutsen
Tommy Tynjä

Applied workaround logic present in JBoss AS7. Lacks specific test though.

Aslak Knutsen

pushed upstream, 0b33a16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2011
  1. Tommy Tynjä
This page is out of date. Refresh to see the latest.
52 container/spi/src/main/java/org/jboss/arquillian/container/spi/client/protocol/metadata/JMXContext.java
View
@@ -18,8 +18,16 @@
package org.jboss.arquillian.container.spi.client.protocol.metadata;
import java.io.IOException;
+import java.lang.ref.Reference;
+import java.lang.ref.WeakReference;
+import java.sql.Connection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
+import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
@@ -31,6 +39,11 @@
*/
public class JMXContext
{
+ private static final Logger log = Logger.getLogger(JMXContext.class.getName());
+
+ private static Set<Reference<MBeanServerConnection>> jmxConnectorRefs =
+ Collections.synchronizedSet(new HashSet<Reference<MBeanServerConnection>>());
+
/*
* The Host and Port of a Remote MBean Server
*/
@@ -58,15 +71,16 @@ public JMXContext(MBeanServerConnection connection)
*/
public MBeanServerConnection getConnection()
{
- if(connection == null)
+ if(connection == null)
{
try
{
- connection = JMXConnectorFactory.connect(getRemoteJMXURL(), null).getMBeanServerConnection();
- }
- catch (IOException e)
- {
- throw new RuntimeException("Could not create remote JMX connection: " + this, e);
+ JMXConnector connector = JMXConnectorFactory.connect(getRemoteJMXURL(), null);
+ connection = connector.getMBeanServerConnection();
+ WeakReference<MBeanServerConnection> ref = new JMXConnectorReference(connection, connector);
+ jmxConnectorRefs.add(ref);
+ } catch (IOException e) {
+ throw new RuntimeException("Could not create remote JMX connection: " + this, e);
}
}
return connection;
@@ -89,4 +103,30 @@ public String toString()
{
return "JMXContext [host=" + host + ", port=" + port + ", connection=" + connection + "]";
}
+
+ private static class JMXConnectorReference extends WeakReference<MBeanServerConnection>
+ {
+ private JMXConnector connector;
+
+ public JMXConnectorReference(final MBeanServerConnection connection, JMXConnector connector)
+ {
+ super(connection);
+ this.connector = connector;
+ }
+
+ @Override
+ public boolean enqueue()
+ {
+ try
+ {
+ connector.close();
+ } catch (Exception ignored) {
+ log.warning("Could not close JMXConnector: " + connector);
+ } finally {
+ connector = null;
+ jmxConnectorRefs.remove(this);
+ }
+ return super.enqueue();
+ }
+ }
}
Something went wrong with that request. Please try again.