Java - 使用 RMI 检测客户端崩溃

发布于 2024-11-10 06:38:14 字数 140 浏览 1 评论 0原文

我已经创建了 RMI 连接,但我不知道如何在客户端结束或崩溃时通知服务器。我找到了 unreferenced() 方法,但它不起作用。有什么想法吗?
(服务器是多客户端的,只有一种对象传递给客户端,但任何客户端都有不同的实例)。
谢谢。

I have created a RMI connection but I do not know how to notify the server when the client ends or crashes. I have found the unreferenced() method, but it does not work. Any idea?

(the server is multiclient, there is only one kind of object passed to the client but any client has a different instance of it).
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2024-11-17 06:38:14

这样做的问题是,如果客户端崩溃,就很难让它做任何事情,并且最终会等待超时。

通常的解决方案是设置“心跳”,周期性调用;如果客户端心跳在 2 个周期后没有到达你的手中,你就会认为它已经死了。

The problem with that is that if the client crashes, it's hard to have it do anything, and you end up waiting for timeouts.

The usual solution is to set up a "heartbeat", a periodic call; if the client heartbeat doesn't get to you after, say, 2 periods, you figure it's dead.

安稳善良 2024-11-17 06:38:14

unreferenced() 方法确实有效,但需要 DGC 时间间隔才能起作用。这因 JDK 版本而异。上次看的时候是十分钟。有系统属性可以对此进行控制,请参阅 Java RMI 主页。该工具将通过特定于客户端的 RemoteSession 对象来使用,该对象实现客户端所需的 API、跟踪客户端状态并实现 Unreferenced 作为故障保护,以防止客户端无法通过其 API 保持自身活动或注销。

除了跟踪客户端上次调用您的时间之外,没有其他可靠的方法来检测客户端崩溃,除非您有客户端回调,在这种情况下,服务器可以尝试调用客户端。然而,这些被广泛视为安全风险,并且在没有特殊处理的情况下通常无法通过防火墙。

The unreferenced() method does indeed work, but it takes the DGC time interval before it does so. This varies with JDK version. Last time I looked it was ten minutes. There are system properties to control this, see the Java RMI Home Page. This facility would be used via a client-specific RemoteSession object that implements the API the client needs, keeps track of client state, and implements Unreferenced as a failsafe against the client failing to keep itself alive or logout via its API.

There is no other reliable way to detect a client crash other than keeping track of when they last called you, unless you have client-side callbacks, in which case the server can try calling the client. However these are widely viewed as a security risk and generally don't work through firewalls without special treatment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文