narrowing & casting with Handle.getEJBObject

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

Micheal Jacob

Ranch Hand

Posts: 89

posted 10 years ago

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.

Ankit Doshi

Ranch Hand

Posts: 222

posted 10 years ago

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

Micheal Jacob

Ranch Hand

Posts: 89

posted 10 years ago

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.

Roger Chung-Wee

Ranch Hand

Posts: 1683

posted 10 years ago

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.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3

Ankit Doshi

Ranch Hand

Posts: 222

posted 10 years ago

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?