Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

Continuing on the theme of a ServiceAware service, what if you wanted to just be able to extract certain statistics from your Mule services at run time?

I added the following method to the ServiceAware component that I wrote about in last week’s post:

public String returnStatistics (Integer theRequest) {
  switch (theRequest) {
	case 0: return "Asynchronous messages received: "+
		myService.getStatistics().getAsyncEventsReceived();
	case 1: return "Synchronous messages received: "+
		myService.getStatistics().getSyncEventsReceived();
	case 2: return "Asynchronous messages dispatched: "+
		myService.getStatistics().getAsyncEventsSent();
	case 3: return "Synchronous messages sent: "+
		myService.getStatistics().getSyncEventsSent();
	case 4: return "Average Execution Time: "+
		myService.getStatistics().getAverageExecutionTime();
	case 5: return "Number of Errors: "+
		myService.getStatistics().getFatalErrors();
  }
  return NO_STATISTICS;
}

It’s a simple enough method that proves my point. Given a code, the method will extract and return a message containing the right information. This is an application of the Control Message pattern. By sending a unique message along any existing endpoint, I can tell the service to perform some arbitrary function unrelated to the normal message flow. In fact, I can easily think of a StatisticQuery structure which would return an object that contains all the relevant statistics.

The code works beautifully and it’s pretty cool to be able to see how many messages have been received and/or sent synchronously or asynchronously. One key point to remember is that the statistics will always show as zero unless you enable statistics collection using JMX. For this, add the following line of XML to your Mule configuration file: