JMX connection to JBoss

Sometimes you need to control JBoss status from outside of JBoss's JVM or just talk with JBoss using JMX.

Good example: starting JBoss from Installation programm and waiting upon it will started.

 

Problem is that you must have jbossall-client.jar in your classpath but you have not.

In JBoss 5 jbossall-client.jar has only references to other 20 jars via MANIFEST.MF

 

Solution is load jbossall-client.jar dynamically and obtain MBeanServerConnection using Reflection.

Pay attention, in order to create InitialContext you must to switch ContextClassLoader to your ClassLoader.

 

<code>

ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader cl = new URLClassLoader(new URL[] {new File("JBOSS_HOME/client/jbossall-client.jar")});

Thread.currentThread().setContextClassLoader(cl);
Class<Context> clazz = (Class<Context>) cl.loadClass("javax.naming.InitialContext");

 

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, String.format("jnp://%s:%d", getJmxHostname(), getJmxPort()));
Constructor<Context> ctor = clazz.getConstructor(Hashtable.class);
Context ctx = ctor.newInstance(env);
Method method = clazz.getMethod("lookup", String.class);
MBeanServerConnection conn = (MBeanServerConnection) method.invoke(ctx, "jmx/invoker/RMIAdaptor");

Thread.currentThread().setContextClassLoader(currentClassLoader);

 

ObjectName name = ObjectName.getInstance("jboss.system", "type", "Server");
Boolean started = (Boolean) connection.getAttribute(name, "Started");
 

</code>

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com