假定在A机器(设IP为192.168.1.22)上部置EJB(ejb容器为JBOSS),其上有一无状态SessionBean First,First有一个方法   public java.lang.String sayHello( java.lang.String name ),First对应的jndi-name为"First". 在B机器上调用First的sayHello方法: package test; 
import java.rmi.RemoteException; import java.util.Date; import java.util.Hashtable; 
import javax.ejb.CreateException; import javax.naming.InitialContext; import javax.naming.NamingException; 
/**  * @author <a href="mailto:[email protected]">starfire </a>  *    */ public class TestFirst { 
    private test.FirstHome getHome() throws NamingException {         //First为jndi-name         return (test.FirstHome) getContext().lookup("First");     } 
    private InitialContext getContext() throws NamingException {         Hashtable props = new Hashtable();
  
        //不同容器的INITIAL_CONTEXT_FACTORY,PROVIDER_URL可能不一样         props.put(InitialContext.INITIAL_CONTEXT_FACTORY,                 "org.jnp.interfaces.NamingContextFactory");         props.put(InitialContext.PROVIDER_URL, "jnp://192.168.1.22:1099"); 
        InitialContext initialContext = new InitialContext(props);         return initialContext;     } 
    public void testBean() { 
        try {             InitialContext context = getContext();             //context.rebind("obj", new Date());             //Object obj = context.lookup("obj");             //System.out.println(obj);             test.First myBean = getHome().create(); 
            //--------------------------------------             //This is the place you make your calls.             System.out.println(myBean.sayHello("hc")); 
        } catch (RemoteException e) {             e.printStackTrace();         } catch (CreateException e) {             e.printStackTrace();         } catch (NamingException e) {             e.printStackTrace();         }     } 
    public static void main(String[] args) {         TestFirst test = new TestFirst();         test.testBean(); 
    } }  
 
  |