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.
The process for creating a deployment descriptor for interceptors includes:
ejb-jar.xml
file for the EJB module<interceptors>
element to define interceptorsTo 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: "); } ... }
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 methodThe 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>
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 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>
3.128.226.121