Using EJB Interceptors — Part 2
In the first part of this series of posts we looked at the around-invoke interceptors supported by EJB 3.1. In this post we will mention method interceptors.
EJB 3.1 provides support for defining method level interceptors through the use of the @Interceptors annotation. Using this annotation it is possible to share common interceptors across different business methods.
The @Interceptors annotation declares an ordered list of interceptors for a target class or method of a target class. To register an interceptor for a method:
@Interceptors(LoggingInterceptor.class)
public Float calculate(Float x, Float y) throws IllegalArgumentException {
return 2 * x + y;
}
It is also possible to register multiple interceptors for a business method. To register two interceptors to a business method:
@Interceptors({LoggingInterceptor.class, SecurityInterceptor.class})
public void someBusinessMethod(SomeParameter param) { ... }
In order to intercept all business method calls of a bean, the @Interceptors annotation can be used at the class-level:
@Stateless
@Interceptors(LoggingInterceptor.class)
public class SimpleSessionBean implements SimpleSessionBeanRemote {
Finally to exclude a method from the use of class-level interceptors we use the @ExcludeClassInterceptors annotation:
@Stateless
@Interceptors(LoggingInterceptor.class)
public class SimpleSessionBean implements SimpleSessionBeanRemote {
@ExcludeClassInterceptors
public void placeOrder(Order o){
…
}
//other methods
}
The output of the interceptor is shown below:
[#|intercepting method call|#] [#|Processing call calculate@com.sirius.test.SimpleSessionBean@1779dfb|#] [#|=>1.0|#] [#|=>2.0|#] [#|Done with calculate@com.sirius.test.SimpleSessionBean@1779dfb|#]
You can download the code for the application here. The application is tested on Glassfish v3.0.
About this entry
You’re currently reading “ Using EJB Interceptors — Part 2 ,” an entry on Sirius ICT
- Published:
- 3.25.10 / 1pm
- Category:
- Blog, Development Issues, Thougths and Reflections
No comments
Jump to comment form | comments rss [?] | trackback uri [?]