1. Modify the ejb-jar.xml
First we need to add some configuration to the ejb-jar.xml, as shown in Figure 1.
Figure 1. Add the interceptor to the ejb-jar.xml
So first we define the interceptor class by referencing
com.jboss.dvd.seam.CheckoutInterceptor, next we need to specify when this class should be called, which is done by adding the second piece of xml which specifies the bean name for which the interceptor should fire. Here we want it to fire when the
CheckoutActionis called.
2. Add the interceptor class
The interceptor class itself looks like
package com.jboss.dvd.seam;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class CheckoutInterceptor {
@AroundInvoke
public Object sendOrderToESB(InvocationContext ctx) throws Exception {
System.out.println("*** Entering CheckoutInterceptor");
Object target = ctx.getTarget();
//Just making sure
if (target instanceof CheckoutAction) {
if (ctx.getMethod().getName().equals("submitOrder")) {
System.out
.println("We will send the following completedOrder object to ESB");
Order completedOrder = ((CheckoutAction) target).currentOrder;
Customer customer = ((CheckoutAction) target).customer;
completedOrder.setCustomer(customer);
System.out.println("Completed Order= " + completedOrder);
}
}
try {
return ctx.proceed();
} finally {
System.out.println("*** Exiting CheckoutInterceptor");
}
}
}
Figure 2. The CheckoutInterceptor Code
And that is all there is to it. In the interceptor we check which method is called, and if it is the
submitOrdermethod we print out the order. In the demo we added code to serialize the order to XML, and then dropped in onto a gateway Queue, on its way to the ESB.