1

スタブを作成するときは、レジストリを起動してコードベースを指定しますか?

RMIアプリケーションを作成しました。私の簡単なアプリケーションは動作します。クライアントパッケージとサーバーパッケージのビルドパスにRemoteObjInterface.classのパッケージがあります。最初にサーバーアプリケーションを起動し、次にクライアントアプリケーションを起動します。

しかし、私はインターネットの他の例を見てきましたが、それらがレジストリを開始し、スタブを作成し、コードベースを指定しているのがわかります。

以下は私のプログラムです:

RemoteObjInterface.class」は私のインターフェース、「RemoteObjImplementation.class」は私のサーバー、「Client.class」は私のクライアントです。

public interface RemoteObjInterface extends Remote {
    public String someMethod() throws RemoteException;
        }

public class RemoteObjImplementation extends UnicastRemoteObject implements
    RemoteObjInterface {
   private static final long serialVersionUID = 1L;
   private static final int PORT = 1099;
   private static Registry registry;

public RemoteObjImplementation() throws RemoteException {
    super();
    }

@Override
public String someMethod() throws RemoteException {

    return new String("Hello");
    }

public static void main(String args[]) {

    Registry registry = LocateRegistry.createRegistry(PORT);

    registry.bind(RemoteObjInterface.class.getSimpleName(),
            new RemoteObjImplementation());

     }
}

public class Client {
    private static final String HOST = "localhost";
    private static final int PORT = 1099;
    private static Registry registry;

public static void main(String[] args) throws Exception {

registry = LocateRegistry.getRegistry(HOST, PORT);

 RemoteObjInterface remoteApi = (RemoteObjInterface) registry.lookup(RemoteObjInterface.class.getSimpleName());

    System.out.println("Message = " +
            remoteApi.someMethod();

    }
}
4

1 に答える 1

1

スタブを作成するタイミング

スタブの作成は、リモートオブジェクトをエクスポートすることの副作用であり、それが拡張された場合にそれを構築することの副作用ですUnicastRemoteObject

レジストリを開始します

あなたがそれを始めたいとき。たとえば、bind()電話をかける前に。rebind()

コードベースを指定しますか?

この機能を使用する必要はまったくありません。オプションです。クライアントがクラスを事前にクライアントに配布するのではなく動的にダウンロードできるようにする場合は、java.rmi.server.codebaseリモートオブジェクト(レジストリを含む)をエクスポートする前にサーバーJVMでシステムプロパティを指定し、URLを指していることを確認しますこれは、レジストリとクライアントの両方からアクセスできます。

于 2013-03-19T23:33:48.153 に答える