Miles to go …

May 10, 2010

TOTD #134: Interceptors 1.1 in Java EE 6 – What and How ?

Filed under: General — arungupta @ 11:04 pm

TOTD #129 explained Managed Beans 1.0, this Tip Of The Day (TOTD) attempts to explain the basics of Interceptors 1.1 – a "new" specification introduced in the Java EE 6.

The specification is not entirely new as the concept is borrowed from the EJB 3.0 specification and abstracted at a higher level so that it can be more generically applied to a broader set of specifications in the platform. Interceptors do what they say – they intercept on invocations and lifecycle events on an associated target class. Basically, interceptor is a class whose methods are invoked when business methods on the target class are invoked and/or lifecycle events such as methods that create/destroy the bean occur. Interceptors are typically used to implement cross-cutting concerns like logging, auditing, and profiling.

Contexts and Dependency Injection (CDI, aka JSR 299) uses the concept defined by Interceptors 1.1 and adds the notion of interceptors binding.

Let see some code to make it all clear.

Each interceptor require at least one interceptor binding to associate with the target class. An interceptor binding is defined as:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MyInterceptorBinding {
}

And then the interceptor is defined as:

import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Interceptor
@MyInterceptorBinding
public class MyInterceptor {
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
System.out.println("before interception");
Object result = context.proceed();
System.out.println("after interception");
return result;
}
}

The "@AroundInvoke" annotation (can be only one per interceptor) on a method in the interceptor class ensures that this method is invoked around the business method interception. This will be more clear after the program flow is explained later. Multiple interceptors can be chained and the flow/outcome may be diverted in any of them using "InvocationContext".

The associated target bean looks like:

. . .
import javax.interceptor.Interceptors;
@ManagedBean(value="mybean")
@Interceptors(MyInterceptor.class)
public class MyManagedBean {
@PostConstruct
public void setupResources() {
// setup your resources
System.out.println("Setting up resources ...");
}
. . .
}

The only missing part on the server-side is enabling bean discovery by adding an empty "beans.xml" as:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

Notice, there is no need to explicitly mention <interceptors> in "beans.xml". Multiple interceptors on a bean can be easily defined as:

@ManagedBean(value="mybean")
@Interceptors({MyInterceptor.class, MyInterceptor2.class})
public class MyManagedBean {

Lets see how this bean and interceptor can be used. Create a Servlet as:

@WebServlet(name="TestServlet", urlPatterns={"/TestServlet"})
public class TestServlet extends HttpServlet {

Inject a bean as …

@Inject
MyManagedBean bean;

and then invoke the bean’s method in "doGet" or "doPost" methods of the servlet as:

String result = bean.sayHello("Duke");

Notice that @Inject is used to inject the managed bean and bean discovery is enabled by adding an empty "beans.xml". This ensures that CDI inject works as expected and all the interceptors are invoked as well. Another alternative is to inject the bean using @Resource but "beans.xml" need to be removed in order for the interceptors to be invoked. So inject your bean using @Inject + "beans.xml" or @Resource. The recommended approach is to use @Inject + "beans.xml" as CDI might be used in other parts of your applications as well.

If 2 interceptors, each with a separate interceptor binding, managed bean class, and the servlet is included in a web application then the directory structure will look like:

./META-INF
./META-INF/MANIFEST.MF
./WEB-INF
./WEB-INF/beans.xml
./WEB-INF/classes
./WEB-INF/classes/server
./WEB-INF/classes/server/MyInterceptor.class
./WEB-INF/classes/server/MyInterceptor2.class
./WEB-INF/classes/server/MyInterceptorBinding.class
./WEB-INF/classes/server/MyInterceptorBinding2.class
./WEB-INF/classes/server/MyManagedBean.class
./WEB-INF/classes/server/TestServlet.class

If there are "System.out.println"s inserted at relevant positions in the interceptor and the Servlet code, then the code flow looks like:

Before Intercept
Before Intercept2
sayHello
After Intercept2
After Intercept
processRequest

"Before XXX" messages are printed by a method from the interceptors, in the order of chaining, before "context.proceed" is invoked in all the interceptors. "sayHello" method is invoked from the "doGet" or "doPost" method in the Servlet. "After XXX" messages are printed from the interceptors after "context.proceed" method is invoked, this time in the reverse order of chain. And finally "processRequest" method is invoked again from the "doGet" or "doPost" method in the Servlet.

The complete source code for the sample explained above can be downloaded here.

Technorati: javaee glassfish v3 managedbeans interceptors cdi ejb servlet

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #151: Transactional Interceptors using CDI – Extensible Java EE 6
  2. TOTD #129: Managed Beans 1.0 in Java EE 6 – What and How ?
  3. TOTD #144: CDI @Produces for container-managed @Resource
  4. TOTD #145: CDI Events – a light-weight producer/consumer in Java EE 6
  5. TOTD #161: Java EE 6 CDI Qualifiers explained – @Default, @Any, @New, @Named

7 Comments »

  1. Hi Arun,

    Good article. However, is the InterceptorBinding annotation implementation required? Could I just annotate my interceptor class with the Interceptor annotation? If so what is the value of the InterceptorBinding? Perhaps to further define the possible Target(s) for your interceptor?

    Thanks,
    Darryl

    Comment by Darryl Stoflet — May 12, 2010 @ 12:54 pm

  2. Hi Arun,

    Good article. However, is the InterceptorBinding annotation implementation required? Could I just annotate my interceptor class with the Interceptor annotation? If so what is the value of the InterceptorBinding? Perhaps to further define the possible Target(s) for your interceptor?

    Thanks,
    Darryl

    Comment by Darryl Stoflet — May 12, 2010 @ 3:11 pm

  3. Darryl,

    That’s how I understood as well.

    -Arun

    Comment by Arun Gupta — May 16, 2010 @ 2:12 pm

  4. Hi,

    I think it’s better to define the interceptor in beans.xml and just annotate the ManagedBean with @MyInterceptorBinding instead of @Interceptors(MyInterceptor.class)

    (see http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/interceptor/InterceptorBinding.htm)

    Comment by Matthias — July 4, 2010 @ 8:05 am

  5. I think it is better if the bean itself does not know it it intercepted. If it knows what is going to be intercepted, I would say it is cleaner to implement the Template Method pattern.

    Instead I would have a default interceptor (possible in JEE5, but with a different annotation and with EJB only from what I can tell) that I configure. Or a non-default where I specify which classes by name to intercept.

    Comment by Archimedes Trajano — September 15, 2010 @ 9:25 pm

  6. Hi.
    Good article. I have a few questions about configuring in "beans.xml".

    In JavaEE6 can we define the order of Interceptor invocation in "beans.xml"? And can configuration in "beans.xml" override definition in a bean class?
    I guess configuration the latter can override the former like JavaEE5.

    Comment by hideaki — October 3, 2010 @ 1:55 pm

  7. hideaki,

    The deployment descriptors always override the annotations.

    Comment by Arun Gupta — October 5, 2010 @ 7:09 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress