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 to null 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.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button