Using EJB interceptors after a method call
Asked Answered
S

1

10

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?

Stringed answered 21/6, 2011 at 14:7 Comment(0)
B
18

@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.

Brena answered 21/6, 2011 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.