Skip to content

io microsphere management JmxUtils

github-actions[bot] edited this page Apr 11, 2026 · 5 revisions

JmxUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.management | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/management/JmxUtils.java

Overview

The utilities class for JMX operations, providing convenient methods to interact with MBeans and MXBeans.

This class offers static utility methods to retrieve and manipulate JMX MBeans and attributes, as well as access various platform MXBean components such as memory, thread, and garbage collection metrics.

Declaration

public abstract class JmxUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.8-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

getClassLoadingMXBean

ClassLoadingMXBean classLoadingMXBean = JmxUtils.getClassLoadingMXBean();
long loadedClassCount = classLoadingMXBean.getLoadedClassCount();
System.out.println("Loaded class count: " + loadedClassCount);

getMemoryMXBean

MemoryMXBean memoryMXBean = JmxUtils.getMemoryMXBean();
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
System.out.println("Heap memory usage: " + heapMemoryUsage);

getThreadMXBean

ThreadMXBean threadMXBean = JmxUtils.getThreadMXBean();
long threadCount = threadMXBean.getThreadCount();
System.out.println("Current thread count: " + threadCount);

getRuntimeMXBean

RuntimeMXBean runtimeMXBean = JmxUtils.getRuntimeMXBean();
String jvmName = runtimeMXBean.getName();
System.out.println("JVM Name: " + jvmName);

getCompilationMXBean

Optional<CompilationMXBean> compilationMXBean = JmxUtils.getCompilationMXBean();
if (compilationMXBean.isPresent()) {
    CompilationMXBean bean = compilationMXBean.get();
    String compilerName = bean.getName();
    System.out.println("Compiler name: " + compilerName);
} else {
    System.out.println("No compilation MXBean available.");
}

getOperatingSystemMXBean

OperatingSystemMXBean osMXBean = JmxUtils.getOperatingSystemMXBean();
String osName = osMXBean.getName();
String version = osMXBean.getVersion();
int availableProcessors = osMXBean.getAvailableProcessors();
System.out.println("Operating System: " + osName);
System.out.println("Version: " + version);
System.out.println("Available Processors: " + availableProcessors);

getMemoryPoolMXBeans

List<MemoryPoolMXBean> memoryPools = JmxUtils.getMemoryPoolMXBeans();
for (MemoryPoolMXBean pool : memoryPools) {
    String poolName = pool.getName();
    MemoryUsage usage = pool.getUsage();
    System.out.println("Memory Pool: " + poolName + ", Usage: " + usage);
}

getMemoryManagerMXBeans

List<MemoryManagerMXBean> memoryManagers = JmxUtils.getMemoryManagerMXBeans();
for (MemoryManagerMXBean manager : memoryManagers) {
    String managerName = manager.getName();
    boolean isVerbose = manager.isVerbose();
    System.out.println("Memory Manager: " + managerName + ", Verbose: " + isVerbose);
}

getGarbageCollectorMXBeans

List<GarbageCollectorMXBean> garbageCollectors = JmxUtils.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gc : garbageCollectors) {
    String gcName = gc.getName();
    long collectionCount = gc.getCollectionCount();
    System.out.println("Garbage Collector: " + gcName + ", Collection Count: " + collectionCount);
}

getMBeanAttributes

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

Map<String, MBeanAttribute> attributesMap = JmxUtils.getMBeanAttributesMap(mBeanServer, objectName);

for (Map.Entry<String, MBeanAttribute> entry : attributesMap.entrySet()) {
    String attributeName = entry.getKey();
    MBeanAttribute mBeanAttribute = entry.getValue();
    System.out.println("Attribute Name: " + attributeName);
    System.out.println("Attribute Info: " + mBeanAttribute.getAttributeInfo());
    System.out.println("Attribute Value: " + mBeanAttribute.getValue());
}
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

MBeanAttribute[] mBeanAttributes = JmxUtils.getMBeanAttributes(mBeanServer, objectName);

