RMI Lookup

A core technology of enterprise server and EJB applications is remote Method Invocation (RMI), Java's version of remote procedure calls. 

A server may instantiate an object and register it with a naming service, such as the RMI Registry or, in the case of WebSphere, the Java Naming and Directory Interface (JNDI).  The naming Service maps a public name for the object with a reference to that object.  When contacted, it delivers a stub object to the client and instantiates a corresponding skeleton object in the server.  The client may then cast that stub using an interface implemented by the actual instantiated object.  Thereafter the client may invoke the methods of the stub as if they were the methods of the actual implementation object. The stub and skeleton pass messages back and forth with the skeleton providing interaction with the implementation object on the server side analogous to the the interaction provided by the stub on the client side.

For an EJB container in which EJB Sessions are run, the container, or Home, is responsible for the actual instantiation of the EJB Session object, invoking a number of so-called life-cycle methods created by the VisualAge environment for that purpose. The ide also generates a set of corresponding hook methods, defined as part of the Home Interface, that allow the programmer to take actions upon creation of an object, removing it, or other similar actions.

Below is an example segment of code for performing the lookup of an EJB Session Bean and the creation of a Home object (the stub) that can be used to invoke the methods defined in its Remote Interface.  Note that the home object is created just once and used for the life of the program.


Preamble

package edu.jbs.ooc.ui;

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.ibm.ejs.ns.jndi.*;
import javax.naming.*;
import javax.ejb.*;

import edu.jbs.ooc.databeans.*;
import edu.jbs.ooc.beans.*;
import edu.jbs.ooc.util.*;

Session Variable

private SessionDB session;

Method to Build Session Object

public void buildSession() {

try
{

Properties properties = new Properties();
    properties.put( javax.naming.Context.PROVIDER_URL, "IIOP://localhost:900/" );
    properties.put( javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.ibm.ejs.ns.jndi.CNInitialContextFactory" );
   
    InitialContext initialContext = new InitialContext( properties );
        
    Object initialReference = initialContext.lookup( "edu/jbs/ooc/beans/SessionDB" );
   
    SessionDBHome home = (SessionDBHome)
    javax.rmi.PortableRemoteObject.narrow( initialReference, SessionDBHome.class );
       
    session = home.create();
    
}
catch(Throwable theException)
{
    System.out.println( "Error in Servlet: UIServlet.buildSession" );
    theException.printStackTrace();
}


}

Using the Session Object

public String add ( UIBean uiBean )  {         
       
	TransportBean reqBean = buildTransportBean( uiBean );
	reqBean.setAction("Add");
		
	ViewBean viewBean = getViewBean( reqBean );
	
	if ( reqBean == null )  {
		viewBean.setMessage( "Add failed; error building parameters." );
		return formFilled( viewBean );
	}
	
	try  
	{
		TransportBean respBean = (TransportBean)session.processAction( reqBean );
		if ( respBean != null )  {
			viewBean.setMessage( respBean.getResponseMessage() );
			ObjectBean objBean = (ObjectBean)(respBean.getBeanCurrent());
			if ( objBean != null ) viewBean.setValues( objBean.getValues() ); 
			return formFilled( viewBean );
		}  else  {
			viewBean.setMessage( "Add failed." );
			return formFilled( viewBean );
		}       
	}  catch(Throwable e)
	{
		System.out.println( "Error in processUserAction.add" );
		e.printStackTrace();
		viewBean.setMessage( "Add failed in processUserAction.catch." );
		return formFilled( viewBean );
	}		
        
}