Using deployment descriptors for callback interceptors

Lifecycle callback methods are used to perform special processing during the creation, destruction and other events of an EJB. The callback events available are EJB-specific. In this recipe we will examine the use of deployment descriptors to specify a callback event.

Getting ready

The process for creating a deployment descriptor for interceptors includes:

  • Creating an ejb-jar.xml file for the EJB module
  • Using the<interceptors> element to define interceptors
  • Adding a lifecycle element to bind the interceptor to an EJB
  • Deploying the application

To illustrate the use of a deployment descriptor for a lifecycle method, we will modify the RegistrationApplication along with the SimpleInterceptor class developed in the first two recipes of Chapter 8, Interceptors. The SimpleInterceptor class has a method called constructed, as shown below, which we want to be a post construct method. Post construct methods will be executed after its class has been created and after any dependency injection has been performed. We could use the @PostConstruct annotation to mark the method as a post construct method. Instead we will use a deployment descriptor.

public class SimpleInterceptor {
private void constructed(InvocationContext invocationContext) {
System.out.println("SimpleInterceptor constructed: ");
}
...
}

How to do it...

Create an ejb-jar.xml file. The root element is<ejb-jar> as shown here:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.1"
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/ejb-jar_3_1.xsd">
...
</ejb-jar>

For a post construction life cycle callback, the<post-construct> element is used. Add an<interceptors> element within the<ejb-jar> element. Within this element you can add one or more<interceptor> elements to declare interceptors and their callback methods.

Add an<interceptor> element and within this element add the following two elements:

  • <lifecycle-callback-class> Used to specify the class
  • <lifecycle-callback-method> Used to specify the method

The following code illustrates the deployment code to affect the use of this lifecycle method.

<interceptors>
<interceptor>
<interceptor-class>packt.DefaultInterceptor</interceptor-class>
</interceptor>
<interceptor>
<interceptor-class>packt.SimpleInterceptor</interceptor-class>
<post-construct>
<lifecycle-callback-class>packt.SimpleInterceptor </lifecycle-callback-class>
<lifecycle-callback-method>constructed </lifecycle-callback-method>
</post-construct>
</interceptor>
</interceptors>

How it works...

In the ejb-jar.xml file, the SimpleInterceptor was declared using the<interceptor> element. As part of this element, the<post-construct> element was used to associate the SimpleInterceptor with the constructed method. At deployment the server will effect these specifications.

There's more...

There are other lifecycle methods which can be configured using a deployment descriptor. A list of common events is found in the following table:

Annotation

Element

@AroundInvoke

<around-invoke>

@PostActivate

<post-activate>

@PostConstruct

<post-construct>

@PreDestroy

<pre-destroy>

@PrePassivate

<pre-passivate>

When multiple lifecycle events are handled by the same class, the lifecycle elements follow each other within the same<interceptor> element for the same class. Here post-construct and pre-destroy methods are declared.

<interceptors>
<interceptor>
<interceptor-class>interceptorClass</interceptor-class>
<post-construct>
<lifecycle-callback-class>package.interceptorClassName </lifecycle-callback-class>
<lifecycle-callback-method>someMethodName </lifecycle-callback-method>
</post-construct>
<pre-destroy>
<lifecycle-callback-class>package.interceptorClassName </lifecycle-callback-class>
<lifecycle-callback-method>someMethodName </lifecycle-callback-method>
</pre-destroy>
</interceptor>
</interceptors>

See also

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

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

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