Java Reference
In-Depth Information
The following is the @AroundInvoke method of the DiscountVerifierInter-
ceptor that actually uses the invocation context as well as most of the methods we dis-
cussed earlier:
public Object giveDiscount(InvocationContext context)
throws Exception {
System.out.println("*** DiscountVerifier Interceptor"
+ " invoked for " + context.getMethod().getName() + " ***");
if (context.getMethod().getName().equals("chargePostingFee")
&& (((String) (context.getContextData().get("MemberStatus")))
.equals("Gold"))) {
Object[] parameters = context.getParameters();
parameters[2] = new Double((Double) parameters[2] * 0.99);
"*** DiscountVerifier Reducing Price by 1 percent ***");
return context.proceed();
You can throw or handle a runtime or checked exception in a business method interceptor.
If a business method interceptor throws an exception before invoking the proceed method,
the processing of other interceptors in the invocation chain and the target business method
will be terminated.
This covers how interceptors are trigged for business methods. But recall that an EJB is
more than just business methods—it has an entire lifecycle. This isn't readily obvious,
but lifecycle callbacks are a form of interception as well. Lifecycle callbacks are triggered
when a bean transitions from one lifecycle state to another. Although this wasn't the case in
the previous lifecycle examples, in some cases such methods can be used for crosscutting
concerns (for example, logging and profiling) that can be shared across beans. For this reas-
on, you can define lifecycle callbacks in interceptor classes in addition to business method
interceptors. Let's take a look at how to do this.
Lifecycle callback methods in the interceptor class
The @PostConstruct , @PrePassivate , @PostActivate , and @PreDestroy
annotations can be applied to bean methods to receive lifecycle callbacks. When applied to
interceptor class methods, lifecycle callbacks work in exactly the same way. Lifecycle call-
backs defined in an interceptor class are known as lifecycle callback interceptors or life-
Search WWH ::

Custom Search