Managing MBeans with WildFly

Today I needed to get a list of all MBeans which are available on WildFly, along with attributes and operations available.

The starting point is this article which shows how to display MBeans running on AS7 by means of the JMXConnectorFactory:

http://middlewaremagic.com/jboss/?p=1246

What is necessary to change, when moving to WildFly is the protocol used for the JMX Connection which is service:jmx:http-remoting-jmx://localhost:9990

Next, I have added a couple of methods to dump as well the attributes and the operations which are available. Finally I have added a sample method which shows how to retrieve an attribute from the MBeanServer. Here is the full code:

package browse;

import java.util.*;
import java.util.Hashtable;
import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.*;

public class MBeanServiceMonitor {

    private static MBeanServerConnection connection;
    private static JMXConnector connector;

    public static void connect() throws IOException {

        Hashtable h = new Hashtable();
        JMXServiceURL address = new JMXServiceURL("service:jmx:http-remoting-jmx://localhost:9990");
        connector = JMXConnectorFactory.connect(address, null);
        connection = connector.getMBeanServerConnection();

        System.out.println("Connected to MBean Server");
    }

    private static void listWildFlyMBeans() throws Exception {

        ObjectName serviceRef = new ObjectName("*.*:*");
        Set<ObjectName> mbeans = connection.queryNames(serviceRef, null);
        for (ObjectName on : mbeans) {
            System.out.println("\t ObjectName : " + on);
            try {
                printAttributes(on);
            } catch (Exception exc) {

            }

        }
    }

    static void printAttributes(final ObjectName http)
            throws Exception {
        MBeanInfo info = connection.getMBeanInfo(http);
        MBeanAttributeInfo[] attrInfo = info.getAttributes();
        MBeanOperationInfo[] operInfo = info.getOperations();
        System.out.println(">Attributes:");
        for (MBeanAttributeInfo attr : attrInfo) {
            System.out.println("  " + attr.getName() + "\n");
        }
        System.out.println(">Operations:");
        for (MBeanOperationInfo attr : operInfo) {
            System.out.println("  " + attr.getName() + "\n");
        }
    }

    public static void main(String[] args) throws Exception {

        connect();
        System.out.println("Dump WildFly MBeans \n\n");
        listWildFlyMBeans();
        getAttributeExample();
    }

    private static void getAttributeExample() {
        try {
            Object obj = connection.getAttribute(new ObjectName("jboss.as:management-root=server"), "releaseCodename");
            System.out.println(obj);
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}

 The expected output of this code will be the list of all Mbeans which are available, their methods, their operations and finally one attribute, the releaseCodename for your server.

Follow us on Twitter