for (MBeanAttribute attr : mBeanAttributes) {
    System.out.println("Attribute Name: " + attr.getName());
    System.out.println("Attribute Type: " + attr.getType());
    System.out.println("Attribute Value: " + attr.getValue());
}

getAttribute

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

MBeanAttributeInfo attributeInfo = JmxUtils.findMBeanAttributeInfo(mBeanServer, objectName, "HeapMemoryUsage");
if (attributeInfo != null) {
    Object heapMemoryUsage = JmxUtils.getAttribute(mBeanServer, objectName, attributeInfo);
    System.out.println("Heap Memory Usage: " + heapMemoryUsage);
}

getAttribute

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");
String attributeName = "HeapMemoryUsage";

Object heapMemoryUsage = JmxUtils.getAttribute(mBeanServer, objectName, attributeName);
System.out.println("Heap Memory Usage: " + heapMemoryUsage);

findMBeanAttributeInfo

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");
String attributeName = "HeapMemoryUsage";

MBeanAttributeInfo attributeInfo = JmxUtils.findMBeanAttributeInfo(mBeanServer, objectName, attributeName);
if (attributeInfo != null) {
    System.out.println("Attribute Info: " + attributeInfo.getDescription());
}

getMBeanInfo

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

MBeanInfo mBeanInfo = JmxUtils.getMBeanInfo(mBeanServer, objectName);
if (mBeanInfo != null) {
    System.out.println("MBean Description: " + mBeanInfo.getDescription());
    System.out.println("MBean Class Name: " + mBeanInfo.getClassName());
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.management.JmxUtils;

API Reference

Public Methods

Method Description
getClassLoadingMXBean Returns the managed bean for the class loading system of the Java virtual machine.
getMemoryMXBean Returns the managed bean for the memory system of the Java virtual machine.
getThreadMXBean Returns the managed bean for the thread system of the Java virtual machine.
getRuntimeMXBean Returns the managed bean for the runtime system of the Java virtual machine.
getCompilationMXBean Returns the managed bean for the compilation system of the Java virtual machine.
getOperatingSystemMXBean Returns the managed bean for the operating system on which the Java virtual machine is running.
getMemoryPoolMXBeans Returns an unmodifiable list of MemoryPoolMXBean objects representing the memory pools in the Java virtual machine.
getMemoryManagerMXBeans Returns a list of MemoryManagerMXBean objects in the Java virtual machine.
getGarbageCollectorMXBeans Returns a list of GarbageCollectorMXBean objects in the Java virtual machine.
getMBeanAttributes Retrieves a read-only map of MBean attributes for the specified MBean registered in the given MBeanServer.
getAttribute Retrieves the value of the specified MBean attribute from the given MBean registered in the MBeanServer.
getAttribute Retrieves the value of the specified MBean attribute from the given MBean registered in the MBeanServer.
findMBeanAttributeInfo Retrieves the metadata (MBeanAttributeInfo) for a specific attribute of an MBean registered in the MBeanServer.
getMBeanInfo Retrieves the metadata (MBeanInfo) for the specified MBean registered in the MBeanServer.
methodSignature
signature
descriptorForElement
descriptorForAnnotations

Method Details

getClassLoadingMXBean

public static ClassLoadingMXBean getClassLoadingMXBean()

Returns the managed bean for the class loading system of the Java virtual machine.

Example Usage

`ClassLoadingMXBean classLoadingMXBean = JmxUtils.getClassLoadingMXBean();
long loadedClassCount = classLoadingMXBean.getLoadedClassCount();
System.out.println("Loaded class count: " + loadedClassCount);
`

getMemoryMXBean

public static MemoryMXBean getMemoryMXBean()

Returns the managed bean for the memory system of the Java virtual machine.

Example Usage

`MemoryMXBean memoryMXBean = JmxUtils.getMemoryMXBean();
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
System.out.println("Heap memory usage: " + heapMemoryUsage);
`

getThreadMXBean

public static ThreadMXBean getThreadMXBean()

Returns the managed bean for the thread system of the Java virtual machine.

Example Usage

`ThreadMXBean threadMXBean = JmxUtils.getThreadMXBean();
long threadCount = threadMXBean.getThreadCount();
System.out.println("Current thread count: " + threadCount);
`

getRuntimeMXBean

public static RuntimeMXBean getRuntimeMXBean()

Returns the managed bean for the runtime system of the Java virtual machine.

Example Usage

`RuntimeMXBean runtimeMXBean = JmxUtils.getRuntimeMXBean();
String jvmName = runtimeMXBean.getName();
System.out.println("JVM Name: " + jvmName);
`

getCompilationMXBean

public static Optional<CompilationMXBean> getCompilationMXBean()

Returns the managed bean for the compilation system of the Java virtual machine. This method returns an empty Optional if the Java virtual machine has no compilation system.

Example Usage

`Optional compilationMXBean = JmxUtils.getCompilationMXBean();
if (compilationMXBean.isPresent()) {
    CompilationMXBean bean = compilationMXBean.get();
    String compilerName = bean.getName();
    System.out.println("Compiler name: " + compilerName);
` else {
    System.out.println("No compilation MXBean available.");
}
}

