Remote,方法拋 RemoteException)UnicastRemoteObject)RMISecurityManager、建立實例、Naming.rebind() 註冊Naming.lookup() 取得 Stub、呼叫遠端方法rmic 產生 Stub/Skel、啟動 rmiregistry、啟動 Server/Client// 介面
public interface FileInterface extends Remote {
byte[] downloadFile(String fileName) throws RemoteException;
}
// 實作
public class FileImpl extends UnicastRemoteObject implements FileInterface {
public byte[] downloadFile(String fileName) {
File file = new File(fileName);
byte[] buffer = new byte[(int)file.length()];
new BufferedInputStream(new FileInputStream(file)).read(buffer);
return buffer;
}
}
// Server
Naming.rebind("//127.0.0.1/FileServer", new FileImpl("FileServer"));
// Client
FileInterface fi = (FileInterface) Naming.lookup("//host/FileServer");
byte[] data = fi.downloadFile("test.pdf");
// 寫入本地檔案
| 維度 | RMI | CORBA |
|---|---|---|
| 語言綁定 | Java Only | Multi-language (IDL) |
| 開發複雜度 | 低(純 Java、註解/介面驅動) | 高(IDL、編譯器、ORB 配置) |
| 傳輸協定 | JRMP (Java Remote Method Protocol) | IIOP (Internet Inter-ORB Protocol) |
| 序列化 | Java Serialization | CDR (Common Data Representation) |
| 安全/交易/命名 | J2EE 容器整合 (JNDI, JTA, JAAS) | CORBA Services (Security, Transaction, Naming, Trading) |
| 防火牆穿透 | 較難(動態埠口) | IIOP 固定埠口較易控制 |
| 效能 | Java 之間極佳 | 跨語言有序列化開銷 |
本文以檔案傳輸為例展示 RMI 完整開發流程,CORBA 篇幅受限略過詳細代碼,核心差異在於「語言中立」與「標準化開銷」的權衡。