トップ «前の日記(2009-01-30) 最新 次の日記(2009-02-07)» 編集

Ussy Diary


2009-02-03

[Java]RMI によるプロセス間通信

同一マシンで RMI による取得を試みてみたところ、手元の MacBook でクライアントの実行に平均で 330 ms 秒かかってしまった。リモート側のデータの更新頻度がそれなりに高くリモートとのやりとりが増えるとなると、この処理時間では影響が出るので別の手段を考えよう。 RMI が悪いのでなく求めているものと残念ながら一致していないだけ。

初回アクセス時の初期化処理に時間がかかるだけで、 2 度目移行は 1ms と高速でした。適当なこと書いてすみませんでした。

public interface Echo extends Remote {
    String getMessage(String message) throws RemoteException;
}
public class HelloWorldEcho implements Echo {
    public String getMessage(String message) throws RemoteException {
        return "Hello World! " + message;
    }

    public static void main(String[] args) throws Exception {
        Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        Echo echo = new HelloWorldEcho();
        Remote remote= UnicastRemoteObject.exportObject(echo, 0);
        registry.rebind("echo", remote);

        Thread.sleep(60000); // 1 分だけ起動
    }
}
public class EchoClient {
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        Echo echo = (Echo)Naming.lookup("echo");
        System.out.println(echo.getMessage("hoge"));
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        System.out.println(echo.getMessage("hoge"));
        System.out.println(System.currentTimeMillis() - start);
    }
}

サンプルなので throws Exception しています。