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/4.20latest/user-guide/monitoring.html#jmx. This empowers users to engage in efficient monitoring through widely recognized JMX monitoring tools like JConsole.
...
List all available metrics
Expand |
---|
title | JMXNamespacesJMXListAllMetrics.java |
---|
|
Code Block |
---|
| 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 ListJMXNamespacesJMXListAllMetrics {
/**
* 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();
}
}
} |
|
Compilation
Code Block |
---|
|
javac JMXListAllMetrics.java |
Execution
Code Block |
---|
|
java JMXListAllMetrics |
Scrape one metric periodically
Expand |
---|
title | JMXClientJMXOneMetric.java |
---|
|
Code Block |
---|
| import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
/**
* This class represents a JMX client that connects to a remote JMX server to retrieve and print
* attribute values of a specified MBean (Managed Bean) object at regular intervals.
*/
public class JMXClientJMXOneMetric {
/**
* The main entry point of the JMX client application.
*
* @param args Command-line arguments. Expects two arguments: <ObjectName> and <DelayInMillis>.
* <ObjectName> specifies the target MBean's ObjectName, and <DelayInMillis> is
* the delay between metric checks in milliseconds.
* Example: java JMXClient metrics:name=com.hivemq.jvm.memory.heap.used 2000
* @throws Exception If there are errors in the JMX connection or attribute retrieval.
*/
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java JMXClient <ObjectName> <DelayInMillis>");
System.err.println("Example: java JMXClient metrics:name=com.hivemq.jvm.memory.heap.used 2000");
System.exit(1);
}
// Parse command-line arguments
String objectNameStr = args[0];
long delayInMillis = Long.parseLong(args[1]);
// Define the JMX service URL for connecting to the remote JMX server
String jmxUrl = "service:jmx:rmi:///jndi/rmi://localhost:9010/jmxrmi";
JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl);
JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
MBeanServerConnection connection = connector.getMBeanServerConnection();
// Define the ObjectName based on the command-line argument
ObjectName mbeanName = new ObjectName(objectNameStr);
// Run indefinitely with the specified delay between checks
while (true) {
// Fetch and print the metric value
Object metricValue = connection.getAttribute(mbeanName, "Value");
System.out.println(objectNameStr + " " + metricValue);
// Sleep for the specified delay before the next check
Thread.sleep(delayInMillis);
}
}
} |
|
Compilation
Code Block |
---|
|
javac JMXClientJMXOneMetric.java |
Execution
Code Block |
---|
|
java JMXClientJMXOneMetric <ObjectName> <DelayInMillis> |
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.
\uD83D\uDCCB Related articles
...