Monitoring spring based applications including hibernate statistics using JMX

Spring Framework is by the most versatile framework for java applications. It can be used with any kind of application, be it an applet, a swing app or a web application.

Spring transaction management is one of the most powerful features of spring. It manages your transactions for you with minimal code, infact all you need to do is put a few lines of code in your configuration file and then annotate your classes/methods with the @Transactional annotation. Combining this with ORM tools such as hibernate, what you have is full fledged transaction management running in under 5 minutes.

Monitoring spring applications using JMX is another thing. Spring’s JMX docs are a worthwhile read but if you just want to get your monitoring up in a hurry, you’re kinda stuck.

I decided to post the minimal configuration required to start monitoring a spring based application. I’m going to monitor loggers and their levels in my application.

First the configuration, spring-jmx.xml. For some reason my WP install won’t let me post XML’s properly so i’ve uploaded the file. Please download it and take a look at it.

Next is the SpringStart.java needed for the demo :

package com.spring.jmx;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringStart {
	public static void main(String[] args) {
		FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("spring-jmx.xml");
		while (true) {
			try {
			Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

Once all this is done run the program with the following extra VM arguments :

-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port="9004" -Dcom.sun.management.jmxremote.authenticate="false" -Dcom.sun.management.jmxremote.ssl="false"

If you want to enable authentication etc then it’s best to read this article.

Open your JConsole, connect to SpringStart, click on the MBeans tab. Here you’ll see all your MBeans listed, even those defined by you. You can view/edit/set their properties from this console as long as you have rights to do so.

It’s also possible to monitor hibernate statistics through the console. The configuration for the same is already present in the configuration file, it’s easy to figure out which part it is. ;)

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • Reddit
  • Digg
  • Facebook
  • Netvibes
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Print
  • Slashdot
  • Share/Bookmark

Tags: , , , ,

Leave a Reply