使用多个对象进行RMI通信可以吗
场景:
- 客户端 C 通过 RMI 连接到服务器
- S C 要求 S 创建一个处理程序 H,S 将 H 返回给 C
- C,然后与 H 对话
现在我可以通过两种方式执行此操作:
- 将处理程序设置为远程处理程序并让 S 将存根返回给它,以便 C 可以直接与之对话 (
h.say(String msg);
) - 为处理程序提供一个 ID 并将其返回给 C。C 将通过以下方式与 H 对话S (
s.sayToHandler(int id, String msg);
)
第一个是更好的 OO,但是性能怎么样?是否会打开额外的 TCP 连接,还是使用 S 和 H 之间现有的连接?
Scenario:
- Client C connects to Server S via RMI
- C asks S to create a handler H, S returns H to C
- C then talks to H
Now I could do this in two ways:
- Make the handler a
Remote
and let S return the stub to it, so C can directly talk to that (h.say(String msg);
) - Give the handler an ID and return that to C. C will talk to H via S (
s.sayToHandler(int id, String msg);
)
The first is nicer OO, but what about the performance? Will an extra TCP connection be opened, or is the existing connection between S and H used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道实施情况。我认为不会建立新的联系。但我知道的是,远程共享的对象越多,依赖远程取消引用来进行垃圾收集的对象就越多(因此会有更多的对象寿命更长,这不好)。
替代方法
我会推荐一种混合方法。对客户端使用好的方法,但在内部以不太好的方式实现它:
BackDoor 和 Handler 是同步接口。第一个具有以 Handler 作为参数的方法,后者具有纯方法。我认为这没什么大不了的。这两个不同的接口让您可以干净地工作,而无需客户端一无所知,并允许瘦可序列化处理程序完成肮脏的工作。
希望你喜欢它!
I don't know about the implementation. I don't think a new connection is made. But what I know is the more objects you share remotely the more objects that depends on remote dereference to get garbage collected (so there will be more objects living longer, not good).
Alternative approach
I'll recommend a mixed approach. Use the nice approach for the client but implement it the not-so-nice-way internally:
BackDoor and Handler are sync'ed interfaces. The first has the methods with Handler as argument, the later has the pure methods. I don't think it's a big deal. And the two different interfaces let you work cleanly without the client know nothing and allowing the thin serializable Handler's do the dirty work.
Hope you like it!
RMI 规范并没有真正说明是否应该有一个新的连接或重用现有的连接。有线协议允许两者,使用多路复用 或每次调用一个 TCP 连接(我不确定,这可能会被重复用于对同一服务器对象的后续调用)。如果您需要通过 HTTP 建立隧道,则每个连接只允许一条消息。
我没有找到任何关于如何配置要使用的协议类型的信息(除了禁用 HTTP 隧道之外)。
如果您想确保只使用一个 TCP 连接,请使用自定义客户端和服务器套接字工厂通过它们本身来建立多个连接的隧道。这可能会比 RMI 运行时系统在那里执行的操作效率低。
The RMI specification does not really say whether there should be a new connection or the existing one reused. The wire protocol allows both, using either multiplexing or one TCP connection per call (which might be reused for following calls to the same sever object, I'm not sure). If you need tunneling through HTTP, than only one message per connection is allowed.
I did not find anything on how to configure which type of protocol to use (other than disabling HTTP tunneling).
If you want to make sure that only one TCP connection is used, use custom client and server socket factories doing the tunneling of multiple connections though one themselves. This will probably be less efficient than what the RMI runtime system would be doing there.