Class JmxUtils
- java.lang.Object
-
- io.microsphere.management.JmxUtils
-
- All Implemented Interfaces:
Utils
public abstract class JmxUtils extends java.lang.Object implements Utils
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.
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
MBeanAttribute
,Utils
-
-
Field Summary
Fields Modifier and Type Field Description static MBeanAttribute[]
EMPTY_MBEAN_ATTRIBUTE_ARRAY
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static java.lang.Object
doGetAttribute(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, javax.management.MBeanAttributeInfo attributeInfo, java.lang.String attributeName)
static javax.management.MBeanAttributeInfo
findMBeanAttributeInfo(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, java.lang.String attributeName)
Retrieves the metadata (MBeanAttributeInfo
) for a specific attribute of an MBean registered in the MBeanServer.static java.lang.Object
getAttribute(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, java.lang.String attributeName)
Retrieves the value of the specified MBean attribute from the given MBean registered in the MBeanServer.static java.lang.Object
getAttribute(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, javax.management.MBeanAttributeInfo attributeInfo)
Retrieves the value of the specified MBean attribute from the given MBean registered in the MBeanServer.static java.lang.management.ClassLoadingMXBean
getClassLoadingMXBean()
Returns the managed bean for the class loading system of the Java virtual machine.static java.util.Optional<java.lang.management.CompilationMXBean>
getCompilationMXBean()
Returns the managed bean for the compilation system of the Java virtual machine.static java.util.List<java.lang.management.GarbageCollectorMXBean>
getGarbageCollectorMXBeans()
Returns a list ofGarbageCollectorMXBean
objects in the Java virtual machine.static MBeanAttribute[]
getMBeanAttributes(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName)
Retrieves an array ofMBeanAttribute
objects representing the attributes of the specified MBean.static java.util.Map<java.lang.String,MBeanAttribute>
getMBeanAttributesMap(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName)
Retrieves a read-only map of MBean attributes for the specified MBean registered in the given MBeanServer.static javax.management.MBeanInfo
getMBeanInfo(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName)
Retrieves the metadata (MBeanInfo
) for the specified MBean registered in the MBeanServer.static java.util.List<java.lang.management.MemoryManagerMXBean>
getMemoryManagerMXBeans()
Returns a list ofMemoryManagerMXBean
objects in the Java virtual machine.static java.lang.management.MemoryMXBean
getMemoryMXBean()
Returns the managed bean for the memory system of the Java virtual machine.static java.util.List<java.lang.management.MemoryPoolMXBean>
getMemoryPoolMXBeans()
Returns an unmodifiable list ofMemoryPoolMXBean
objects representing the memory pools in the Java virtual machine.static java.lang.management.OperatingSystemMXBean
getOperatingSystemMXBean()
Returns the managed bean for the operating system on which the Java virtual machine is running.static java.lang.management.RuntimeMXBean
getRuntimeMXBean()
Returns the managed bean for the runtime system of the Java virtual machine.static java.lang.management.ThreadMXBean
getThreadMXBean()
Returns the managed bean for the thread system of the Java virtual machine.
-
-
-
Field Detail
-
EMPTY_MBEAN_ATTRIBUTE_ARRAY
public static final MBeanAttribute[] EMPTY_MBEAN_ATTRIBUTE_ARRAY
-
-
Method Detail
-
getClassLoadingMXBean
@Nonnull public static java.lang.management.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);
- Returns:
- a
ClassLoadingMXBean
object for the Java virtual machine. - See Also:
ManagementFactory.getClassLoadingMXBean()
-
getMemoryMXBean
@Nonnull public static java.lang.management.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);
- Returns:
- a
MemoryMXBean
object for the Java virtual machine. - See Also:
ManagementFactory.getMemoryMXBean()
-
getThreadMXBean
@Nonnull public static java.lang.management.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);
- Returns:
- a
ThreadMXBean
object for the Java virtual machine. - See Also:
ManagementFactory.getThreadMXBean()
-
getRuntimeMXBean
@Nonnull public static java.lang.management.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);
- Returns:
- a
RuntimeMXBean
object for the Java virtual machine. - See Also:
ManagementFactory.getRuntimeMXBean()
-
getCompilationMXBean
@Nonnull public static java.util.Optional<java.lang.management.CompilationMXBean> getCompilationMXBean()
Returns the managed bean for the compilation system of the Java virtual machine. This method returns an emptyOptional
if the Java virtual machine has no compilation system.Example Usage
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."); }
- Returns:
- an instance of
Optional
containing aCompilationMXBean
object for the Java virtual machine, or an empty Optional if the Java virtual machine has no compilation system. - See Also:
ManagementFactory.getCompilationMXBean()
-
getOperatingSystemMXBean
@Nonnull public static java.lang.management.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);
- Returns:
- an
OperatingSystemMXBean
object for the Java virtual machine. - See Also:
ManagementFactory.getOperatingSystemMXBean()
-
getMemoryPoolMXBeans
@Nonnull @Immutable public static java.util.List<java.lang.management.MemoryPoolMXBean> getMemoryPoolMXBeans()
Returns an unmodifiable list ofMemoryPoolMXBean
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<MemoryPoolMXBean> memoryPools = JmxUtils.getMemoryPoolMXBeans(); for (MemoryPoolMXBean pool : memoryPools) { String poolName = pool.getName(); MemoryUsage usage = pool.getUsage(); System.out.println("Memory Pool: " + poolName + ", Usage: " + usage); }
- Returns:
- a non-null, unmodifiable list of
MemoryPoolMXBean
objects.
-
getMemoryManagerMXBeans
@Nonnull @Immutable public static java.util.List<java.lang.management.MemoryManagerMXBean> getMemoryManagerMXBeans()
Returns a list ofMemoryManagerMXBean
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<MemoryManagerMXBean> memoryManagers = JmxUtils.getMemoryManagerMXBeans(); for (MemoryManagerMXBean manager : memoryManagers) { String managerName = manager.getName(); boolean isVerbose = manager.isVerbose(); System.out.println("Memory Manager: " + managerName + ", Verbose: " + isVerbose); }
- Returns:
- a non-null, unmodifiable list of
MemoryManagerMXBean
objects representing the memory managers in the Java virtual machine. - See Also:
ManagementFactory.getMemoryManagerMXBeans()
-
getGarbageCollectorMXBeans
@Nonnull @Immutable public static java.util.List<java.lang.management.GarbageCollectorMXBean> getGarbageCollectorMXBeans()
Returns a list ofGarbageCollectorMXBean
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<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); }
- Returns:
- a non-null, unmodifiable list of
GarbageCollectorMXBean
objects representing the garbage collectors in the Java virtual machine. - See Also:
ManagementFactory.getGarbageCollectorMXBeans()
-
getMBeanAttributesMap
@Nonnull @Immutable public static java.util.Map<java.lang.String,MBeanAttribute> getMBeanAttributesMap(javax.management.MBeanServer mBeanServer, javax.management.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 correspondingMBeanAttribute
instances.Example Usage
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()); }
- Parameters:
mBeanServer
- the MBeanServer from which to retrieve the attributesobjectName
- the name of the MBean whose attributes are to be retrieved- Returns:
- a non-null, unmodifiable map where the keys are attribute names and the values are MBeanAttribute instances
-
getMBeanAttributes
@Nonnull public static MBeanAttribute[] getMBeanAttributes(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName)
Retrieves an array ofMBeanAttribute
objects representing the attributes of the specified MBean.This method fetches all attributes from the MBean registered under the given
ObjectName
in the providedMBeanServer
. Each attribute is encapsulated in anMBeanAttribute
instance, which includes both the metadata (MBeanAttributeInfo
) and the current value of the attribute.Example Usage
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()); }
- Parameters:
mBeanServer
- the MBeanServer from which to retrieve the MBean attributesobjectName
- the name of the MBean whose attributes are to be retrieved- Returns:
- a non-null array of
MBeanAttribute
objects representing the attributes of the specified MBean
-
getAttribute
@Nullable public static java.lang.Object getAttribute(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, javax.management.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); }
- Parameters:
mBeanServer
- the MBeanServer from which to retrieve the attribute valueobjectName
- the name of the MBean whose attribute is to be retrievedattributeInfo
- the metadata of the attribute whose value is to be retrieved- Returns:
- the current value of the MBean attribute, or
null
if the attribute is not readable or an error occurs
-
getAttribute
@Nullable public static java.lang.Object getAttribute(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, java.lang.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 providedMBeanServer
. 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);
- Parameters:
mBeanServer
- the MBeanServer from which to retrieve the attribute valueobjectName
- the name of the MBean whose attribute is to be retrievedattributeName
- the name of the attribute whose value is to be retrieved- Returns:
- the current value of the MBean attribute, or
null
if the attribute is not readable or an error occurs
-
findMBeanAttributeInfo
@Nullable public static javax.management.MBeanAttributeInfo findMBeanAttributeInfo(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, java.lang.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 correspondingMBeanAttributeInfo
; otherwise, it returnsnull
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()); }
- Parameters:
mBeanServer
- the MBeanServer from which to retrieve the MBean attribute infoobjectName
- the name of the MBean whose attribute info is to be retrievedattributeName
- the name of the attribute for which metadata is requested- Returns:
- the
MBeanAttributeInfo
for the specified attribute if found;null
otherwise
-
getMBeanInfo
public static javax.management.MBeanInfo getMBeanInfo(javax.management.MBeanServer mBeanServer, javax.management.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 givenObjectName
. 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()); }
- Parameters:
mBeanServer
- the MBeanServer from which to retrieve the MBeanInfoobjectName
- the name of the MBean whose metadata is to be retrieved- Returns:
- the
MBeanInfo
for the specified MBean if found;null
otherwise
-
doGetAttribute
protected static java.lang.Object doGetAttribute(javax.management.MBeanServer mBeanServer, javax.management.ObjectName objectName, @Nullable javax.management.MBeanAttributeInfo attributeInfo, java.lang.String attributeName)
-
-