Wednesday, September 3, 2008

What is the order of invocation on lifecycle interceptor methods for EJB3 beans - Passivate, Activate, PostContruct & PreDestroy annotations and xml

One of the confusing things about lifecycle interceptors is the fact that they can be defined in a variety of places, including a superclass, a deployment descriptor that references a class that acts as an interceptor, and even the EJB3 bean itself. So, the question arises, what is the order that a given set of lifecycle interceptor methods will be invoked if they are defined in a variety of different, yet valid, locations? Well, the EJB specification, namely page 306 of the core contracts document, specifically addresses this very question. Check it out!

"If multiple callback interceptor methods are defined for a lifecycle event for a bean, the following rules governing their invocation order apply. The deployment descriptor may be used to override the interceptor invocation order specified in annotations, as described in section 12.8.
• Default interceptors, if any, are invoked first. Default interceptors can only be specified in the deployment descriptor. Default interceptors are invoked in the order of their specification in the deployment descriptor.
• If there are any interceptor classes defined on the bean class, the lifecycle callback interceptor methods defined by those interceptor classes are invoked before any lifecycle callback interceptor methods defined on the bean class itself.
• The lifecycle callback interceptor methods defined on those interceptor classes are invoked in the same order as the specification of the interceptor classes in the Interceptors annotation.
• If an interceptor class itself has superclasses, the lifecycle callback interceptor methods defined by the interceptor class’s superclasses are invoked before the lifecycle callback interceptor method defined by the interceptor class, most general superclass first.
• After the lifecycle callback interceptor methods defined on interceptor classes have been invoked, then

• If a bean class has superclasses, any lifecycle callback interceptor methods defined on those superclasses are invoked, most general superclass first.
• The lifecycle callback interceptor method, if any, on the bean class itself is invoked.

• If a lifecycle callback interceptor method is overridden by another method (regardless of
whether that method is itself a lifecycle callback interceptor method (of the same or different
type)), it will not be invoked.

The deployment descriptor may be used to override the interceptor invocation order specified in annotations, as described in section 12.8.2. All lifecycle callback interceptor methods for a given lifecycle event run in the same Java call stack. If there is no corresponding callback method on the bean class (or any of its superclasses), the InvocationContext.proceed invocation on the last interceptor method defined on an interceptor class in the chain will be a no-op. The InvocationContext object provides metadata that enables interceptor methods to control the invocation of further methods in the chain. See Section 12.5."

No comments: