JBoss.org Community Documentation
The Interceptor
interface enables one to build a chain of method interceptors through which each EJB method invocation must pass. The Interceptor
interface is given below.
import org.jboss.invocation.Invocation; public interface Interceptor extends ContainerPlugin { public void setNext(Interceptor interceptor); public Interceptor getNext(); public Object invokeHome(Invocation mi) throws Exception; public Object invoke(Invocation mi) throws Exception; }
Example 11.5. The org.jboss.ejb.Interceptor interface
All interceptors defined in the container configuration are created and added to the container interceptor chain by the EJBDeployer
. The last interceptor is not added by the deployer but rather by the container itself because this is the interceptor that interacts with the EJB bean implementation.
The order of the interceptor in the chain is important. The idea behind ordering is that interceptors that are not tied to a particular EnterpriseContext
instance are positioned before interceptors that interact with caches and pools.
Implementers of the Interceptor
interface form a linked-list like structure through which the Invocation
object is passed. The first interceptor in the chain is invoked when an invoker passes a Invocation
to the container via the JMX bus. The last interceptor invokes the business method on the bean. There are usually on the order of five interceptors in a chain depending on the bean type and container configuration. Interceptor
semantic complexity ranges from simple to complex. An example of a simple interceptor would be LoggingInterceptor
, while a complex example is EntitySynchronizationInterceptor
.
One of the main advantages of an interceptor pattern is flexibility in the arrangement of interceptors. Another advantage is the clear functional distinction between different interceptors. For example, logic for transaction and security is cleanly separated between the TXInterceptor
and SecurityInterceptor
respectively.
If any of the interceptors fail, the call is terminated at that point. This is a fail-quickly type of semantic. For example, if a secured EJB is accessed without proper permissions, the call will fail as the SecurityInterceptor
before any transactions are started or instances caches are updated.