getOperatingSystemMXBean

public static OperatingSystemMXBean getOperatingSystemMXBean()

Returns the managed bean for the operating system on which the Java virtual machine is running.

Example Usage

`OperatingSystemMXBean osMXBean = JmxUtils.getOperatingSystemMXBean();
String osName = osMXBean.getName();
String version = osMXBean.getVersion();
int availableProcessors = osMXBean.getAvailableProcessors();
System.out.println("Operating System: " + osName);
System.out.println("Version: " + version);
System.out.println("Available Processors: " + availableProcessors);
`

getMemoryPoolMXBeans

public static List<MemoryPoolMXBean> getMemoryPoolMXBeans()

Returns an unmodifiable list of MemoryPoolMXBean objects representing the memory pools in the Java virtual machine.

The Java virtual machine can have one or more memory pools, and this method provides access to them. The returned list is unmodifiable and reflects the current state of the JVM's memory pools.

Example Usage

`List memoryPools = JmxUtils.getMemoryPoolMXBeans();
for (MemoryPoolMXBean pool : memoryPools) {
    String poolName = pool.getName();
    MemoryUsage usage = pool.getUsage();
    System.out.println("Memory Pool: " + poolName + ", Usage: " + usage);
`
}

getMemoryManagerMXBeans

public static List<MemoryManagerMXBean> getMemoryManagerMXBeans()

Returns a list of MemoryManagerMXBean objects in the Java virtual machine. The Java virtual machine may have one or more memory managers, and this method provides access to them.

The returned list reflects the current state of the JVM's memory managers and may change over time as memory managers are added or removed during execution.

Example Usage

`List memoryManagers = JmxUtils.getMemoryManagerMXBeans();
for (MemoryManagerMXBean manager : memoryManagers) {
    String managerName = manager.getName();
    boolean isVerbose = manager.isVerbose();
    System.out.println("Memory Manager: " + managerName + ", Verbose: " + isVerbose);
`
}

getGarbageCollectorMXBeans

public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans()

Returns a list of GarbageCollectorMXBean objects in the Java virtual machine. The Java virtual machine may have one or more garbage collectors, and this method provides access to them.

The returned list reflects the current state of the JVM's garbage collectors and may change over time as garbage collectors are added or removed during execution.

Example Usage

`List garbageCollectors = JmxUtils.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gc : garbageCollectors) {
    String gcName = gc.getName();
    long collectionCount = gc.getCollectionCount();
    System.out.println("Garbage Collector: " + gcName + ", Collection Count: " + collectionCount);
`
}

