2 Replies Latest reply on Oct 2, 2001 3:56 PM by Craig Reichenbach

    PortableRemoteObject vs ctx.lookup

    Craig Reichenbach Newbie

      I'm getting back into EJB - last time I was exposed to it the Home interface was gotten using a lookup on the JNDI name. The JBoss example uses a static PortableRemoteObject method. Can someone tell me which is the preferred method to get the Home interface and why?

      Object ref = jndiContext.lookup("interest/Interest")
      InterestHome home = (InterestHome)PortableRemoteObject.narrow(ref, InterestHome.class);

      InterestHome home = (InterestHome) jndiContext.lookup("interest/Interest");


        • 1. Re: PortableRemoteObject vs ctx.lookup
          Jason Vasquez Newbie


          My understanding is that when working over RMI, the ctx.lookup with a cast will work fine. But, according to the newer EJB specs, the system should support RMI/IIOP as a transport--RMI/IIOP requires the call to PortableRemoteObject.narrow(). So, to create the most portable code, use the narrow() call. But, doing a plain lookup anda a cast works fine in JBoss for now.

          hope that helps,

          • 2. Re: PortableRemoteObject vs ctx.lookup
            Craig Reichenbach Newbie

            Thanks Jason. I found some additional information on this I will post should any other rusty ejb programmers stumble on this thread.


            From Borland:

            So what is actually going on? CORBA-based implementations, in general, do not return the "actual" server type from a method invocation. They return the "signature" type which can then be narrowed to a more specific type, if need be. In certain circumstances, determining if an object can be narrowed to the desired type requires an RPC to the server implementing the object. However, although our implementation is built on CORBA, we thought it was more user friendly to support the casting behavior, when possible. This was how our beta Container behaved. However, as stated above, sometimes this behavior requires an RPC to the server.
            So, now we can start to answer the question.
            Using javax.rmi.PortableRemoteObject.narrow will succeed in all cases, and is preferred. In fact the final EJB 1.1 specification pretty much mandates the use of PortableRemoteObject always.