AspectJ: Using AOP for Performance Monitoring
March 29, 2007 – 6:38 pmOne of the great features of Java 5 (see: Pro Java 5 ) is Aspect technology. Here, I'll show you how you can use custom Aspects to instrument your application and monitor using AspectJ and Jamon.
Java 5 Aspects
First, a brief primer primer on Aspects (see the AspectJ web site for more info). An aspect consists of 3 items: joinpoints, pointcuts and advice.
From the AspectJ FAQ:
What are join points?
Join points are well-defined points in the execution of a
program. Not every execution point is a join point: only those
points that can be used in a disciplined and principled manner are.
So, in AspectJ, the execution of a method call is a join point, but
"the execution of the expression at line 37 in file Foo.java" is
not.The rationale for restricting join points is similar to the
rationale for restricting access to memory (pointers) or
restricting control flow expressions (goto) in
Java: programs are easier to understand, maintain and extend
without the full power of the feature.AspectJ join points include reading or writing a field; calling
or executing an exception handler, method or constructor.A pointcut picks out
join points
. These join points are described by the pointcut
declaration. Pointcuts can be defined in classes or in aspects,
and can be named or be anonymous.Advice is code that executes at each
join point picked out by a
pointcut. There are three
kinds of advice: before advice, around advice and after advice. As
their names suggest, before advice runs before the join point
executes; around advice executes before and after the join point;
and after advice executes after the join point. The power of
advice comes from the advice being able to access values in the
execution context of a pointcut.
Jamon
Jamon is an API that provides a way to easily create timers for any parts of your application and view the data in a web application. With just a few small changes to your J2EE web application configuration, you can create monitors for your servlets, your JDBC connections, and, using custom aspects, any of your applications methods, without changing any of your application code.
Sample Code
Here's a sample Aspect that monitors all of the public methods in the classes and subclasses of com.myorg.important.package:
package com.myorg.aspect;import com.jamonapi.Monitor; import com.jamonapi.MonitorFactory;public aspect MyJamonAspect() {pointcut MyCriticalMethods() : execution(public * com.myorg.important.package..*(..));Object around(): MyCriticalMethods() {Monitor monitor = MonitorFactory.start(thisJoinPoint.toString()); Object ret = proceed(); monitor.stop(); return ret;}}
Setup
1. Save your aspect (e.g. MyJamonAspect.aj) and then compile with the AspectJ compiler, ajc. Be sure to set your classpath with the jamon.jar file.
2. Deploy the jamon web app to your J2EE application server
3. Create an aop.xml file in the META-INF directory of your application. Here is a sample:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> <aspectj> <weaver> <include within="com.myorg..*"/> </weaver> <aspects> <aspect name="com.myorg.aspects.MyJamonAspect" /> </aspects> </aspectj>
4. Add the aspectj agent to the app server command line: -javaagent:path/to/aspectjweaver.jar
5. Start your application, and you should see your instrumented methods in the jamon web application. Add the jamon servlet filter and JDBC monitor, and you’ve got great insight into your application’s performance for FREE
