Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lifecycle callback interceptor

 
Pavan Shahi
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Reddy Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic