rmiregistry 或 JNDI),等待調用。若接收端 JVM 未定義對象類,RMI 自動從 Codebase(HTTP 服務器)下載字節碼。對象按真實類型傳遞,行為不變,實現行為型應用:服務端無需預知任務類,運行時動態加載新任務實現。
java.rmi.Remote(標記接口)。RemoteException。Serializable)。RemoteInterface extends Remote)。Impl extends UnicastRemoteObject implements RemoteInterface)。rmic 生成 Stub/Skeleton(Java 5+ 無需 Skeleton)。rmiregistry,發布類文件到 HTTP 服務器(Codebase)。Naming.rebind("//host/Name", remoteObj)。RemoteInterface r = (RemoteInterface) Naming.lookup("//host/Name")。// 任務接口
interface Task extends Serializable {
Object execute();
}
// 遠程引擎接口
interface Compute extends Remote {
Object executeTask(Task t) throws RemoteException;
}
// 服務端實現
class ComputeEngine extends UnicastRemoteObject implements Compute {
public Object executeTask(Task t) { return t.execute(); }
}
// 客戶端提交任務
class PiTask implements Task { ... } // 計算圓周率
Compute engine = (Compute) Naming.lookup("//server/Compute");
Object result = engine.executeTask(new PiTask());
RMISocketFactory 固定端口或使用 HTTP 通道。