jmx
List all JVM MBeans
With this example we are going to demonstrate how to list all JVM MBeans. An MBean is a managed Java object, similar to a JavaBeans component, that follows the design patterns set forth in the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed. MBeans expose a management interface that consists of a set of readable or writable attributes, or both, a set of invokable operations and a self-description. In short, to list all JVM MBeans you should:
- Create an MBeanServer that is the interface for MBean manipulation on the agent side. Use
getPlatformMBeanServer()
API method of ManagementFactory. - Use
queryMBeans(ObjectName name, QueryExp query)
API method of MBeanServer to get all the MBeans controlled by this MBean server. Since all parameters are set tonull
all MBeans are to be selected. - Iterate over the Set of ObjectInstance objects to get the name and class of each MBean.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.enterprise; import java.lang.management.ManagementFactory; import java.util.Iterator; import java.util.Set; import javax.management.MBeanServer; import javax.management.ObjectInstance; public class ListAllJVMMBeans { public static void main(String[] args) throws Exception { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); Set<ObjectInstance> instances = server.queryMBeans(null, null); Iterator<ObjectInstance> iterator = instances.iterator(); while (iterator.hasNext()) { ObjectInstance instance = iterator.next(); System.out.println("MBean Found:"); System.out.println("Class Name:t" + instance.getClassName()); System.out.println("Object Name:t" + instance.getObjectName()); System.out.println("****************************************"); } } }
Output:
MBean Found:
Class Name: com.sun.management.OperatingSystem
Object Name: java.lang:type=OperatingSystem
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Tenured Gen
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Perm Gen
****************************************
MBean Found:
Class Name: java.util.logging.Logging
Object Name: java.util.logging:type=Logging
****************************************
MBean Found:
Class Name: sun.management.CompilationImpl
Object Name: java.lang:type=Compilation
****************************************
MBean Found:
Class Name: javax.management.MBeanServerDelegate
Object Name: JMImplementation:type=MBeanServerDelegate
****************************************
MBean Found:
Class Name: sun.management.MemoryImpl
Object Name: java.lang:type=Memory
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Survivor Space
****************************************
MBean Found:
Class Name: sun.management.RuntimeImpl
Object Name: java.lang:type=Runtime
****************************************
MBean Found:
Class Name: sun.management.GarbageCollectorImpl
Object Name: java.lang:type=GarbageCollector,name=Copy
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Eden Space
****************************************
MBean Found:
Class Name: sun.management.GarbageCollectorImpl
Object Name: java.lang:type=GarbageCollector,name=MarkSweepCompact
****************************************
MBean Found:
Class Name: sun.management.ThreadImpl
Object Name: java.lang:type=Threading
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Perm Gen [shared-ro]
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Perm Gen [shared-rw]
****************************************
MBean Found:
Class Name: sun.management.HotSpotDiagnostic
Object Name: com.sun.management:type=HotSpotDiagnostic
****************************************
MBean Found:
Class Name: sun.management.ClassLoadingImpl
Object Name: java.lang:type=ClassLoading
****************************************
MBean Found:
Class Name: sun.management.MemoryManagerImpl
Object Name: java.lang:type=MemoryManager,name=CodeCacheManager
****************************************
MBean Found:
Class Name: sun.management.MemoryPoolImpl
Object Name: java.lang:type=MemoryPool,name=Code Cache
****************************************
This was an example of how to list all JVM MBeans.