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?
Class − Class level interceptor is invoked for every method of the bean. Class level interceptor can be applied both by annotation of via xml(ejb-jar. xml). Method− Method level interceptor is invoked for a particular method of the bean. Method level interceptor can be applied both by annotation of via xml(ejb-jar.
An interceptor is a method that is automatically called when the business methods of an Enterprise JavaBeans (EJB) are invoked or lifecycle events of an EJB occur. There are three kinds of interceptor methods: business method interceptors, timeout method interceptors (which are new in EJB3.
A Default interceptor is an interceptor that is invoked whenever a business method is invoked on any bean within the deployment.
Annotation Type AroundInvokeDefines an interceptor method that interposes on business methods. May be applied to any non-final, non-static method with a single parameter of type InvocationContext and return type Object of the target class (or superclass) or of any interceptor class.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With