Java Reference
In-Depth Information
The first thing we do in our interceptor method is to obtain a reference to the object
and method being intercepted. We then output a message to the log indicating
the class and method being invoked. This code is executed just before we let the
intercepted method execute, which we do by invoking invocationContext.
proceed() . We store the return value of this method in a variable, then add some
additional logic to be executed just after the method finishes. In our example, we
simply send an additional line of text to the application server log. Finally our
method returns the return value of invocationContext.proceed() .
Decorating the EJB with the @Interceptors
Annotations
In order for an EJB's method to be intercepted, it must be decorated with the @
Interceptors annotation. This annotation has a single class array attribute. This
attribute contains all the interceptors to be executed before and/or after the
method call.
The @Interceptors annotation can be used at the method level, in which case it
applies only to the method it decorates, or at the class level, in which it applies to
every method in the bean.
The following example is a new version of our EchoBean session bean, slightly
modified to have its echo() method intercepted by the LoggingInterceptor we
wrote in the previous section.
package com.ensode.sessionbeanintro.ejb;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.interceptor.Interceptors;
@Stateless
public class EchoBean implements EchoRemote {
@Interceptors({LoggingInterceptor.class})
@TransactionAttribute(
TransactionAttributeType.REQUIRES_NEW)
public String echo(String saying) {
return "echoing: " + saying;
}
}
 
Search WWH ::




Custom Search