Monday, March 21, 2011

JDK 7 JMX Platform Management Beans: A First Peek

One of the new features anticipated for JDK 7 is advertised in the Java SE 7 preview as Enhanced JMX Agent and MBeans. In this post, I look at portions of the enhanced JMX and MXBean support. Specifically, I briefly cover new-to-JDK7 PlatformManagedObject interface and ManagementFactory.getAllPlatformMXBeanInterfaces() method.

The JDK 7 New Features section on "Enhanced JMX Agent and MBeans" states the following about this area of new features:
An implementation-specific enhanced JMX management agent, ported from JRockit, which makes it easier to connect to the platform MBean server through firewalls, together with a richer set of MBeans which expose additional information about the internal operation of the VM

One of the convenient methods available in the preview release (Milestone 12 AKA M12) version of JDK 7 (b134 in my case) is ManagementFactory.getAllPlatformMXBeanInterfaces(). The current documentation for this method indicates that this method has been available since 1.7 and states: "Returns a list of Class objects, subinterface of PlatformManagedObject, representing all management interfaces for monitoring and managing the Java platform."

The referenced PlatformManagedObject is an interface that is new with JDK 1.7. This interface is extended by new BufferPoolMXBean (part of java.nio package), ClassLoadingMXBean, CompilationMXBean, GarbageCollectorMXBean, MemoryManagerMXBean, MemoryMXBean, MemoryPoolMXBean, OperatingSystemMXBean, java.lang.management.PlatformLoggingMXBean (new to 1.7 but extends previously available java.util.LoggingMXBean in addition to extending PlatformManagedObject), RuntimeMXBean, and ThreadMXBean. A side note here is that the new interface PlatformMXBean extends LoggingMXBean but does not add any new methods. Instead, its purpose is, as the documentation states, to "simply unif[y] LoggingMXBean PlatformManagedObject."

It is very easy to programatically access the available "management interfaces for monitoring and managing the Java platform" with the ManagementFactory.getAllPlatformMXBeanInterfaces() method. The next simple Groovy listing can be used to see these.

#!/usr/bin/env groovy
// getAllJava7PlatformMxBeansInterfaces.groovy
//
// Main script for demonstrating how simple it is to ask what interfaces are
// available for monitoring and managing in JDK 7.
//

java.lang.management.ManagementFactory.getAllPlatformMXBeanInterfaces().each
{
   println it
}

The above Groovy script is simple, but is enough to print out the interface class names provided by the ManagementFactory.getAllPlatformMXBeanInterfaces() method. The output from this simple script is shown next (I've included Java version information for reference).


The script is so simple that it can be evaluated inline using Groovy's command line -e option rather than calling an external script file with the simple command:

groovy -e "java.lang.management.ManagementFactory.getAllPlatformMXBeanInterfaces().each{println it}"

This is shown in the next screen snapshot.


In both cases (script file or invoked inline), the output indicates several available interfaces for managing and monitoring:
interface java.lang.management.ClassLoadingMXBean
interface java.lang.management.CompilationMXBean
interface java.lang.management.MemoryMXBean
interface java.lang.management.GarbageCollectorMXBean
interface java.lang.management.MemoryManagerMXBean
interface java.lang.management.MemoryPoolMXBean
interface java.lang.management.OperatingSystemMXBean
interface java.lang.management.RuntimeMXBean
interface java.lang.management.ThreadMXBean
interface java.util.logging.PlatformLoggingMXBean
interface java.nio.BufferPoolMXBean
interface com.sun.management.GarbageCollectorMXBean
interface com.sun.management.OperatingSystemMXBean
interface com.sun.management.UnixOperatingSystemMXBean
interface com.sun.management.HotSpotDiagnosticMXBean

In this case, there are the nine MXBeans in the java.lang.management package, one MXBean in the java.util.logging package, and one MXBean in the java.nio package. Additionally, there are four MXBeans provided in the platform-specific com.sun.management package. The four HotSpot-specific MXBeans extend the standard MXBeans of similar names and are HotSpotDiagnosticMXBean, GarbageCollectorMXBean, OperatingSystemMXBean, and UnixOperatingSystemMXBean.

Conclusion

The examples covered in this post demonstrate a few things of interest. It demonstrates direct use of ManagementFactory.getAllPlatformMXBeanInterfaces(), indirect use of the new PlatformManagedObject interface, and the ease of using Groovy to play with these new features.

1 comment:

Dustin said...

Augusto Sellhorn's post Hidden Java 7 Features – System and Process CPU Load Monitoring talks about the new MXBean support in Java SE 7. Of particular interest is the discussion focused on the implementation-specific com.sun.management.OperatingSystemMXBean (which extends java.lang.management.OperatingSystemMXBean). The post's author points out that this does not work on Windows.