Forums Register Login

narrowing & casting with Handle.getEJBObject

+Pie Number of slices to send: Send
Page# 139 on the Head First EJB says the following:


Remember, you always have to cast and narrow a stub unless the method that returns it has the actual Remote interface as its declared return type.



The declared return type of the getEJBObject method of the Handle interface is itself "EJBObject". Then why do we need to narrow and cast while calling the getEJBObject method over the handle object?

Can someone please help me understand this?


Thanks,
Ankit
+Pie Number of slices to send: Send
Hello Ankit,

When getEJBObject() is called on deserialized handle, the return type is EJBObject.

EJBObject is common interface extended by all bean provider's component remote interface.

Until you don't narrow it (because of RMI-IIOP) to particular component remote interface, you cannot call bean provider's business methods.

Hope it helps.

Micheal.
+Pie Number of slices to send: Send
Thanks for your reply Michael.

I have still some more confusion. I tried serializing the handle and then deserializing and using it - without narrowing, just casting. Following the short code snippet for AdviceClient and AdviceClient2

AdviceClient


AdviceClient2


Note above that I have just used plain old casting, (no narrowing).

Now, I initially ran the AdviceClient. Then I undeployed the app and shutdown the j2ee server. Then after some time, I started the j2ee server back up, deployed the app, and after that I ran the AdviceClient2, and it worked.

Please help me understand the above.


Thanks,
Ankit
+Pie Number of slices to send: Send
Hi,

Which j2ee server you are using ?

EJB Spec (Page No 65) says, you should narrow it after deserializing the handle.

Your j2ee server may be helping you (in terms of reducing one line of code !!! by allowing you to use java casting), but your client code cannot be used as it is if you change your EJB Container.

Micheal.
+Pie Number of slices to send: Send
You must always assume that the server is using RMI-IIOP. So, it is necessary to narrow and cast. To do otherwise will cost you marks in the exam.
+Pie Number of slices to send: Send
Well, I was using the J2EE RI.

To clarify more on at what all times we should narrow and cast -- Is it that if the "declared" return type of a method is anything other than the actual remote component interface type (Advice in case of the Advice application from HFEJB) then you always have to narrow and cast - is this right?


Thanks,
Ankit
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
That clarifies, Thanks.
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 944 times.
Similar Threads
Narrowing of EJBObject from Handle
context.lookup()
Narrowing
PortableRemoteObject.narrow() explain
Narrowing of return from getEJBHome()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:52:55.