File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Invoking EJB methods using reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Invoking EJB methods using reflection" Watch "Invoking EJB methods using reflection" New topic

Invoking EJB methods using reflection

Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I stumbled on something that is extremely counter intuitive. I wrote an EJB client that implements two different methods of invoking a normal EJB method. The first one employs the conventional way ie., to get home, create bean and call the method. The second one on the other hand, uses EJB metadata and reflection to invoke the EJB method.
Here is the code -

I wrote a stateless session bean, deployed it on WebLogic and ran my client invoking the same bean method using both mechanisms.
Much to my surprise, I noticed a significant difference in the response times involved between the two methods. Now comes the surprising part - using reflection actually performs better, way better than the conventional mode. My small benchmark on WebLogic shows -
Normal invoke took 9754 ms
Reflection invoke took 41 ms
I have always been told to avoid using reflection because of the extra involved in runtime introspection. However my little experiment shows otherwise.
What am I missing here?

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
What are you missing? Nothing.
You've just got the overhead of the JNDI calls, container overheads and so on. I usually find that if you only call an EJB once, you get hit badly on performance. Subsequent requests are usually much, much quicker. Have you tried measuring the average, min and max response times? That should present a more realistic picture.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Ok, I am going to try to measure the min, max and average times. But I should tell you the results are consistent. No matter what the first way of invocation is( I have tried swapping the two method calls), the reflection-way always performs way better than the conventional way.
By the way JNDI lookup is done in the getHome() method and as you can see in the code, getHome() is common to both the methods.
[ March 25, 2003: Message edited by: Ajith Kallambella ]
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Ajith & Simon,
This is a really interesting out come and i really am amazed that this behaviour which is totally the anti thesis of what we have been made to understand occurs.
And the overhead of the JNDI call is still there in both the methods. If you look at the way both the methods call the getHome method, i am assuming that that method does the JNDI look up.
Ajith what would really be interesting is if u could run the JProbe Profiler on this snippet and compare what methods in the 2 codes are taking how much time. I suppose that will give us a good idea about who is doing what.

Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
Ajith -
I am very new to this, so I apologise if I am totally offbeat.
I see in your normalInvoke() method, you have done the narrowing twice. Can't we do that only once while acquiring the home interface, and with that just call the home's create method.
Trader trader = home.create();
I'm following the EJB-Monson-Haefel's book; nowhere I see calling the PortableRemoteObject.narrow twice.
Thanks, Sudd

deena Janarthanam

Joined: Nov 15, 2002
Posts: 5
I think when you use conventional mechanism to call a business method, EJB remoteObject might be using the reflection over the network to invoke any business method,but it might be faster if you write your own reflection mechanism without network overhead using EJB metadata on client side and network might be used only to inovke a business method.
[ April 02, 2003: Message edited by: deena Janarthanam ]
[ April 02, 2003: Message edited by: deena Janarthanam ]
I agree. Here's the link:
subject: Invoking EJB methods using reflection
jQuery in Action, 3rd edition