Question

What are some of the best practices in designing a JMX MBean? Any examples of ones you feel are especially useful?

Was it helpful?

Solution

Return absolute counts instead of rates. e.g. return total number of db commits, rather than deriving a rate.

By doing this, your clients can monitor and derive rates themselves, over whatever time periods they require. Perhaps more importantly, this protects the clients from missing surges in rates if they only connect infrequently.

If you're using JMX beans primarily via the HTML interface, then there are several practises I follow. The below often means that your JMX bean should wrap an existing bean (as opposed to just JMX-exposing existing methods):

  1. output properly formatted strings representing returned objects. Getting a default toString() output can be next to useless
  2. capture and display exceptions. Otherwise you'll likely get a blank page and have to go to the log files to determine what went wrong
  3. if you're displaying different character sets, you may have to escape the output appropriately to prevent display problems (I ran into this with a JMX control displaying our Chinese data)
  4. inputs to exposed methods should be sanitised properly (e.g. if you input a id as part of an operation you may wish to trim() it to remove whitespace etc.)

The above changes the emphasis from a bean simply exposed via JMX to something approaching a useable admin console.

OTHER TIPS

The first thing that got me with my first JMX bean was return types - it's a lot easier if your methods return Strings - that way your client can easily display the response (I was mainly working with JConsole) - if you don't do this, you get things like com.mycompany.Response@xxxx as a response, which doesn't mean much :)

Make sure that attributes have no side effects and are predictable in operation.

There is nothing worse than an innocent looking attribute that executes a time-consuming (or resource-consuming) operation. I've seen some humdingers in my time..

Do not use JMX for logging so, for example, don't use an MBean function that returns details of all the connections since startup.

One should remember that JMX is meant for monitoring. Meaning - Display only data that is relevant to the current moment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top