Client View of Bean  «Prev  Next»

Lesson 4Finding the home object
ObjectiveDescribe how to look up the home object.

Finding and looking up the home object in EJB

The client acquires the remote reference to the home object from the Name Service. Assuming the name of the bean's service is "hello", the code fragment in the client will be:

javax-naming-context
javax.naming.Context initialContext = new javax.naming.InitialContext(); 
  1. The JNDI initialContext() method returns the initial context. The initial context is equivalent to the root directory of the JNDI Name Service. The result is stored in the variable initialContext
  2. Object objRef = initialContext.lookup("MyHello")
    
    Looks up the "MyHello" service in the initial context and returns the remote reference (the stub) for the Hello bean's home object. This reference is stored in objRef.
  3. javax.rmi.PortableRemoteObject.narrow: Transforms the actual remote reference type, stored in objRef, to a HelloHome reference.
  4. HelloHome.class: The class of the return from lookup. This is required by the narrow()method.
  5. (HelloHome): A Java cast to the correct type.
  6. HelloHome home: home will contain the remote reference (the stub) to the home object when the statement is completed. The stub implements the HelloHome interface.

The JNDI initialContext() method returns the initial context.

JNDI Initialcontext Lookup

JDNI Interface

This is not EJB code but rather JNDI, and this is all there is to it. Using JNDI is similar to looking up a file in a directory. Use the MouseOver to see the description of each piece of the code.

JNDI Advice

The EJB 1.1 spec. calls for use of the narrow() to ensure portability with RMI-IIOP[1]. This is the predominant protocol RMI will use in the future.

Remote Method Invocations

A remote procedure call (RPC) is a procedural invocation from a process on one machine to a process on another machine. RPCs enable traditional procedures to reside on multiple machines, yet still remain in communication. They are a simple way to perform cross-process or cross-machine networking. A remote method invocation in Java takes the RPC concept one step further and allows for distributed object communications. RMI-IIOP allows you to invoke methods on objects remotely, not merely procedures. You can build your networked code as full objects. This yields the benefits of an object-oriented programming, such as inheritance, encapsulation, and polymorphism.
In the next lesson, you will be introduced to code in the client used to create the session bean.

[1]RMI-IIOP: The protocol, based on IIOP, that is planned for the implementation of RMI.