File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Lifecycle callback interceptor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Lifecycle callback interceptor" Watch "Lifecycle callback interceptor" New topic
Author

Lifecycle callback interceptor

Pavan Shahi
Ranch Hand

Joined: May 01, 2006
Posts: 39
Hi

Can I define more then one life cycle callback interceptor method for the same lifecycle event of a bean

For example I have two interceptor class

1.
public class MyInterceptorOne {
...
@PostConstruct
public void any-method-name (InvocationContext ctx) {
...
ctx.proceed();
...
}

}

2
public class MyInterceptorTwo {
...
@PostConstruct
public void any-method-name (InvocationContext ctx) {
...
ctx.proceed();
...
}

}

Then I impose these two interceptors on single bean class. Is this is possible ?


SCJP 1.4 (93%)<br />SCBCD 5 (96%)
Reddy Kumar
Greenhorn

Joined: Apr 09, 2008
Posts: 3
You MUST have zero or one callback method defined for any lifecycle event but, the same method could serve more than one lifecycle event.

Correct me if I'm wrong.
Steven Young
Ranch Hand

Joined: Apr 11, 2007
Posts: 36
You can have several interceptor classes, each containing the same callback method. The limit refers to having the same callback method in the same class. Spec says

"a given class may not have more than one lifecycle callback interceptor method for the same lifecycle event"

However, you can have several interceptor classes as in the Pavan's post. A long as each interceptor class only defines the lifecycle callback (e.g @PostConstruct) once within a class.

The spec alludes to multiple interceptor methods containing lifecycle callbacks in the statement:
"Lifecycle callback interceptor methods may be defined on superclasses of the bean class or interceptor classes" - note the use of plurals.

This is also backed up by EJB3 In Action which has the statement:
"Note that a bean can have the same lifecycle callbacks both in the bean itself as well as in one or more interceptors."

So in Paven's example, you would annotate the session bean with:
@Interceptors( {MyInterceptorOne.class, MyInterceptorTwo.class} )
Pavan Shahi
Ranch Hand

Joined: May 01, 2006
Posts: 39
Hi Steven Young

Thanks giving such a good explanation for my question. I hope it will clarify doubts of other ranchers also.
Pavan Shahi
Ranch Hand

Joined: May 01, 2006
Posts: 39
Hi Steven

I have one more question.

As for spec �if an AroundInvoke method is overridden by another method (regardless of whether that method is itself an AroundInvoke method), it will not be invoked�.
Is this rule is also apply for lifecycle callback method ?

Means if override same life cycle method in my bean class or in two interceptors classes will it invoke or not?
Steven Young
Ranch Hand

Joined: Apr 11, 2007
Posts: 36
Hi Paven,

EJB Core Spec (page 307) says
"If a lifecycle callback interceptor method is overriden by another method (regardless of whether that method is itself a lifecycle interceptor callback interceptor method (of same or different type) ), it will not be invoked"

This is how I think this statement should be interpreted:

Let's say you created a ParentInterceptor class with @PostConstruct method init(), and a ChildInterceptor class that extends ParentInterceptor which is listed in the Interceptors annotation in a stateless session bean called MyBean:

@Stateless
@Interceptors(ChildInterceptor.class)
public class MyBean{
}

If the ChildInterceptor class does not override the init() method, then the ParentInterceptor.init() method would be invoked when stateless session bean MyBean is instantiated.

If you created method "@PostConstruct init()" in ChildInterceptor.class, than the ParentInterceptor.init() method would not be invoked and the ChildInterceptor.init() method would run when MyBean is instantiated.

If you created method init() without the @PostConstruct annotation in ChildInterceptor, then neither init() method would be invoked when MyBean is instantiated.

Remember, if you have defined a @PostConstruct method in MyBean, that method would still be invoked when you instantiate a MyBean, because it is unaffected by the overriding rules in your Interceptor class hierarchies.

This stuff gets complicated and messy when you try to think through all the permutations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lifecycle callback interceptor
 
Similar Threads
Interceptors invocation sequence?
Multiple life-cycle Callback methods per class
Exceptions in lifecycle callback methods (I)
@ExcludeClassInterceptors annotation
Exceptions in lifecycle callback methods (II)