Using deployment descriptor for default interceptors

Default interceptors are intended to be executed for every session and message-driven EJB in the EJB module. However, default interceptors can only be declared within the ejb-jar.xml deployment descriptor. This recipe will illustrate this technique.

Getting ready

The process for creating a deployment descriptor for default interceptors includes:

  1. Creating an ejb-jar.xml file for the EJB module
  2. Using the<interceptors> element to define your interceptors
  3. Adding an<interceptor-binding> element to bind the interceptor to an EJB
  4. Deploying the application

The basic structure of the XML elements is as follows:

<interceptors>
<interceptor>
<interceptor-class>interceptorClass</interceptor-class>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>interceptorClass</interceptor-class>
</interceptor-binding>
</assembly-descriptor>

The use of the asterisk in this position means the interceptor is to be used with all of the EJBs in the EJB module.

How to do it...

We will use the Chapter 8, Defining and using interceptors recipe, to illustrate deployment descriptors. In the ejb-jar.xml file, add an<interceptors> element to declare your default interceptor. Within the element add one<interceptor> element for each interceptor. In this case there is only one. The<interceptor-class> element is needed to specify the name of the interceptor class.

<interceptors>
<interceptor>
<interceptor-class>packt.DefaultInterceptor</interceptor-class>
</interceptor>
</interceptors>

Next, add an<interceptor-binding> element inside of an<assembly-descriptor> element to bind the interceptor to all of the EJBs in the module. Next, add an<ejb-name> element within the<interceptor-binding> element and use an asterisk for its value to specify all EJBs in the module. Add the<interceptor-class> element next to specify the name of the interceptor.

<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>packt.DefaultInterceptor</interceptor-class>
</interceptor-binding>
</assembly-descriptor>

We need to create the default interceptor before we can run our application. Add a new class to the packt package called DefaultInterceptor. It should be very similar to the SimpleInterceptor except for the use of the method name defaultMethod. Modify the println methods to indicate we are executing from the default interceptor.

public class DefaultInterceptor {
@AroundInvoke
public Object defaultMethod(InvocationContext context) throws Exception{
System.out.println("Default Interceptor: Invoking method: " + context.getMethod().getName());
Object result = context.proceed();
System.out.println("Default Interceptor: Returned from method: " + context.getMethod().getName());
return result;
}

Execute the application. The output should be similar to the following:

INFO: Default Interceptor: Invoking method: register

INFO SimpleInterceptor entered: register

INFO: register

INFO: Default Interceptor: Invoking method: create

INFO: Default Interceptor: Returned from method: create

INFO: SimpleInterceptor exited: register

INFO: Default Interceptor: Returned from method: register

How it works...

The ejb-jar.xml file contained the XML elements used to associate the DefaultInterceptor with all of the EJBs of an application. The DefaultInterceptor was added to the packt package and declared with the<interceptors> element. Within the<assembly-descriptor> element it was bound to the application's EJBs. Upon execution of the application, the interceptors were invoked as expected.

See also

The Using deployment descriptors for interceptors recipe covers the general aspects of using interceptors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.99.152