我试图了解如何以及何时在一个非常简单的应用程序中创建 RMI 连接。
server = new RemoteServer();
registry = LocateRegistry.createRegistry(PORT);
registry.bind("Remote", server);
客户端部分是:
registry = LocateRegistry.getRegistry(HOST, PORT);
remote = (IRemoteServer) registry.lookup("Remote");
假设远程已经定义了一些方法doSomething()
。现在,假设一切都设置正确并且正常工作,我试图了解 TCP 连接何时建立。
- 当我调用
lookup()
时是否会建立连接,并且每当我调用remote.doSomething()
时都会重用相同的连接?如果我第二次调用lookup()
,会创建第二个连接吗? - 或者是每次调用
remote.doSomething()
时都会创建一个新连接,然后再断开?
( This question 有点相关,但它谈论的是多个线程调用针对单个远程对象的调用。)
最佳答案
when the RMI connection gets created
没有单一的“RMI 连接”,因此问题的格式已经不正确。
Is a connection made when I call lookup()
是的,可能,见下文。
with that same connection getting reused whenever I call remote.doSomething()?
如果远程对象与注册表位于同一主机:端口上,则可能会出现这种情况,请参见下文;否则肯定会有新的TCP连接。
If I call lookup() a second time, will that create a second connection?
可能,请参见下文。
Or is a new connection created every time I call remote.doSomething() and then torn down afterwards?
可能不是。
这些都没有指定,但是 Sun/Oracle 实现在各种 RMI system properties 的控制下执行客户端连接池。 ,特别是 sun.rmi.transport.tcp.threadKeepAliveTime。
默认情况下,空闲时间超过 60 秒的连接将被关闭。以前是 15 秒。
关于java - RMI 连接何时创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519941/