Monitoring Java application with AspectJ and JAMon

JAMon (Java Application Monitor) is a Java API used to monitor the performance of Java applications.

Aspect-oriented programming (AOP) is a way of modularizing crosscutting concerns, such as monitoring, logging, error handling, ..etc. AspectJ is an implementation of aspect-oriented programming for Java.

In this tutorial, we will use JAMon and AspectJ to monitor methods in a Java web application, so we are going to have statistics on :

  • Hits : how many times a method was executed.
  • Total : total method execution times.
  • Avg : average method execution times.
  • etc..

The role of AspectJ is to intercept each method execution and then apply some JAMon statements to gather statistics.

The collected statistics will be displayed as HTML table via jamonadmin.jsp page.

 

1. Technologies used

  • JAMon 2.74
  • AspectJ 1.6
  • JSF 2.2.8
  • Tomcat 7.0

 

2. Create the web application to monitor

We start by creating the web application we are going to monitor.

2.1 Application structure :

Monitoring Java application with Aspectj and JAMon-0

 

2.2 Application controller

In addition of the getter and setter methods of the property name, we are going to add three other methods to implement navigation rules.

File : ViewController.java

 

2.3 Application web pages

We create some JSF pages in which we are going to call our methods.

Page 1 : index.xhtml

Page 2 : welcome.xhtml

Page 3 : page1.xhtml

 

3. Create an AspectJ application

The object of this application is to monitor each methods in the ViewController class.

Aspect-Profiler

We need to add JAMon JAR (jamon-2.74) to our project.

 
3.1 Create our aspect

Here is our aspect definition in AspectJ :

File : AspectProfiler.aj

We have defined our pointcut pcut() as follows :

  • include execution of any method within the com.keylesson.view package (or any subpackage under that), with (or with no) arguments , regardless of return (or parameter) types.
  • exclude call to any method where the call is made from : our aspect itself, any class within the com.jamonapi package (or any subpackage under that), and any class within the com.fdsapi  package (or any subpackage under that).

In other words, our pointcut will track all methods in ViewController class.

Then, we have defined our advice around() to implement what to do when the pointcut is verified…we get method signature, start monitoring, let method execute, gather statistics, stop monitoring and return flow to method.

 
3.2 Export our AspectJ project as JAR file

Monitoring Java application with Aspectj and JAMon-3

 
3.3 Enable load time weaving

The aspect weaver is going to merge our aspect and our class in order to generate a woven class (obtained by injecting the aspect code into our class).

We first configure the weaver via an XML file (META-INF/aop.xml) : declaring classes to include/exclude to/from the weaving process.

 

Then we put the following libraries into Tomcat lib (../Tomcat 7.0/lib/) : jamon-2.74.jar, aspectjrt.jar, aspectjweaver-1.6.jar, Aspect-Profiler.jar

And finally, we lunch the JVM with the AspectJ agent registered, so we need to add the following option to JVM parameters : -javaagent:/usr/local/apache-tomcat-7.0.23/lib/aspectjweaver-1.6.jar" 

On windows OS :

Monitoring Java application with Aspectj and JAMon-4

 

On Linux OS : we need to change in the catalina.sh file, by adding the option to JAVA_OPTS  settings :

JAVA_OPTS="$JAVA_OPTS ... -javaagent:/usr/local/apache-tomcat-7.0.23/lib/aspectjweaver-1.6.jar"

Monitoring Java application with Aspectj and JAMon-5

 

4. Deployment 

In addition to our web application, we need to deploy the jamon.war

Put both WAR files into Tomcat webapps

 

5. Test It

Open the browser, and type in http://localhost:8080/JSF-JamonAPI-Java/

Enter a name and hit the button to go to the welcome page,  then go the page 1, then return to welcome page and finally press exit button to return to index page.

To view statistics collected about our application methods execution, go to http://localhost:8080/jamon/jamonadmin.jsp

Monitoring Java application with Aspectj and JAMon-8

Et voilà!

 

Download source code