日期:2014-05-20 浏览次数:20636 次
import javax.naming.Context;
import javax.naming.InitialContext;
public class WarehouseServer {
public static void main(String[] args) throws Exception{
System.out.println("Constructing server implementation ...");
WarehouseImpl centralWarehouse = new WarehouseImpl();
Context namingContext = new InitialContext();
namingContext.bind("rmi:central_warehouse", centralWarehouse);
System.out.println("Waiting for invocations from clients...");
}
}
package chapter11.server;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import javax.naming.Context;
import javax.naming.InitialContext;
public class WarehouseServer {
public static void main(String[] args) throws Exception{
System.out.println("Constructing server implementation ...");
WarehouseImpl centralWarehouse = new WarehouseImpl();
Context namingContext = new InitialContext();
LocateRegistry.createRegistry(8888);
namingContext.bind("rmi://localhost:8888/central_warehouse", centralWarehouse);
System.out.println("Waiting for invocations from clients...");
}
}
import java.util.Enumeration;
import javax.naming.*;
public class WarehouseClient {
public static void main(String[] args) throws Exception{
Context namingContext = new InitialContext();
System.out.println("RMI registry bindings:");
Enumeration<NameClassPair> e = namingContext.list("rmi://localhost:8888/");
while(e.hasMoreElements())
System.out.println(e.nextElement().getName());
String url = "rmi://localhost:8888/central_warehouse";
Warehouse centralWarehouse = (Warehouse)namingContext.lookup(url);
}
}