This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes multiple AroundInvoke Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "multiple AroundInvoke" Watch "multiple AroundInvoke" New topic
Author

multiple AroundInvoke

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
What if there are two or many @AroundInvoke methods in single interceptor class (I mean the same class)? Which will be the first one that invoked?
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Hi,

It’s important to realize that an interceptor must always have only one method that is designated as the around invoke (AroundInvoke) method.


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Dirk Dühr
Greenhorn

Joined: Feb 08, 2009
Posts: 11
I'd say multiple AroundInvoke methods in the same class are not legal. But you may have seperate AroundInvoke methods on each level of the interceptor's class hierarchie.

That's what I found in the Java EE Specification 5.0:

12.3 Business Method Interceptors
"... However, only one AroundInvoke method may be present on a given class."

12.3.1 Multiple Business Method Interceptor Methods
"... If an interceptor class itself has superclasses, the interceptor methods defined by the interceptor class’s superclasses are invoked before the interceptor method defined by the interceptor class, most general superclass first."
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Thanks to Niranjan, and Dirk.

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Hi Dirk,

Thanks for adding the relevant information for 12.3.1.

Section 12.3.1 is talking of methods in the interceptor class heirarchy.

How would be the invocation if -
1. the invoked method is 'overloaded'
2. the invoced methos is 'overridden'.

in the heirarchy.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Niranjan Deshpande wrote:
How would be the invocation if -
1. the invoked method is 'overloaded'
2. the invoced methos is 'overridden'.


I don't understand that how it is possible to overload a method, which is annotated with @AroundInvoke annotation. Since the signature of the @ArounInvoke method must be consist with one InvocationContext parameter, how can it be overloaded?
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Good Catch!

so we have two scenarios, assuming interceptor class2 extends interceptor class1 :

1. class 1 has @AroundInvoke method1, class 2 has @AroundInvoke method2- here method1 will be executed and then methd2. Correct?

1. class 1 has @AroundInvoke method1, class 2 has @AroundInvoke method1- here method1 in class2 overrides the one in class1. What will happen here?
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
As I know, both scenarios above will provide the same result. Am I correct?
Dirk Dühr
Greenhorn

Joined: Feb 08, 2009
Posts: 11
I guess Treimin hits the mark:
The expected around-invoke method signature including return type and throws clause is fixed.

That means, any method overloading a specified around-invoke method (e.g. by specifiying different parameter types in the method signature) shoud be ignored by the EJB container, at least.

While overloaded around-invoke methods seem to be legal concering the specs, the ejb-jar_3_0.xsd scheme comments they are not:

ejb-jar_3_0.xsd:
...
<xsd:complexType name="around-invokeType">
<xsd:annotation>
<xsd:documentation>

The around-invoke type specifies a method on a
class to be called during the around invoke portion of an
ejb invocation. Note that each class may have only one
around invoke method and that the method may not be
overloaded.
...

So the EJB container might complain about overloaded around-invoke methods.

But overriding is funny enough. I just wonder about this scenario:



Now, I inject MySpecialInterceptor into an arbitrary busines method. Which around-invoke method body is executed?

Due to "base class first" + "override overrides" ;-) ... I'd assume:

So I actually defined two around-invoke methods in one interceptor class?!
Dirk Dühr
Greenhorn

Joined: Feb 08, 2009
Posts: 11
Niranjan Deshpande wrote:
....
so we have two scenarios, assuming interceptor class2 extends interceptor class1 :

1. class 1 has @AroundInvoke method1, class 2 has @AroundInvoke method2- here method1 will be executed and then methd2. Correct?

I think so, too. Base class first.

Niranjan Deshpande wrote:
...
1. class 1 has @AroundInvoke method1, class 2 has @AroundInvoke method1- here method1 in class2 overrides the one in class1. What will happen here?

Just realized I replicated your question with my preceeding post. Sorry, did not read your second scenario carefully enough.
I think in this case the invocation sequence is: class2.method1 since the invocation is triggered by the base class override, then class2.method2.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Hi Dirk,

Thanks for the xsd

Did you actually try running the interceptors? What was the result?

I think you did not define two methods in the same class, but you inherited them!

Dirk Dühr
Greenhorn

Joined: Feb 08, 2009
Posts: 11
Hi Niranjan,

Did not find the time yet. I'll give it a try the next days on Glassfish and JBoss.
I'll post my results soon.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: multiple AroundInvoke
 
Similar Threads
how many interceptor method in a Bean class ?
ejb3.0 Annotations
EJB Core spec 12.3.1 Multiple Business Method Interceptor Methods
Lifecycle callback interceptor
how to skip execution of a line in a method..need help ranchers