0

I would like to log the execution time for some methods in my controllers.
Right now I am just trying to figure out so I don't log but print, and it's for all methods.
I have a Profiler class:

@Aspect
public class Profiler {

    @Pointcut("execution(public * *(..))")
    public void methods() {}

    @Around("methods()")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        System.out.println(joinPoint.getSignature() + " " + (System.currentTimeMillis() - start));
        return result;
    }
}

My pom.xml contains aspectjrt version 1.8.5
I don't use Spring.

I tried dozens of combinations of annotations like @Provider, @ApplicationScoped, @Initialized(ApplicationScoped.class), @Named.
I tried to use javax.ws.rs.core.Application, jboss-aop.xml, etc.
Nothing works (but no errors) :-(

Thanks for your help.

Maxime Laval
  • 4,068
  • 8
  • 40
  • 60

2 Answers2

0

Java EE 7 does not support AOP in its full generality, but using interceptors, you can usually achieve the same effect.

Have a look at the Interceptors and CDI specs for more details.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • So I lose the benefit of not having to modify any classes in my projects right? – Maxime Laval Mar 13 '15 at 18:40
  • You can write a CDI extension to add interceptors to your classes programmatically. - However, if all you want to do is one-shot logging with this AOP pointcut, then I suppose the question boils down to weaving your classes in a way compatible with WildFly. What did you do to weave your classes, or did you assume WildFly would do this automatically? – Harald Wellmann Mar 13 '15 at 22:26
  • I didn't assume anything ;-) I am new to WildFly, I used to work with Spring + Tomcat/Jetty (which I prefer much more). I used to use Spring AOP + AspectJ so 1 class for the @Aspect (like the one in my question) and I was done, no need to modify the existing code. – Maxime Laval Mar 14 '15 at 00:41
  • @hwellmann: With all due respect, your statement in the answer does not make sense. JEE does not need to support AOP because AspectJ does. The issue seen by the OP is probably related to a classloading problem. He needs to find a way to use the LTW Java agent *aspectjweaver.jar* correctly and that one in turn needs to find *aop.xml* and the aspects in the right place. This is the usual problem with AspectJ in application servers. Not being a WildFly expert, I cannot provide a concrete solution, but this hint only. – kriegaex Mar 14 '15 at 13:43
0

The last version of AOP was released in 2010, a year before Java 7 was released.

Wildfly requires Java 7 or above, so the answer to your question unfortunately seems to be you can't.

AOP works on manipulating Java bytecode, and Java 6 and Java 7 bytecode will be different. This will make the AOP libraries incompatible with versions of Java later than 6

kirsty
  • 267
  • 1
  • 2
  • 14