Skip to content

Instantly share code, notes, and snippets.

@bric3
Created October 14, 2011 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bric3/1286811 to your computer and use it in GitHub Desktop.
Save bric3/1286811 to your computer and use it in GitHub Desktop.
Commodity class to create a JMX loopback connection
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.remote.*;
import java.io.IOException;
import java.lang.management.ManagementFactory;
/**
* Utilitarian/commodity class to allow easy JMX testing by setting up a loopback connection
* by wrapping the logic of a {@link JMXConnectorServer}.
* <p/>
* <p><b>DISCLAIMER :</b> this class is not self managed, you must ensure in your
* code that you close things yourself. And this class is NOT thread-safe.</p>
* <p/>
* <p>
* Example use of this class :
* <pre>
* &#x40;Before
* public start_loopback() {
* jmxLoopback = new JMXLoopback(someMBeanServer);
* }
*
* &#x40;After
* public stop_loopback() {
* jmxLoopback.stop();
* }
*
* &#x40;Test
* public void that_definately_should_work_this_way() throws Exception {
* JMXConnector jmxConnetor = JMXConnectorFactory.connect(jmxLoopback.getActualJMXServiceUrl());
* MBeanServerConnection connection = jmx.getgetMBeanServerConnection();
*
* // and so on...
* }
* </pre>
* </p>
*
* @see JMXConnectorServer
* @see JMXConnectorServerFactory
*
* @author Brice Dutheil
*/
public class JMXLoopback {
private static JMXConnectorServer connectorServer;
private MBeanServer mBeanServer;
private JMXConnector connector;
/**
* Creates the JMX loopback using the platform MBean server and start it.
*
* @throws IOException Thrown if the JMXConnectorServer cannot be created or started.
* @see java.lang.management.ManagementFactory#getPlatformMBeanServer()
* @see #JMXLoopback(javax.management.MBeanServer)
*/
public JMXLoopback() throws IOException {
this(ManagementFactory.getPlatformMBeanServer());
}
/**
* Creates the JMX loopback using the given MBean server and start it.
*
* @param mBeanServer The MBeanServer used to create the loopback
* @throws IOException Thrown if the JMXConnectorServer cannot be created or started.
*/
public JMXLoopback(MBeanServer mBeanServer) throws IOException {
this.mBeanServer = mBeanServer;
init(mBeanServer);
}
private void init(MBeanServer mBeanServer) throws IOException {
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(
new JMXServiceURL("service:jmx:rmi://"),
null,
mBeanServer
);
connectorServer.start();
}
/**
* Stop the loopback.
*
* @throws IOException Thrown if the JMXConnectorServer could not be stopped cleanly.
*/
public void terminate() throws IOException {
if (connector != null) {
connector.close();
}
connectorServer.stop();
}
/**
* Return the actual JMX service URL that should be used when creating your JMXConnector.
*
* @return The actual JMX service URL
* @see javax.management.remote.JMXConnector
* @see javax.management.remote.JMXConnectorFactory
*/
public JMXServiceURL getActualJMXServiceUrl() {
return connectorServer.getAddress();
}
/**
* Commodity method to get the actual MBean server of ths loopback.
*
* @return The actual MBeanServer
*/
public MBeanServer getActualMBeanServer() {
return connectorServer.getMBeanServer();
}
/**
* Create a new MBeanServerConnection using the loopback.
*
* <p>
* This creates internally a new JMXConnector. If a connection was already created,
* this closes the current connector and creates a new one. This connector will be
* closed when calling {@link #terminate()}.
* </p>
*
* @return A new MBeanServerConnection
* @throws IOException Thrown if there is a problem when connecting or creation the connection.
*/
public MBeanServerConnection createMBeanServerConnection() throws IOException {
if (connector != null) {
connector.close();
connector = null;
}
connector = JMXConnectorFactory.connect(connectorServer.getAddress());
return connector.getMBeanServerConnection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment