Mar 14, 2014

Yammer Metrics with Spring tutorial

When you are running long term applications like web applications, batch jobs, or stand-alone status update jobs, it is good to know some statistics about them, like number of requests served or request duration. You can also gather more generic information like the state of your internal collections, how many times some portion of code is being executed, or health checks like database availability, or any kind of connection to an external system.

All this kind of instrumentation can be achieved by using native JMX or using a modular project like yammer Metrics. Metrics provides a powerful way to measure the behaviour of your critical components and reporting them to a variety of systems like, JConsole, System Console, Ganglia, Graphite, CSV, or making them available through a web services as JSON data.

Step 1: Required libraries. The metrics-core, metrics-annotation, and metrics-spring are the key libraries for gathering metrics.

Step 2: Artifacts used.

Step 3: Let's create a very basic TradeEngine, and monitor the number of requests and request execution times.

package com.writtentest12;

public interface TradeEngine {
    abstract void execute(Request... requests);

package com.writtentest12;

import java.util.concurrent.atomic.AtomicInteger;
import com.yammer.metrics.annotation.ExceptionMetered;
import com.yammer.metrics.annotation.Gauge;
import com.yammer.metrics.annotation.Timed;

public class TradeEngineImpl implements TradeEngine {

 private final AtomicInteger currentRequests = new AtomicInteger();

 public void execute(Request... requests) {
  try {
   Thread.sleep(2000); //just to emulate some processing
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block

 private void executingRequests(int count) {

Take notice of @Gauge, @Timed, and @ExceptionMetered annotations used for monitoring. The Request class used in the TradeEngine.

package com.writtentest12;

public class Request {


Step 4: Spring applicationContext.xml file to wire up TradeEngine, metrics-registry, and jmx-reporter.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <metrics:metrics-registry id="trade-metrics"/>
    <metrics:health-check-registry id="trade-health"/>
    <metrics:annotation-driven metrics-registry="trade-metrics" />
    <metrics:jmx-reporter metrics-registry="trade-metrics"/>
 <bean id="tradeEngine" class="com.writtentest12.TradeEngineImpl"/>

Step 5: Stand-alone TradeEngineMain class that runs until forcefully stopped. Run continuously so that you can monitor the metrics via JMX compliant jconsole.

package com.writtentest12;

import org.springframework.context.ApplicationContext;

public class TradeEngineMain {

 public static void main(String[] args) {

  ApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "com/writtentest12/applicationContext.xml" });

  TradeEngine tradeEngine = (TradeEngine) context.getBean("tradeEngine");

  try {
   while (true) {
    Request[] requests = new Request[2];
    requests[0] = new Request();
    requests[1] = new Request();

  } catch (InterruptedException e) {


You can run the  TradeEngineMain  application, and then start the jconsole from a command-line and monitor the number of requests processed and the stats on the execution time. I will cover this in the next post.

Labels: , ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home