Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 758
Gonzalo Garcia Lasurtegui Avatar asked Jun 21 '11 14:06

Gonzalo Garcia Lasurtegui


People also ask

What is method level interceptor in ejb?

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.

What are interceptors for business methods?

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.

What is default interceptor in ejb?

A Default interceptor is an interceptor that is invoked whenever a business method is invoked on any bean within the deployment.

What is AroundInvoke?

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.


1 Answers

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

like image 191
Brett Kail Avatar answered Jan 03 '23 17:01

Brett Kail