I need to access the jmx-console but need to do it programatically

Solution Unverified - Updated

Environment

  • JBoss Enterprise Application Platform (EAP)
    • 4.x
    • 5.x

Issue

Resolution

The following is an example of how you can make jmx invocation programatically

import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.management.*;

try{
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
        env.put(Context.SECURITY_PRINCIPAL, "admin");
        env.put(Context.SECURITY_CREDENTIALS, "admin");
        Context ctx = new InitialContext(env);
        MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");

        ObjectName name = new ObjectName("jboss:service=JNDIView");
        MBeanInfo  info = server.getMBeanInfo(name);
        System.out.println("JNDIView Class: " + info.getClassName());

       Object result = server.invoke(name,"list", new Object[] {Boolean.TRUE}, new String[] {"boolean"}); 
       System.out.println("Invoking : "+result.toString());

 }catch(Exception ex){
   ex.printStackTrace();
}
  • Copy the above into a file with a .bsh extension and deploy to $JBOSS_HOME/server/$PROFILE/deploy directory. Alternatively you can adapt as you see fit
  • The principal and credentials listed above are the ones configured in conf/props/jmx-console-users.properties (used in the jmx-console security domain of conf/login-config.xml).
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.