了解 java RMI exportObject 方法

Understanding java RMI exportObject method

我是 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

因此我们创建一个存根并将其手动或通过 RmiRegistry 转移到另一个 VM,这里无关紧要。

我对“[...] 使用服务器套接字导出对象 [...]”感到困惑

这是什么意思?

  • 创建一个 ServerSocket 以侦听您在导出时指定的端口上的传入连接。此端口可以在多个远程对象之间共享。
  • 关于 RMISocketFactory 的说法是不正确的。你在哪里读到的?此 class 自 1998 年以来已过时。
  • 存根包含服务器的主机名或 IP 地址和端口号,以及一些用于识别它所属的远程对象的内部数据。
  • 存根和远程对象之间的 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.

正确。