2 Replies Latest reply on Aug 31, 2009 6:29 AM by henk de boer

    PortableRemoteObject.narrow() still needed in EJB3 / Jboss A

    henk de boer Master

      Hi,

      In a lot of EJB3 example code I often encounter the use of PortableRemoteObject.narrow() to do a sort of double casting from the Object obtained from JNDI to the target EJB (remote) interface.

      However, I found that at least in Jboss AS 5 I never seem to need to do this 'double casting'. Both local references and more importantly remote references, even when truly obtained in a remote JVM, can be directly cast to the required interface.

      I found this old blog posting: http://www.bright-green.com/blog/2003_04_03/portableremoteobjectnarrow_mus.html it tries to explain why PortableRemoteObject.narrow() was needed in the first place, but I just don't see the logic. The rationale seems to be that some obscure other languages might not have casting and thus need such a method. Java however does have native casting, but somehow still needs or needed this method.

      I found another explanation in Mastering EJB 4th Edition:


      After looking up the remote object, we perform the operation javax.rmi.PortableRemoteObject.narrow(). This is a static method on an RMI-IIOP class called PortableRemoteObject. This method casts the generic object that we looked up via JNDI to our RMI-IIOPinterface type. This narrow() operation is required whenever you look up an RMI-IIOP object via JNDI. Why do we need it, and why don't we just cast it using a regular Java cast? The short answer is that the networking layer needs a chance to sneak in and create the necessary stub objects. Obviously, the code for these stubs must be available at this time. In most cases, that requires that the rmiccommand has been run previously to generate them.


      That too doesn't sound very logical, since JNDI is an interface anyway and the SPI implementation can always return that stub too of course if PortableRemoteObject.narrow() is able to do it.

      My question now is, is PortableRemoteObject.narrow() completely deprecated or is it still needed for some specific use cases?