我对 RMI 很陌生,我只是决定尝试一下。我对 exportObject(Object, int)
方法感到困惑。文档说:
Exports the remote object to make it available to receive incoming calls, using the particular supplied port. The object is exported with a server socket created using the
RMISocketFactory
class.
考虑以下简单示例:
public interface Client extends Remote {
void clientMethod() throws RemoteException;
}
public class ClientImpl implements Client {
public clientMethod() throws RemoteException {
System.out.println("clientMethod invoked");
}
}
Client stub = (Client) UnicastRemoteObject
.exportObject(new ClientImpl(), 56789); //<------ HERE
因此,我们创建一个 stub ,并将其手动或通过 RmiRegistry
传输到另一个 VM
,这在这里并不重要。
我对“[...] 对象是通过服务器套接字导出的 [...]”感到困惑
他们的意思是什么?
最佳答案
- 创建一个
ServerSocket
来监听导出时指定的端口上的传入连接。该端口可以在多个远程对象之间共享。 - 有关
RMISocketFactory
的说法不正确。你在哪里读到的?该类自 1998 年以来已过时。 - stub 包含服务器的主机名或 IP 地址和端口号,以及一些用于标识其所属远程对象的内部数据。
- 当您调用远程方法时,会通过连接池按需创建 stub 和远程对象之间的 TCP 连接。
So, when we transfer the stub to another VM (VM 0), the stub will hold a socket connection to the VM (VM 2) it was originally created on.
不,请参阅上文。
The VM 2 in turn will maintain a server socket to accept incoming method invocations.
正确。
关于java - 理解java RMI的exportObject方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39902866/