Friday January 13, 2006 ![]() |
JMX, SNMP, Java, etc...Daniel Fuchs blogs on JMX, SNMP, Java, etc... |
... or why is ThreadMXBean a singleton MBean? The flu has been plaguing me for a week, and last night I couldn't find sleep. So at 3am in the morning, I gave up. I switched on the light, grabbed a pen and a wad of paper, and started writing about the ThreadMXBean. Actually the ThreadMXBean is more like a pretext, and what I really wanted to do - besides writing me to sleep, was talking about modeling with JMX, and laying out the ground for a coupled of further entries that I'm planning to write. In his blog, Eamonn McManus has already begun to write about modeling guidelines. ThreadMXBean is in fact a perfect example of non trivial design decisions which provide interesting insights on modeling issues. But let's start at the beginning.
One of the great new feature that was introduced in
Java SE 5.0 (Tiger)
is the
Monitoring and Management API (to make it shorter: M&M).
Why does the M&M API expose a single MBean, providing access to all threads, rather than simply exposing one MBean per thread? Wouldn't this have been simpler? Well - you might already have your own idea about that, but you will see that we will find more reasons than simple performance issues.
The ThreadMXBean offers methods like getDaemonThreadCount(), getPeakThreadCount(), findMonitorDeadlockedThreads(), etc... which is higher value aggregated information that naturally finds its place on an MBean that provides management and monitoring for all threads. This might not strike you as such a big deal at first, but it does have several advantages: mainly that the code you have to write in the managing application is often much simpler. Had we registered one MBean per thread, without taking care of data consistency, we might have ended up exposing uncorrelated pieces of thread information in the form of uncorrelated MBean attributes. With a single MBean, the M&M API doesn't need to perform any operation on threads if none is explicitly requested by a management application. Had we chosen to model Threads as individual MBeans, the M&M API would have needed to monitor the creation/deletion of threads, just for the purpose of registering/deregistering the associated MBeans. Registering/deregistering MBeans can be a costly process since it involves in particular: But then you might ask, what if I precisely wanted to receive a notification when a new thread was created, or a thread was terminated? What if I wanted to be able to register for AttributeChangedNotification in order to monitor the state of a a thread? Aren't those natural MBean operations, and doesn't this imply that Threads should have been MBeans? Well, not necessarily, because there's none of these actions that couldn't be implemented also through the ThreadMXBean singleton. Had we had these requirements, nothing would have prevented us from making the ThreadMXBean emit notifications about thread creation/termination, or about state changes. As you may have figured by reading these lines, there is more about designing a management interface than to force-feeding your application objects into MBeans. In a next blog yet to come (hopefully not through a sleepless night) I'll try explaining why the Java SE SNMP agent - through its JVM_MANAGEMENT_MIB, doesn't provide access to thread stack traces. That will be an interesting story, nicely linked to this one, but too long for this night. Sleep well!
|