Thursday, 24 November 2011

AOP Timer

    <aop:aspectj-autoproxy />
    <bean id="timer" class="AopTimer" />

----------




@Aspect
public class AopTimer
{
  private static final Logger logger = Logger.getLogger(AopTimer.class.getName());

  public AopTimer()
  {
    logger.info("---------- instantiating ----------");
  }

  @Around("@annotation(com.helpdeskSos.aop.TimeIt)")
  public Object time(ProceedingJoinPoint jp) throws Throwable
  {
    long start = new Date().getTime();
    try
    {
      return jp.proceed();
    }
    finally
    {
      long end = new Date().getTime();
      String method = jp.getTarget().getClass().getSimpleName() + "." + jp.getSignature().getName();
      logger.info("ellapsed time in " + method + ": " + (end - start) + " (ms)");
    }
  }
}

----------




@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimeIt
{

}


----------

  @TimeIt
  public void sendMail(Incident incident)