Using EJB interceptors after a method call

I know one can use interceptors before a method call by using the @AroundInvoke annotation.

What I would like to do is execute certain code after the method call, so that I can for example create a log entry before and after a method execution.

Is this possible with EJB3, or do I need to use AOP?

Upvotes: 10

Views: 4116

Answers (1)

Brett Kail
Brett Kail

Reputation: 33936

@AroundInvoke interceptor is passed InvocationContext, and proceed() must be called to advance the method. Thus:

@AroundInvoke
public Object log(InvocationContext ic) throws Exception {
  logEntry();
  try {
    return ic.proceed();
  } finally {
    logExit();
  }
}

Depending on your needs, you could also log the return value or exceptions, filter the methods being logged, etc.

Upvotes: 18

Related Questions