Using deployment descriptors for timer interceptors

The @AroundTimeout annotation is used to specify an interceptor method for a timer. The interceptor method will be executed before and after the timer method executes. The creation and use of timers is detailed in Chapter 9, Timer Services. In this recipe, we will learn how to specify a timer interceptor using a deployment descriptor.

Getting ready

The process for creating a deployment descriptor for a timer interceptor includes:

  1. Creating an ejb-jar.xml file for the EJB module
  2. Adding an<around-timeout> element to the file
  3. Deploying the application

    We will reuse the code developed in the Chapter 9, Using interceptors with timers recipe. In the recipe, the @Schedule annotation was used with the SystemReportManager's displayMemoryReport method. This configuration displays the JVM memory utilization every 10 seconds. The @Schedule annotation used and the displayMemoryReport method minus its body are shown here:

    @Schedule(second = "0,10,20,30,40,50", minute="*", hour = "*")
    public void displayMemoryReport(Timer timer) {...}
    

    The interceptorTimeout method follows:

    public Object interceptorTimeout(InvocationContext invocationContext) throws Exception {
    System.out.println("interceptorTimeout executing");
    Timer timer = (Timer) invocationContext.getTimer();
    System.out.println("Timer: " + timer.getSchedule());
    Object object = invocationContext.proceed();
    System.out.println("interceptorTimeout returning");
    return object;
    }
    

How to do it...

Create an ejb-jar.xml if one does not already exist. Add a declaration for the SystemReportManager session EJB using the<enterprise-beans> and<session> elements. Within the<session> element, use the<ejb-name> to specify the SystemReportManager EJB followed by an<around-timeout> element. Within the<around-timeout> element, add a<method-name> element using interceptorTimeout as its value. These elements are listed 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">
<enterprise-beans>
<session>
<ejb-name>SystemReportManager</ejb-name>
<around-timeout>
<method-name>interceptorTimeout</method-name>
</around-timeout>
</session>
</enterprise-beans>
</ejb-jar>

Redeploy the application. When the deployment is complete, the timer along with its interceptor should execute every 10 seconds.

How it works...

The ejb-jar.xml file was used for associating the interceptorTimeout method with the SystemReportManager's displayMemoryReport method. In the<session> element, the SystemReportManager was declared using the<ejb-name> element. Following this was the<around-timeout> element where the interceptor was named. At deployment, the server will associate this interceptor with the SystemReportManager methods.

See also

The Using deployment descriptors for interceptor 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
3.12.166.255