Creating MBeans for your CQ5 application

JMX is the de-facto standard for monitoring java processes and applications. A lot of monitoring systems have the ability to consume these data.

By default CQ 5.5 has a number of MBeans, which offer runtime information about internal state. Most interesting ones are the MBeans about the repository state and the replication MBeans. But it isn’t hard to create your own MBeans, so you provide information about the internal state of your application to the monitoring system; or you can monitor resources which are critical to your application and use case.

In Cq5 we are working in a OSGI environment, so we will use one of my favorite patterns, the OSGI whiteboard pattern. We will use the JMX-Whiteboard bundle of the Apache Aries project to register services to JMX. Also that implementation is very short and understandable and shows the power of the whiteboard pattern. (I already had a short blog entry on this last year.)

In this example I want to demonstrate it on an already existing counter, the total number of requests handled by sling.It requires CQ 5.5, where the JMX whiteboard bundle is already deployed by default; but if you install the JMX Whiteboard bundle yourself, you can also use older versions of CQ5.

And it goes like this:

1. Build JMX interface

That’s a regular Java interface, which ends with the “MBean” string. This interface provides the methods and getters/setters you want to expose to JMX.

package sample.jmx;
public interface RequestCounterMBean {
int getRequestCounter();
}

2. Add the implementation class

As next step we have the implementation class. To make it easy, I implement the counter as a servlet filter, which just increments an counter.

package sample.jmx;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;

// add OSGI exports for all implementing classes
@Service
@Component(immediate=true,metatype=true)
@Properties({
  @Property(name="jmx.objectname",value="sample.jmx:id='requestCounter'"),
  @Property(name="pattern",value="/.*")
})
public class RequestCounter implements RequestCounterMBean, Filter{

  private int count = 0;

  public int getRequestCount() {
    return count;
  }

  public void destroy() {
    // nothing to do
  }

  public void doFilter(ServletRequest request, ServletResponse response, 
      FilterChain chain) throws IOException, ServletException {
    count++;
    chain.doFilter(request, response);
  }

  public void init(FilterConfig arg0) throws ServletException {
    // nothing to do
  }
}

3. That’s it.

The magic lies in the Annotations: @Service without any other parameter registers this service as implementation of all interfaces we implement on the class, in this case as ServletFilter and as MBean. So when this service is started, it is registered as an implementation of the RequestCounterMBean interface; as this class name ends with “MBean”, it is tracked by the JMX whiteboard service and then registered to all available MBean servers und the Objectname “sample.jmx:id=’requestCounter'”.

Now you should be able to export any interesting information of your CQ5 application as a MBean. Of course there are other ways to export data via JMX, but I consider this one as a very simple approach, which is very easy to understand and use.

JConsole showing our own MBean

One thought on “Creating MBeans for your CQ5 application

  1. Great post. The whiteboard pattern make these things so much easier to integrate.

    You can also double click on the property in VisualVM to chart changes to the property over time.

    For tracking things like request counts and timings Coda Hale’s OSGi-friendly Metrics library (http://metrics.codahale.com/getting-started/) is excellent. It also exposes information over JMX.

Comments are closed.