`

代理模式Remote Proxies(三、RMI)

 
阅读更多

因为我本科毕业设计中大量采用RMI实现分布式,且使用了Eclipse中用于开发RMI的插件,这里主要阐述以下几点:

一、注意;

二、代码;

三、如何手工编写RMI应用。

 

一、注意

1)RMI产生stub的改进

P128. Earlier versions of the JDK constructed separate (stub) files for use on the client and server machines. As of 1.2, the RMI compiler("rmic" command) creates a single stub file that both the client and server machines need.

 

2)RMI的好处——远程代理的好处

P131 The benefit of RMI is that it lets client programs("ShowRocket Client") interact with a local object("RocketImpl_Stub") that is a proxy for a remote object("RocketImpl")!!!

“本地的代理”代理了“远程真正实现服务的对象”

 

二、代码

接口Rocket.java

 

package com.oozinoz.remote;

import java.rmi.*;

public interface Rocket extends Remote {
    void boost(double factor) throws RemoteException;
    double getApogee() throws RemoteException;
    double getPrice() throws RemoteException;
}

 

接口实现RocketImpl.java

package com.oozinoz.remote;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class RocketImpl extends UnicastRemoteObject implements Rocket {
    protected double price;
    protected double apogee;

    public RocketImpl(double price, double apogee) throws RemoteException {
        this.price = price;
        this.apogee = apogee;
    }

    public void boost(double factor) {
        apogee *= factor;
    }

    public double getApogee() throws RemoteException {
        return apogee;
    }

    public double getPrice() throws RemoteException {
        return price;
    }
}

 

在网络上某个节点注册一个对象RegisterRocket.java

package com.oozinoz.remote;

import java.rmi.*;

public class RegisterRocket {

    public static void main(String[] args) {
        try {
            Rocket biggie = new RocketImpl(29.95, 820);
            Naming.rebind("rmi://localhost:5000/Biggie", biggie);
            System.out.println("Registered biggie");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 在网络上另一个节点查询这个对象ShowRocketClient.java

package com.oozinoz.remote;

import java.rmi.*;

public class ShowRocketClient {

    public static void main(String[] args) {
        try {
            Object obj = Naming.lookup("rmi://localhost:5000/Biggie");
            Rocket biggie = (Rocket) obj;
            System.out.println("Apogee is " + biggie.getApogee());
        } catch (Exception e) {
            System.out.println("Exception while looking up a rocket:");
            e.printStackTrace();
        }
    }
}

  

三、手工编写RMI应用

1、编译

rmi>dir
Rocket.java
RocketImpl.java
RegisterRocket.java
ShowRocketClient.java

rmi>javac -d . *.java --> 编译java文件,指定存放生成类文件的位置
rmi>dir
Rocket.java
RocketImpl.java
RegisterRocket.java
ShowRocketClient.java
com\oozinoz\remote
Rocket.class
RocketImpl.class
RegisterRocket.class
ShowRocketClient.class

 

2、生成Stub,这个Stub在客户端和服务器端都要使用

rmi>rmic com.oozinoz.remote.RocketImpl
--> 为RocketImpl生成Stub,这个Stub需要被放到客户端和服务器两端
rmi>dir
Rocket.java
RocketImpl.java
RegisterRocket.java
ShowRocketClient.java
com\oozinoz\remote
Rocket.class
RocketImpl.class
RegisterRocket.class
ShowRocketClient.class
RocketImpl_Stub.class

 

3、运行效果

  • 大小: 268.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics