JMX Metrics Quick Access with Java

HiveMQ, a robust and highly configurable MQTT broker, extends its functionality by offering an extensive array of metrics via Java Management Extensions (JMX): https://docs.hivemq.com/hivemq/latest/user-guide/monitoring.html#jmx. This empowers users to engage in efficient monitoring through widely recognized JMX monitoring tools like JConsole.

In addition to the plethora of visualization tools like Prometheus, Grafana, and Datadog available in the realm of monitoring, there is substantial merit in acquiring an overview of these metrics using pure Java tooling. This article serves as a comprehensive resource, offering practical Java examples for listing all accessible metrics and facilitating the extraction of metric values.

 Instructions

List all available metrics

import javax.management.*; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import java.util.Set; /** * This class demonstrates how to list JMX namespaces and MBeans on a remote JMX server. */ public class JMXListAllMetrics { /** * The main entry point of the program. * * @param args Command-line arguments (not used in this example). */ public static void main(String[] args) { // Press Opt+Enter with your caret at the highlighted text to see how // IntelliJ IDEA suggests fixing it. System.out.printf("Hello and welcome!"); // Define the JMX server URL String jmxUrl = "service:jmx:rmi:///jndi/rmi://localhost:9010/jmxrmi"; try { // Create a JMX connector to connect to the remote JMX server JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl); JMXConnector connector = JMXConnectorFactory.connect(serviceURL); // Connect to the MBean server MBeanServerConnection mbeanServerConnection = connector.getMBeanServerConnection(); // List all MBeans and their object names System.out.println("Listing all MBeans and their namespaces:"); Set<ObjectName> mbeanNames = mbeanServerConnection.queryNames(null, null); for (ObjectName mbeanName : mbeanNames) { //System.out.println("MBean: " + mbeanName); String namespace = mbeanName.getDomain(); // Extract the namespace //System.out.println("Namespace: " + namespace); System.out.println(namespace + " " + mbeanName); } // Close the JMX connector connector.close(); } catch (Exception e) { e.printStackTrace(); } } }
  1. Compilation

    javac JMXListAllMetrics.java
  2. Execution

    java JMXListAllMetrics

Scrape one metric periodically

  1. Compilation

  2. Execution

    Replace <ObjectName> with the desired metric's ObjectName and <DelayInMillis> with the desired delay between checks in milliseconds.

 

Dependencies for JMX are already included in the JDK, making it easy to access and monitor HiveMQ metrics with these Java programs.

Note: Ensure that you have the necessary permissions and network access to connect to the remote JMX server and access the HiveMQ metrics:

https://docs.hivemq.com/hivemq/latest/user-guide/monitoring.html#_configuration

Filter by label

There are no items with the selected labels at this time.