At some point during a project, you will invariably run into the problem of needing to record the execution time of a portion of your system. In the case I recently ran into, I suspected that the performance of an external web service call degraded over time. So what was the simplest solution to show the execution times for these calls without having to install additional tools?
The first idea that came to mind was using a Java Aspect to wrap the method execution. Searching for a sample implementation, I found a few samples. The following looked promising:

public class LogExecutionTime {
  private static final String LOG_MESSAGE_FORMAT = "%s.%s execution time: %dms";
  private static final Log LOG =   LogFactory.getLog(LogExecutionTime.class);
  @Pointcut("execution(something goes here")
  public void myPointCut() {}
  public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
    StopWatch stopWatch = new StopWatch();
    Object retVal = joinPoint.proceed();
    logExecutionTime(joinPoint, stopWatch);
    return retVal;
  private void logExecutionTime(ProceedingJoinPoint joinPoint, StopWatch stopWatch) {
    String logMessage = String.format(LOG_MESSAGE_FORMAT, joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName(), stopWatch.getTime());;

That was easy. The only part of the implementation I still needed was the pointcut expression. I have always hated the fact that I have to know in advance where to apply the aspect. Recalling Joel Brinkman’s blog Cast Away with AspectJ, I decided to use a custom annotation. I created a small annotation class:

public @interface LogMe {
  String value() default "";

After implementing the annotation I had to make some adjustments to the implementation of the aspect.

@Pointcut("execution(@com.somecompany.erp.util.LogMe * *(..))")
public void isAnnotated() {}
public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {

After implementing this piece, I only needed to add the annotation @LogMe to a method and the execution would be logged to the logger configured in the
The entire implementation took about 15-20 minutes and gave me a flexible tool would allow me to log the execution time of any method call simply by adding my annotation.