Wednesday, January 30, 2008

Determining Your MBeanServer's MBeanServerId

A common need when using JMX is to know your particular JMX MBeanServer's MBeanServerId (AKA "Agent ID"). The Agent ID is most easily obtained by executing the following code against an MBeanServer.

* Get the MBeanServerId of Agent ID for the provided MBeanServer.
* @param aMBeanServer MBeanServer whose Server ID/Agent ID is desired.
* @return MBeanServerId/Agent ID of provided MBeanServer.
public String getMBeanServerId(final MBeanServer aMBeanServer)
String serverId = null;
final String SERVER_DELEGATE = "JMImplementation:type=MBeanServerDelegate";
final String MBEAN_SERVER_ID_KEY = "MBeanServerId";
ObjectName delegateObjName = new ObjectName(SERVER_DELEGATE);
serverId = (String) aMBeanServer.getAttribute( delegateObjName,

catch (MalformedObjectNameException malformedObjectNameException)
System.err.println( "Problems constructing MBean ObjectName: "
+ malformedObjectNameException.getMessage() );
catch (AttributeNotFoundException noMatchingAttrException)
System.err.println( "Unable to find attribute " + MBEAN_SERVER_ID_KEY
+ " in MBean " + SERVER_DELEGATE + ": "
+ noMatchingAttrException );
catch (MBeanException mBeanException)
System.err.println( "Exception thrown by MBean's (" + SERVER_DELEGATE
+ "'s " + MBEAN_SERVER_ID_KEY + ") getter: "
+ mBeanException.getMessage() );
catch (ReflectionException reflectionException)
System.err.println( "Exception thrown by MBean's (" + SERVER_DELEGATE
+ "'s " + MBEAN_SERVER_ID_KEY + ") setter: "
+ reflectionException.getMessage() );
catch (InstanceNotFoundException noMBeanInstance)
System.err.println( "No instance of MBean " + SERVER_DELEGATE
+ " found in MBeanServer: "
+ noMBeanInstance.getMessage() );
return serverId;

The MBeanServer passed into the method shown above could be a newly created one using one of the MBeanServerFactory.createMBeanServer methods or could be an MBeanServer obtained via a call to ManagementFactory.getPlatformMBeanServer(). This method could be added to any JMX MBeanServer-hosted code to determine the agent ID for that JMX Server.

There are really only two lines of "substantial" code in the example above and they are in bold. Much of the rest of the code is set-up code and exception handling. The exceptions caught in the code above are all checked exceptions, but the code would have been shorter if I had declared the method to simply throw these exceptions or if I had simply caught a general Exception. I caught each individual checked exception intentionally to be more illustrative of what exceptions could get thrown and why they might be thrown.

If the above code is run within code like this:

"MBeanServerId: "
+ me.getMBeanServerId( ManagementFactory.getPlatformMBeanServer() ));

the output shown (when run on a machine named "HOMER" with the Java SE 6 JMX Platform Server/Reference Implementation) is:

MBeanServerId: HOMER_1201751774839

The long numeral after HOMER_ is a timestamp for the MBean server and so will almost certainly differ for your MBean server.

No comments: