Java RMI Hello Worldの例
RMIはRemote Method Invocationの略で、RPC(Remote Procedure Calls)と同等のオブジェクト指向です。 RMI was designed to make the interaction between applications using the object-oriented model and run on different machines seem like that of stand-alone programs.
以下のコードは、サーバーとクライアント間の通信モデルの非常に単純な例を使用して、Java RMIの基礎を提供します。
1. リモートインターフェース
最初に設計する必要があるのは、サーバーとクライアントの両方が実装するRemote
インターフェイスです。 Interface
は常にパブリックであり、Remote
を拡張する必要があります。 Remote
インターフェースで説明されているすべてのメソッドは、throws節にRemoteException
をリストする必要があります。
RMIInterface
にはメソッドが1つしかありません。 Stringパラメータを受け取り、Stringを返します。
RMIInterface.java
package com.techfou.rmiinterface; import java.rmi.Remote; import java.rmi.RemoteException; public interface RMIInterface extends Remote { public String helloTo(String name) throws RemoteException; }
2. サーバー
サーバーはUnicastRemoteObject
を拡張し、以前に作成したInterface
を実装します。 mainメソッドでは、localhost上のサーバーを「MyServer」という名前でバインドします。
ServerOperation.java
package com.techfou.rmiserver; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import com.example.rmiinterface.RMIInterface; public class ServerOperation extends UnicastRemoteObject implements RMIInterface{ private static final long serialVersionUID = 1L; protected ServerOperation() throws RemoteException { super(); } @Override public String helloTo(String name) throws RemoteException{ System.err.println(name + " is trying to contact!"); return "Server says hello to " + name; } public static void main(String[] args){ try { Naming.rebind("//localhost/MyServer", new ServerOperation()); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.toString()); e.printStackTrace(); } } }
3. クライアント
最後に、クライアントを作成します。 サーバーを「検索」するために、クライアントは、パラメーターとして渡した名前に関連付けられたリモートオブジェクトの参照を「検索」するRMIInterface
オブジェクトを使用します。 そのRMIInterface
オブジェクトを使用して、サーバーと通信し、応答を受信できます。
ファイル:ClientOperation.java
package com.example.rmiclient; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import javax.swing.JOptionPane; import com.example.rmiinterface.RMIInterface; public class ClientOperation { private static RMIInterface look_up; public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { look_up = (RMIInterface) Naming.lookup("//localhost/MyServer"); String txt = JOptionPane.showInputDialog("What is your name?"); String response = look_up.helloTo(txt); JOptionPane.showMessageDialog(null, response); } }
4. 実行方法
4.1 Create the three java files with your favorite IDE or Download the code below. 以下に示すように、ソースフォルダーに移動します。
4.2 First thing we need to do is compile our sources. 以下のコードをダウンロードした場合はcompileEverything.bat
を実行するか、ディレクトリでコマンドウィンドウを開いて実行します。
ターミナル
javac src/com/example/rmiinterface/RMIInterface.java src/com/example/rmiserver/ServerOperation.java src/com/example/rmiclient/ClientOperation.java
4.3 Confirm that your sources were compiled by accessing their respective directories:
4.4 Next we need to start the rmiregistry. 再度、startServer.bat
を実行するか、コマンドウィンドウを開いて次を実行します。
ターミナル
cd src start rmiregistry java com.example.rmiserver.ServerOperation
4.5 If RmiRegistry started successfully, there will be another window that looks like this:
4.6 Now we are ready to run our Client:
新しいコマンドプロンプトウィンドウを開き(またはダウンロードしたファイルからrunClient.bat
を実行し)、次のコマンドを実行します。
ターミナル
cd src java com.example.rmiclient.ClientOperation
ClientOperation
が実行され、入力を求められます。
入力フィールドに名前を入力します(例: 「マリレナ」)、「OK」をクリックします
4.7 On Server side we can see this:
4.8 And on the Client side this:
4.9 The client closes after the exchange is over, while the server stays on and we can communicate again if we run the client file again. 実際、クライアントを再度実行し、次のことを行います。
サーバー:
クライアント:
ソースコードをダウンロード
ダウンロード–SimpleRmiExample.zip(4 KB)