Advertise with us

Search Mastertheboss

Our eBooks

How to get the list of MBeans in JBoss or WildFy

In order to retrieve the list of MBeans in a remote JBoss EAP or WildFly Server you can use the JMX API as follows.

The pre-requisite is that you create a Management User to access the remote JBoss / WildFly Server. (f.e. "admin","admin" in our example).

Then, here is a sample Main class:

import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class HelloWorld {
	public static void main(String[] args) throws Exception {
		status();
	}

	public static void status() throws Exception{
		String host = "localhost";
		int port = 9990;  // management-http port
		String urlString = "service:jmx:remote+http://" + host + ":" + port;
		System.out.println("\n\n\t****  urlString: "+urlString);;
		JMXServiceURL serviceURL = new JMXServiceURL(urlString);

		Map map = new HashMap();
		String[] credentials = new String[] { "admin", "admin" };
		map.put("jmx.remote.credentials", credentials);
		JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, map);

		MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

		ObjectName objectName = ObjectName.getInstance("jboss.as:management-root=server");
		String attributeName = "serverState";
		Object result = connection.getAttribute(objectName, attributeName);

		jmxConnector.close();

	}

}

Please note that you need to include in your application's classpath the jboss-client JAR files in order to be able to use the remote+http protocol:

<dependency>
  <groupId>org.wildfly</groupId>
  <artifactId>wildfly-client-all</artifactId>
  <version>20.0.0.Final</version>
</dependency>
© 2020 mastertheboss.com. All Rights Reserved.

Please publish modules in offcanvas position.