Re: [aspectj-users] context information in aspect fields
Hi, It may or may not be useful to you, but the Perf4j project[1] uses AspectJ to instrument code with stopwatches, providing generic around-method timing via an @Profiled annotation. There's also an unreleased enhancement in github which allows methods to be timed without annotating them (by completing an abstract pointcut), so no source-changes requires. Best Brett [1] http://perf4j.codehaus.org/ On 6 November 2012 21:29, Reik Schatz wrote: > Hi, > > I am working on a timer example using AspectJ similar to this logging > example: > http://adamgent.com/post/5479576926/the-python-decorator-pattern-for-java-using-aspectj > > However the Timer (not Logger) I am using takes among others a String > argument which describes the context of the place where you are timing (i.e. > if you are timing a save method in a DAO class, the argument would be > "save"). Now that the Timer is defined in the Aspect, is it possible to > retrieve some context information outside of the pointcut or advice > definition? Ideally I want to do something like: > > public aspect TimedAspect { > > private final Timer TIMER = Metrics.newTimer(getClass(), "TARGET METHOD > NAME HERE"); > > pointcut timedExecution() : execution(@Timed * * (..)); // execution of > a annotated method > > Object around() : timedExecution() { > final TimerContext context = TIMER.time(); > try { > return proceed(); > } finally { > context.stop(); > } > } > } > > For me it looks like the only way is to create the Timer object inside the > advice and use the context information available. > > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users > ___ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users
Re: [aspectj-users] context information in aspect fields
Yes. And for non-static members you ought not to forget the use of volatile for the member... ;) [ Romain Muller | Software Development Engineer | +33 (0)6 48 25 66 70 | romain.mul...@esial.net ] Le 6 nov. 2012 à 11:47, Reik Schatz a écrit : > Thanks, both approaches are good i think. However if you want to code this > really clean, I guess even with a ConcurrentHashMap in the Map approach you'd > still have to do some locking for the situation where 2 threads are calling > the annotated method and Map is still empty. Same goes for the > perthis approach if the non-static member is created inside the advice, > correct? > > > On Tue, Nov 6, 2012 at 11:31 AM, Romain Muller > wrote: > You can't do this with a "final" TIMER field. You may want to have a "private > final Map timers" in which you'd associate Timers to > MethodNames. > > The MethodName you can get from thisJoinPoint(StaticPart) - you may even be > able to use thisJoinPointStaticPart.toString() for that purpose. > > [ Romain Muller | Software Development Engineer | +33 (0)6 48 25 66 70 | > romain.mul...@esial.net ] > > Le 6 nov. 2012 à 11:29, Reik Schatz a écrit : > >> Hi, >> >> I am working on a timer example using AspectJ similar to this logging >> example: >> http://adamgent.com/post/5479576926/the-python-decorator-pattern-for-java-using-aspectj >> >> However the Timer (not Logger) I am using takes among others a String >> argument which describes the context of the place where you are timing (i.e. >> if you are timing a save method in a DAO class, the argument would be >> "save"). Now that the Timer is defined in the Aspect, is it possible to >> retrieve some context information outside of the pointcut or advice >> definition? Ideally I want to do something like: >> >> public aspect TimedAspect { >> >> private final Timer TIMER = Metrics.newTimer(getClass(), "TARGET METHOD >> NAME HERE"); >> >> pointcut timedExecution() : execution(@Timed * * (..)); // execution of >> a annotated method >> >> Object around() : timedExecution() { >> final TimerContext context = TIMER.time(); >> try { >> return proceed(); >> } finally { >> context.stop(); >> } >> } >> } >> >> For me it looks like the only way is to create the Timer object inside the >> advice and use the context information available. >> ___ >> aspectj-users mailing list >> aspectj-users@eclipse.org >> https://dev.eclipse.org/mailman/listinfo/aspectj-users > > > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users > > > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users smime.p7s Description: S/MIME cryptographic signature ___ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users
Re: [aspectj-users] context information in aspect fields
Thanks, both approaches are good i think. However if you want to code this really clean, I guess even with a ConcurrentHashMap in the Map approach you'd still have to do some locking for the situation where 2 threads are calling the annotated method and Map is still empty. Same goes for the perthis approach if the non-static member is created inside the advice, correct? On Tue, Nov 6, 2012 at 11:31 AM, Romain Muller wrote: > You can't do this with a "final" TIMER field. You may want to have a > "private final Map timers" in which you'd associate Timers > to MethodNames. > > The MethodName you can get from thisJoinPoint(StaticPart) - you may even > be able to use thisJoinPointStaticPart.toString() for that purpose. > * > [* *Romain Muller* *| *Software Development Engineer *|* +33 *(0)6* 48 *25 > * 66 *70* *|* romain.mul...@esial.net *]* > > Le 6 nov. 2012 à 11:29, Reik Schatz a écrit : > > Hi, > > I am working on a timer example using AspectJ similar to this logging > example: > http://adamgent.com/post/5479576926/the-python-decorator-pattern-for-java-using-aspectj > > However the Timer (not Logger) I am using takes among others a String > argument which describes the context of the place where you are timing > (i.e. if you are timing a save method in a DAO class, the argument would be > "save"). Now that the Timer is defined in the Aspect, is it possible to > retrieve some context information outside of the pointcut or advice > definition? Ideally I want to do something like: > > public aspect TimedAspect { > > private final Timer TIMER = Metrics.newTimer(getClass(), "TARGET > METHOD NAME HERE"); > > pointcut timedExecution() : execution(@Timed * * (..)); // execution > of a annotated method > > Object around() : timedExecution() { > final TimerContext context = TIMER.time(); > try { > return proceed(); > } finally { > context.stop(); > } > } > } > > For me it looks like the only way is to create the Timer object inside the > advice and use the context information available. > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users > > > > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users > > ___ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users
Re: [aspectj-users] context information in aspect fields
As an alternative if you do not lile the Map idea, you might want to use a non-singleton instantiation model like "perthis" or "pertarget" in combination with a normal (non-static) member variable for the Timer. Alexander Kriegisch http://scrum-master.de Am 06.11.2012 um 11:31 schrieb Romain Muller : > You can't do this with a "final" TIMER field. You may want to have a "private > final Map timers" in which you'd associate Timers to > MethodNames. > > The MethodName you can get from thisJoinPoint(StaticPart) - you may even be > able to use thisJoinPointStaticPart.toString() for that purpose. > > [ Romain Muller | Software Development Engineer | +33 (0)6 48 25 66 70 | > romain.mul...@esial.net ] > > Le 6 nov. 2012 à 11:29, Reik Schatz a écrit : > >> Hi, >> >> I am working on a timer example using AspectJ similar to this logging >> example: >> http://adamgent.com/post/5479576926/the-python-decorator-pattern-for-java-using-aspectj >> >> However the Timer (not Logger) I am using takes among others a String >> argument which describes the context of the place where you are timing (i.e. >> if you are timing a save method in a DAO class, the argument would be >> "save"). Now that the Timer is defined in the Aspect, is it possible to >> retrieve some context information outside of the pointcut or advice >> definition? Ideally I want to do something like: >> >> public aspect TimedAspect { >> >> private final Timer TIMER = Metrics.newTimer(getClass(), "TARGET METHOD >> NAME HERE"); >> >> pointcut timedExecution() : execution(@Timed * * (..)); // execution of >> a annotated method >> >> Object around() : timedExecution() { >> final TimerContext context = TIMER.time(); >> try { >> return proceed(); >> } finally { >> context.stop(); >> } >> } >> } >> >> For me it looks like the only way is to create the Timer object inside the >> advice and use the context information available. >> ___ >> aspectj-users mailing list >> aspectj-users@eclipse.org >> https://dev.eclipse.org/mailman/listinfo/aspectj-users > > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users ___ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users
Re: [aspectj-users] context information in aspect fields
You can't do this with a "final" TIMER field. You may want to have a "private final Map timers" in which you'd associate Timers to MethodNames. The MethodName you can get from thisJoinPoint(StaticPart) - you may even be able to use thisJoinPointStaticPart.toString() for that purpose. [ Romain Muller | Software Development Engineer | +33 (0)6 48 25 66 70 | romain.mul...@esial.net ] Le 6 nov. 2012 à 11:29, Reik Schatz a écrit : > Hi, > > I am working on a timer example using AspectJ similar to this logging > example: > http://adamgent.com/post/5479576926/the-python-decorator-pattern-for-java-using-aspectj > > However the Timer (not Logger) I am using takes among others a String > argument which describes the context of the place where you are timing (i.e. > if you are timing a save method in a DAO class, the argument would be > "save"). Now that the Timer is defined in the Aspect, is it possible to > retrieve some context information outside of the pointcut or advice > definition? Ideally I want to do something like: > > public aspect TimedAspect { > > private final Timer TIMER = Metrics.newTimer(getClass(), "TARGET METHOD > NAME HERE"); > > pointcut timedExecution() : execution(@Timed * * (..)); // execution of a > annotated method > > Object around() : timedExecution() { > final TimerContext context = TIMER.time(); > try { > return proceed(); > } finally { > context.stop(); > } > } > } > > For me it looks like the only way is to create the Timer object inside the > advice and use the context information available. > ___ > aspectj-users mailing list > aspectj-users@eclipse.org > https://dev.eclipse.org/mailman/listinfo/aspectj-users smime.p7s Description: S/MIME cryptographic signature ___ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users
[aspectj-users] context information in aspect fields
Hi, I am working on a timer example using AspectJ similar to this logging example: http://adamgent.com/post/5479576926/the-python-decorator-pattern-for-java-using-aspectj However the Timer (not Logger) I am using takes among others a String argument which describes the context of the place where you are timing (i.e. if you are timing a save method in a DAO class, the argument would be "save"). Now that the Timer is defined in the Aspect, is it possible to retrieve some context information outside of the pointcut or advice definition? Ideally I want to do something like: public aspect TimedAspect { private final Timer TIMER = Metrics.newTimer(getClass(), "TARGET METHOD NAME HERE"); pointcut timedExecution() : execution(@Timed * * (..)); // execution of a annotated method Object around() : timedExecution() { final TimerContext context = TIMER.time(); try { return proceed(); } finally { context.stop(); } } } For me it looks like the only way is to create the Timer object inside the advice and use the context information available. ___ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users