getMBeanAttributes

public static MBeanAttribute[] getMBeanAttributes(MBeanServer mBeanServer, ObjectName objectName)

Retrieves a read-only map of MBean attributes for the specified MBean registered in the given MBeanServer. The keys are attribute names, and the values are corresponding MBeanAttribute instances.

Example Usage

`MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

Map attributesMap = JmxUtils.getMBeanAttributesMap(mBeanServer, objectName);

for (Map.Entry entry : attributesMap.entrySet()) {
    String attributeName = entry.getKey();
    MBeanAttribute mBeanAttribute = entry.getValue();
    System.out.println("Attribute Name: " + attributeName);
    System.out.println("Attribute Info: " + mBeanAttribute.getAttributeInfo());
    System.out.println("Attribute Value: " + mBeanAttribute.getValue());
`
}

getAttribute

public static Object getAttribute(MBeanServer mBeanServer, ObjectName objectName, MBeanAttributeInfo attributeInfo)

Retrieves the value of the specified MBean attribute from the given MBean registered in the MBeanServer.

This method uses the provided MBeanAttributeInfo to determine the name and other metadata of the attribute, then fetches its current value from the MBean.

Example Usage

`MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

MBeanAttributeInfo attributeInfo = JmxUtils.findMBeanAttributeInfo(mBeanServer, objectName, "HeapMemoryUsage");
if (attributeInfo != null) {
    Object heapMemoryUsage = JmxUtils.getAttribute(mBeanServer, objectName, attributeInfo);
    System.out.println("Heap Memory Usage: " + heapMemoryUsage);
`
}

getAttribute

public static Object getAttribute(MBeanServer mBeanServer, ObjectName objectName, String attributeName)

Retrieves the value of the specified MBean attribute from the given MBean registered in the MBeanServer.

This method fetches the current value of the attribute identified by the given name from the MBean registered under the specified ObjectName in the provided MBeanServer. If the attribute is not readable or an error occurs, null is returned.

Example Usage

`MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");
String attributeName = "HeapMemoryUsage";

Object heapMemoryUsage = JmxUtils.getAttribute(mBeanServer, objectName, attributeName);
System.out.println("Heap Memory Usage: " + heapMemoryUsage);
`

findMBeanAttributeInfo

public static MBeanAttributeInfo findMBeanAttributeInfo(MBeanServer mBeanServer, ObjectName objectName, String attributeName)

Retrieves the metadata (MBeanAttributeInfo) for a specific attribute of an MBean registered in the MBeanServer.

This method searches through the attributes of the specified MBean, identified by its ObjectName, to find an attribute with the given name. If found, it returns the corresponding MBeanAttributeInfo; otherwise, it returns null and logs a warning if the attribute is not found.

Example Usage

`MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");
String attributeName = "HeapMemoryUsage";

MBeanAttributeInfo attributeInfo = JmxUtils.findMBeanAttributeInfo(mBeanServer, objectName, attributeName);
if (attributeInfo != null) {
    System.out.println("Attribute Info: " + attributeInfo.getDescription());
`
}

getMBeanInfo

public static MBeanInfo getMBeanInfo(MBeanServer mBeanServer, ObjectName objectName)

Retrieves the metadata (MBeanInfo) for the specified MBean registered in the MBeanServer.

This method fetches the MBeanInfo for the MBean identified by the given ObjectName. The MBeanInfo contains detailed information about the MBean, including its attributes, operations, constructors, and notifications.

Example Usage

`MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("java.lang", "type", "Memory");

MBeanInfo mBeanInfo = JmxUtils.getMBeanInfo(mBeanServer, objectName);
if (mBeanInfo != null) {
    System.out.println("MBean Description: " + mBeanInfo.getDescription());
    System.out.println("MBean Class Name: " + mBeanInfo.getClassName());
`
}

See Also

  • MBeanAttribute
  • Utils

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally