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)