RMI,远程方法调用。java
传统的Web应用程序,只限于对请求使用HTTP协议,对响应使用HTML编程
使用代理的RMI,代理之间通讯实现技术:架构
CORBA,通用对象请求代理架构,支持任何编程语言编写的对象之间的方法调用。使用二进制的IIOP协议来实现对象间的对信。IDL定义接口编程语言
Web服务架构WS,统称为WS-*,独立于编程语言,基于xml格式的SOAP协议通讯。WSDL定义接口
分布式
RMI,Java远程方法调用技术,支持JAVA分布式对象之间的方法调用。ide
若是相互通讯的程序都是由java实现,那么,CORBA与WS*-的通用性和复杂性就不是须要的。RMI是专门针对java之间的通讯。
this
RMI调用过程编码
RMI使用序列化机制编码,SOAP协议中对象被编码为xmlurl
实现远程对象和获取客户端存根的细节有赖于分布式对象采用的技术代理
RMI编程模型
1)接口与实现
import java.rmi.Remote; import java.rmi.RemoteException; public interface Warehouse extends Remote { double getPrice(String description) throws RemoteException; }
/** * Unicast产生单一对象ip地址和端口 */ public class WarehouseImpl extends UnicastRemoteObject implements Warehouse { private Map<String, Double> prices; protected WarehouseImpl() throws RemoteException { prices = new HashMap<String, Double>(); prices.put("Blackwell Toaster", 24.95); prices.put("ZapXpress Microwave Oven", 24.95); //若是不extends UnicastRemoteObject //调用静态方法实例化 // UnicastRemoteObject.exportObject(this, 0); } @Override public double getPrice(String description) throws RemoteException { Double price = prices.get(description); return price == null ? 0 : price; } }
2)RMI注册表
public class WarehouseServer { public static void main(String[] args) throws RemoteException, NamingException { System.out.println("Constructing server implementiong"); WarehouseImpl warehouse = new WarehouseImpl(); System.out.println("Bingding server implementiong to registry"); Context context = new InitialContext(); context.bind("rmi:warehouse", warehouse); System.out.println("Waiting for invocations from clients"); } }
public class WarehouseClient { public static void main(String[] args) throws NamingException, RemoteException { Context context = new InitialContext(); System.out.println("RMI registry bindings:"); Enumeration<NameClassPair> e = context.list("rmi://localhost/"); while (e.hasMoreElements()) { System.out.println(e.nextElement().getName()); } String url = "rmi://localhost/warehouse"; Warehouse warehouse = (Warehouse) context.lookup(url); String descr = "Blackwell Toaster"; double price = warehouse.getPrice(url); System.out.println(descr + ":" + price); } }
JVM之间传递值有两种机制
1)实现了Remote接口的类的对象做为远程引用传递;
2)实现了Serializable接口,没有实现Remote接口的类的对象将使用序列化复制传递;
序列化对于大型对象来讲比较慢,而已只是传递副本,不能改变传递参数;能够选择传递引用,远程调用方法比本地调用方法开销大得多。