Managing MBeans with WildFly

This article shows how to get a list of all MBeans which are available on WildFly, along with attributes and operations available.

Accessing WildFly through JMX

Firstly, to access WildFly via JMX, we need to use an URL scheme.

  • For Wildfly 9 and older: service:jmx:http-remoting-jmx://<HOST>:<PORT>
  • For Wildfly 10+: service:jmx:remote+http://<HOST>:<PORT>

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:remote+http://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 release Codename for your server.

Please note, to run the above example you need to include the wildfly-client-all dependency for your server version. For example:

<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-client-all</artifactId>
    <version>26.0.0.Final</version>
</dependency>

Other programmatic approaches

The above example shows how to use the JMX API to call an MBean via MBeanServer can be tedious: it involves a reflection-like syntax and a complex exception handling. If you want to use a simpler paradigm you can switch to the Jolokia Client API.

Check this article for more details: Monitor JBoss AS with Jolokia