PerformanceInterceptorAspect.java

package com.tradecloud.annotations;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StopWatch;

/**
 * @author pvzyl 14 Dec 2011
 * <p>
 * Wanted to use
 * org.springframework.aop.interceptor.PerformanceMonitorInterceptor but
 * this didn't seem to work that well with using aspect annotations and
 * @Around. In the end used the stopwatch they used and checked the concepts in
 * AbstractTraceInterceptor. Not sure if this will give us better
 * performance if we use the spring interceptor....
 * <p>
 * http://www.javalobby.org/java/forums/t44746.html
 * "http://stackoverflow.com/questions/4829088/java-aspect-oriented-programming-with-annotations"
 * <p>
 * "http://stackoverflow.com/questions/1079343/java-simple-technique-for- annotation-based-code-injection"
 * "http://stackoverflow.com/questions/6448471/spring-aop-aspect-not-executing"
 * <p>
 * http://java.dzone.com/articles/spring-aop
 * <p>
 * https://jira.springsource.org/browse/SPR-4557
 * <p>
 * Ouch: https://jira.springsource.org/browse/SPR-4557
 * <p>
 * http://www.javaworld.com/javaworld/jw-01-2007/jw-0105-aop.html?page=
 * 2
 */
@Aspect
public class PerformanceInterceptorAspect {

    private static Logger log = Logger.getLogger(PerformanceInterceptorAspect.class);

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public PerformanceInterceptorAspect() {

    }

    @Around(value = "@annotation(PerformanceInterceptor)")
    public Object measureRunningTime(ProceedingJoinPoint joinPoint) {

        StopWatch stopWatch = null;

        try {
            log.info("Method name: " + joinPoint.getSignature().getName());

            String name = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();

            stopWatch = new StopWatch(name);
            stopWatch.start(name);

            final Object returnValue = joinPoint.proceed();

            return returnValue;
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            stopWatch.stop();
            log.info(stopWatch.shortSummary());
        }
        return null;

    }

}