EJB 3 Interceptors tutorial

  • This is a short tutorial for EJB 3 interceptors, and its purpose is to explain the concept of an interceptor and what is it good for.
  • The content of this tutorial is a shrinked version of a tutorial posted here: http://www.mastertheboss.com/en/jboss-server/177-ejb-interceptors-in-depth.html
  • which also contains code example.
  •  
  • EJB 3.0 interceptors allow you to add functionality to business methods of your session bean without modifying the methods' code.
  • An interceptor is a method that's executed before any of the bean's business methods are invoked.
  •   
  • How does it work ?
  • When a method of the session bean is invoked, the Interceptor is also invoked.
  • The interceptor has full access to the invoked method and its parameter.
  •   
  • Interceptors are particularly useful when you need to:
  1. Validate parameters before they're passed to a business method
  2. Perform security checks at the time the business method is called.
  3. Perform other useful for actions such as logging and profiling without changing the code of your EJB.
  •   
  • On which kind of components can you apply Interceptors ?
  • Interceptors can be used on stateless session beans, stateful session beans, and message driven beans.
  •  
  • How many kinds of Interceptors can you apply ?
  • You can apply Interceptors at three different levels:
  •  
  • 1) Default interceptors
  • These interceptors needs to be declared in your ejb-jar.xml and are valid across all your EJB deployed :
  • 1.    <assembly-descriptor> 
  • 2.        <interceptor-binding> 
  • 3.          <ejb-name>*</ejb-name> 
  • 4.          <interceptor-class>sample.interceptor.MyDefaultInterceptor</interceptor-class> 
  • 5.       </interceptor-binding> 
  • 6.       ... 
  • 7.    </assembly-descriptor> 
  •  
  • 2) Class level interceptors
  • This is the kind of interceptor we've seen in our example: it is valid across all methods of an EJB.
  •    @Stateless
  •    @Interceptors(value=com.sample.SampleInterceptor.class)
  •    public class StatelessBean
  •    {
  •       ...
  •    }
  •  
  • 3) Method level interceptors
  • The method level interceptor only intercepts the single method call :
  •  
  •    @Interceptors(value=com.sample.MethodInterceptor.class)
  •    public void doSomething()
  •    {
  •       ...
  •    }
  •  
  • Order of Interception
  • First, the default interceptor is invoked, then Bean level interceptors and, at last, method level interceptors.
  • If you declare a list of interceptors in your xml/annotations, then the interceptors are invoked in the order in which they were declared in the annotation.

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com