JMX

Today I joined two sessions on JMX, one was given by Eamonn McManus's and one of the new JMX features coming in JMX 1.3 (which will be part of Mustang) is the possibility to directly query attributes of a composite element. So for example you MBean has a ThreadInfo attribute, which is an object that has attributes in itself. Using this new feature you can directly query one of the attributes of the ThreadInfo attribute by addressing it as threadInfo.isActive.

In the last project we did we ran into the similar problem and solved it by utilizing commons-jxpath and commons-modeler of the Jakarta-commons project. What we did was define a custom MBean implemenetation class com.sun.util.jmx.JXPathEnabledMBean in the mbean-descriptiors file of commons-modeler like this:

<mbean         
  name="Foo"
  description="Foo something."
  className="com.sun.util.jmx.JXPathEnabledMBean"
  type="com.sun.x.y.Foo">
The className attribute overrides the default ModelMBean of commons-modeler. In the JXPathEnabledMBean class we used JXPath in the getAttribute() and setAttribute() methods to drill down to the requested attribute in case the requested attribute an atomic member of the class managed object itself. As an example the getAttribute() method:
public class JXPathEnabledMBean extends BaseModelMBean {

  public Object getAttribute(final String name)
      throws AttributeNotFoundException, MBeanException, ReflectionException {

    try {
      return super.getAttribute(name);
    } catch (ReflectionException re) {
      // Attribute not an atomic attribute of managed object
      // Try using JXPath...
      JXPathContext context;
      try {
        Object object = getManagedResource();
        context = JXPathContext.newContext(object);
        // JXPath uses '/' to as separator...
        String attName = name.replace('.', '/');
        return context.getValue(attName);
      } catch (Exception e) {
        // Here we know the attribute is not retrievable at all.
        logger.error("Failed getting attribute via JXPath", e);
        throw re;
      }
    }
  }

.....
}

0 comments:

Post a Comment