JAVA基础之分布式对象

  1. RMI,远程方法调用。

  2. 传统的Web应用程序,只限于对请求使用HTTP协议,对响应使用HTML

  3. 使用代理的RMI,代理之间通信实现技术:

    CORBA,通用对象请求代理架构,支持任何编程语言编写的对象之间的方法调用。使用二进制的IIOP协议来实现对象间的对信。IDL定义接口

    Web服务架构WS,统称为WS-*,独立于编程语言,基于xml格式的SOAP协议通信。WSDL定义接口

    RMI,Java远程方法调用技术,支持JAVA分布式对象之间的方法调用。

    如果相互通信的程序都是由java实现,那么,CORBA与WS*-的通用性和复杂性就不是需要的。RMI是专门针对java之间的通信。

    200207_ykLH_2500345.jpg

  4. RMI调用过程

    203404_YwQl_2500345.jpgRMI使用序列化机制编码,SOAP协议中对象被编码为xml

    实现远程对象和获取客户端存根的细节有赖于分布式对象采用的技术

  5. 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);
        }
    }

    225225_D9S0_2500345.png

  6. JVM之间传递值有两种机制

    1)实现了Remote接口的类的对象作为远程引用传递;

    2)实现了Serializable接口,没有实现Remote接口的类的对象将使用序列化复制传递;

    序列化对于大型对象来说比较慢,而已只是传递副本,不能改变传递参数;可以选择传递引用,远程调用方法比本地调用方法开销大得多。


转载于:https://my.oschina.net/u/2500345/blog/539026

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值