要編寫Java的RMI應用程式,必須遵循以下步驟:
- 定義遠程介面
- 開發實現類(遠程對象)
- 開發伺服器程式
- 開發客戶端程式
- 編譯應用程式
- 執行應用程式
定義遠程介面
遠程介面提供特定遠程對象的所有方法的描述。客戶端可與此遠程介面進行通信。
創建遠程介面 -
- 創建一個擴展預定義介面的介面,該介面屬於該包。
- 聲明客戶端在此介面中可以調用的所有業務方法。
- 由於在遠程調用期間存在網路問題,因此可能會拋出一個名稱為
RemoteException
的異常。
以下是遠程介面的示例。首先定義一個名為Hello
的介面,此介面有一個名為printMsg()
的方法。
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface Hello extends Remote {
void printMsg() throws RemoteException;
}
開發實現類(Remote Object)
我們需要實現在前面的步驟中創建的遠程介面。可以單獨寫一個實現類,也可以直接使伺服器程式實現這個介面。
開發一個實現類 -
- 實現上一步創建的介面。
- 提供遠程介面的所有抽象方法的實現。
以下是一個實現類。 在這裏,我們創建了一個名為ImplExample
的類,並實現了在上一步中創建的介面Hello
,並提供了列印消息 - printMsg()
方法的具體實現。
// Implementing the remote interface
public class ImplExample implements Hello {
// Implementing the interface method
public void printMsg() {
System.out.println("This is an example RMI program");
}
}
開發伺服器程式
RMI伺服器程式應實現遠程介面或擴展實現類。在這裏,我們應該創建一個遠程對象並將其綁定到RMIregistry
。
開發伺服器程式 -
- 從要調用遠程對象的位置創建一個客戶端類。
- 通過實例化實現類創建一個遠程對象,如下所示。
- 使用屬於包
java.rmi.server
中的UnicastRemoteObject
類的exportObject()
方法導出遠程對象。 - 使用屬於
java.rmi.registry
包中的LocateRegistry
類的getRegistry()
方法獲取RMI註冊表。 - 使用
Registry
類的bind()方法將創建的遠程對象綁定到註冊表。對於此方法,傳遞表示綁定名稱和導出的對象的字串作為參數。對於此方法,需要傳遞一個表示綁定名稱的字串值作為參數。這將返回遠程對象。
以下是RMI伺服器程式的示例,創建一名為:Server.java的檔保存以下代碼 -
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample {
public Server() {}
public static void main(String args[]) {
try {
// Instantiating the implementation class
ImplExample obj = new ImplExample();
// Exporting the object of implementation class
// (here we are exporting the remote object to the stub)
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
開發客戶端程式
在這裏我們編寫一個客戶端程式,獲取遠程對象並使用此對象調用所需的方法。
要開發客戶端程式,請參考以下步驟 -
- 從想要調用遠程對象的地方創建一個客戶端類。
- 使用屬於
java.rmi.registry
包中的LocateRegistry
類的getRegistry()
方法獲取RMI註冊表。 - 使用屬於
java.rmi.registry
包中的Registry
類的lookup()
方法從註冊表獲取對象。 lookup()
返回一個類型為remote
的對象,將其轉換為Hello
類型。- 最後使用獲取的遠程對象調用所需的方法。
以下是RMI客戶端程式的示例,創建一個名稱為:Client.java 的檔 -
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
try {
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
// Looking up the registry for the remote object
Hello stub = (Hello) registry.lookup("Hello");
// Calling the remote method using the obtained object
stub.printMsg();
// System.out.println("Remote method invoked");
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
編譯應用程式
編譯應用程式 -
- 編譯遠程介面。
- 編譯實現類。
- 編譯伺服器程式。
- 編譯客戶端程式。
或者 -
打開存儲所有程式的檔夾,使用以下命令編譯所有Java檔,如下所示 -
javac *.java
執行結果如下 -
執行應用程式
第一步 - 使用以下命令啟動rmi註冊表。
start rmiregistry
這將在單獨的窗口中啟動一個rmi註冊表,如下所示。
第二步 - 運行伺服器類檔,如下所示。
java Server
打開另一個命令行提示符,執行上面命令,如下所示 -
第三步 - 運行客戶端類檔,如下所示。
java Client
打開另一個命令行提示符,執行上面命令,如下所示 -
驗證遠程調用結果 - 當啟動客戶端後,將在伺服器中看到以下輸出。
上一篇:
Java RMI介紹
下一篇:
Java RMI圖形介面應用程式