[java] How do I time a method's execution in Java?

A strategy that works to me in java ee was:

  1. Create a class with a method annotated with @AroundInvoke;

    @Singleton
    public class TimedInterceptor implements Serializable {
    
        @AroundInvoke
        public Object logMethod(InvocationContext ic) throws Exception {
            Date start = new Date();
            Object result = ic.proceed();
            Date end = new Date();
            System.out.println("time: " + (end.getTime - start.getTime()));
            return result;
        }
    }
    
  2. Annotate the method that you want to monitoring:

    @Interceptors(TimedInterceptor.class)
    public void onMessage(final Message message) { ... 
    

I hope this can help.