The Implementation

Create an implementation of this interface that inherits from curam.util.jmx.CuramMBeanAbstract. To make it easier further on, derive the name of this class from the name of the implemented MBean by removing the MBean suffix. The super class provides the MBean with access to the application configuration via the execution context and it facilitates the handling of changes in application configuration data that might be of interest to the MBean.

Figure 1. A custom MBean implementation
package com.mytest;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.openmbean.
                       CompositeDataSupport;
import javax.management.openmbean.
                       CompositeType;
import javax.management.openmbean.
                       OpenDataException;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.
                       TabularDataSupport;
import javax.management.openmbean.TabularType;

import curam.util.jmx.CuramMBeanAbstract;

public class MyStats extends CuramMBeanAbstract
      implements MyStatsMBean {
private static final Logger log = Logger
     .getLogger(MyStats.class.getName());

private static final OpenType[] kItemTypes
                        = new OpenType[] {
  SimpleType.STRING,
  SimpleType.LONG,
};

private static final String[] kItemNames
                = new String[] {
   "Item",
   "Execution time(ms)"};

private static final String[] kItemDescriptions
               = new String[] {
   "The name of the item",
   "The execution time in milliseconds"};

private static TabularType stTabularType;

private static CompositeType stRowType;

private static MyStats instance;

static {
  try {
    stRowType = new CompositeType(
        "MyStatsType", "My statistics",
         kItemNames, kItemDescriptions, kItemTypes);
    stTabularType = new TabularType(
        "MyStats", "My statistics",
        stRowType, new String[] { kItemNames[0]});
  } catch (Exception e) {
    log.log(Level.SEVERE,
         "Failed to create the open types.", e);
  }
}

public MyStats() {
  super();
  instance = this;
}

/* (non-Javadoc)
 * @see com.mytest.MyStatsMBean#getStats()
 */
public TabularData getStats()
      throws OpenDataException {
  if (stRowType == null
           || stTabularType == null) {
    return null;
  }
  TabularDataSupport sup = new TabularDataSupport(
           stTabularType);

  // sample stats
  Object[] values = new Object[2];
  // ...
  // get the values
  // ...
  CompositeDataSupport cd =
                        new CompositeDataSupport(
    stRowType, kItemNames, values);
  sup.put(cd);
  return sup;
}

/**
 * This method is invoked from instrumented code
 * to update the statistics for
 * <code>item</code>.
 * @param item the item to update statistics for
 * @param executionTime the execution time
 */
public static void updateStats(
           String item, long executionTime) {
  if(instance != null) {
    instance._updateStats(item, executionTime);
  }
}

/**
 * This method is invoked by the JMX infrastructure when
 * a request is made to reset the JMX statistics.
 * @see com.mytest.MyStatsMBean#reset()
 */
public void reset() {
 // ...
 // reset the statistics
 //...
}

private void _updateStats(String item, long executionTime) {
  // ...
  // update the items average execution time
  // ...
}
}

More complex MBeans that require dynamic configuration parameters or support per user data collection can override or utilize the provided protected methods in curam.util.jmx.CuramMBeanAbstract.