Saturday, September 6, 2008

JAMon Data for Java Applications

Recently i used JAMon for gathering important metric in one application which was not j2ee application. For JDK based application how do you get information in absence of JAMon Web Interface. I asked about it to Steve Souza who imeedaitedly returned with answer : MonitorFactory.getData() and MonitorFactory.getHeader(). Based on this data jsmons.jsp is written. For JDK based application i thought we can give same information via JMX. Below is small wrapeer to do

  • Register JAmon Factory as MBean and implement some key functions like Attributes and Operations


JAMonMBean mbean = new JAMon();
ObjectName name = new ObjectName("jamon.perf:type=JAMonBean");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(mbean, name);

  • JAMon Client


ObjectName name = new ObjectName("jamon.perf:type=JAMonBean");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
String arr[] = {key};
String[] signature = new String[] { "java.lang.String" };
obj = mbs.invoke(name,operationName, arr,signature );


  • Start background thread to store information in file


Object[][] data= client.getData();
// record format date : label : Hits : Avg : Total : StdDev
String timestamp = "" + System.currentTimeMillis();
if(data!=null)
{
for(int i=0;i<data.length;i++)
{
StringBuilder sb = new StringBuilder();
sb.append(timestamp).append(" : ");
sb.append(data[i][0]).append(" : ");
sb.append(data[i][1]).append(" : ");
sb.append(data[i][2]).append(" : ");
sb.append(data[i][3]).append(" : ");
sb.append(data[i][4]);
pw.println(sb.toString());
pw.flush();
//System.out.println(sb.toString());
}
}


Equipped with above, I have also written a small utility which outputs information of all counters periodically in the format

timestamp : label : avg. response time : hits : std. deviation

I have uploaded code used to demonstrate the JAMon Bean have been uploaded at http://tushar.khairnar.googlepages.com/perf-sample.zip.

Please see Sample Program for the same.

2 comments:

Arnaud said...

How did you create your JAMon MBean?

Geek said...

Hi,

Any class can be registered as mbean to Platform mbean or other mbean servers. Below is small example of Hello Mbean.

public interface HelloMBean {
// operations

public void sayHello();

// a read-write attribute called CacheSize of type int
public int getCacheSize();
public void setCacheSize(int size);

}


public class Hello implements HelloMBean {

public void sayHello() {
System.out.println("hello, world");
}


public int getCacheSize() {
return this.cacheSize;
}

/* Setter for the CacheSize attribute. To avoid problems with
stale values in multithreaded situations, it is a good idea
for setters to be synchronized. */
public synchronized void setCacheSize(int size) {
}

}


Sample Code for Creatring and Registering above Hello MBean.


MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

// Create the Hello World MBean
Hello mbean = new Hello();
mbean.setCacheSize(150);

try {
// Construct the ObjectName for the MBean we will register
ObjectName name = new ObjectName("com.sun.example:type=Hello");

// Register the Hello World MBean
mbs.registerMBean(mbean, name);
} catch (MalformedObjectNameException e) {
e.printStackTrace();
System.exit(0);
} catch (InstanceAlreadyExistsException e) {
e.printStackTrace();
System.exit(0);
} catch (MBeanRegistrationException e) {
e.printStackTrace();
System.exit(0);
}


I have uploaded code used to demonstrate the JAMon Bean have been uploaded at http://tushar.khairnar.googlepages.com/perf-sample.zip. Please see Sample Program for the same.

Hope you find it useful.

Thanks and Regards,
Tushar