[ajug-members] AspectJ question

Dan Marchant driedtoast at gmail.com
Thu Sep 7 06:52:15 EDT 2006


What you are looking for is around.

public aspect PerformanceAspect {
       pointcut largeMethods(int i): call(* get*(..)) && args(i);
       Logger log = Logger.getLogger("PerformanceAspect");

       Object around(int i): largeMethods(i) {
         long start = System.currentTimeMillis();
         Object obj = proceed(i*2);
         long end = System.currentTimeMillis();
         Signature sig = thisJoinPointStaticPart.getSignature();

         log.info(sig.toString() + "Time took: " + (end-start));

         return obj;
       }
     }


Hope this helps. Obviously this sets out an example you can use.
Look for Around Advice within the AspectJ docs.

- Dan

On 9/6/06, Brian Lee <brian_a_lee at hotmail.com> wrote:
> Hello all,
>
> I am looking at using AspectJ for my Java 1.4 project. I was going through
> the online docs and googled around, but couldn't find any examples of an
> aspect class that is able to capture performance times for method calls.
>
> I would like to add a performance aspect to a set of methods so that I
> output a log statement with the total time spent before and after the method
> is executed.
>
> I saw a lot of examples of before and after join points, but not one that
> could introduce a local long variable to store the currentTimeMillis before
> and after the method executes.
>
> Has anyone else used aspects for this kind of performance logging? Any help
> is appreciated.
>
> Thanks,
> BAL
>
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>



More information about the ajug-members mailing list