Java RMI:在客户端代码中实现超时

发布于 2024-10-22 01:29:08 字数 267 浏览 2 评论 0原文

我有一个Java RMI 系统。典型的情况是:客户端调用服务器的方法。 客户端有一个内部计时器,因此如果服务器没有在适当的时间(客户端中指定的时间)完成,那么客户端必须执行其他操作。

因此,客户端必须等待服务器在特定时间内完成其工作,并且如果服务器没有完成则执行其他操作(无论做什么)。 我怎样才能做到这一点?

我不关心连接超时等问题,假设服务器和客户端通过RMI连接,一切都很好,只是服务器的工作可能是计算密集型的并且可能需要一些时间。

多谢!

I have a Java RMI system. The situation is typical: the client invokes a method of the server.
The client has an internal timer, so if the server doesn't finish in due time (the time is specified in the client), then the client must do something else.

So, the client must wait for the server to finish its job for a specific time and in case the server didn't finish do something else (it doesn't matter what). How can I do this?

I don't care about connection timeouts and so on, assume that the server and client are connected through RMI and everything it's fine, only that the server's job can be computationally intensive and can require some time.

thanks a lot!

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

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

发布评论

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

评论(3

意中人 2024-10-29 01:29:08

在另一个线程上进行 RMI 调用。让原始线程等待一定长度的时间来等待 RMI 调用线程的响应。

或者,让服务器 RMI 线程将任务委托给工作线程。如果工作线程响应不够快,则返回调用者。

Make the RMI call on another thread. Have the originating thread wait a certain length of time for a response from the RMI-calling thread.

Alternatively, have the server RMI thread delegate the task to a worker thread. Return to the caller if the worker thread doesn't respond sufficiently quickly.

合久必婚 2024-10-29 01:29:08

一般来说,当您希望 Java 中的操作超时时,您正在谈论一两个同步/异步转换层。我从来没有用 RMI 这样做过,但我想你也会做类似的事情。也许询问本次讨论的参与者:(异步 Java RMI)会很有用。根据( Spring Async RMI Call )中提出的观点,我想说你需要做 使用单独的线程

  1. 调用 RMI 服务;考虑使用执行者服务。
  2. 使用包装器公开它,该包装器调用执行器服务并在有限的时间内阻塞以获取结果;考虑使用期货。

In general, when you want operations to timeout in Java, you are talking about one or two synchronous/asynchronous conversion layers. I've never done this with RMI, but I imagine you do something similar. Perhaps asking the participants of this discussion: ( Asynchronous Java RMI ) will be useful. Based on the points made in ( Spring Async RMI Call ), I would say you need to do the following:

  1. Call the RMI service using separate thread(s); consider using executor service.
  2. Expose this with a wrapper that calls through to the executor service and blocks for a finite amount of time for results; consider using Futures.
最单纯的乌龟 2024-10-29 01:29:08

您需要在每个客户端 JVM 上设置名为(我认为)sun.rmi.transport.tcp.responseTimeout 的未记录属性。值以毫秒为单位。

You need to set the undocumented property called (I think) sun.rmi.transport.tcp.responseTimeout at each client JVM. Value in milliseconds.

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