你能在x秒后让rpc调用超时吗?

发布于 2024-09-14 10:16:48 字数 139 浏览 2 评论 0原文

如果我有一个进行 RPC 调用的服务(比如 Web 服务、restful 服务,或者只是从 url 中抓取数据等),我是否可以让它在 5 秒后超时?

我不希望它在远程服务关闭时挂起然后崩溃,而是尝试 x 秒,如果它关闭,然后继续下一个远程服务调用。

If I have a service that makes RPC calls (say to web services, or restful services, or just to scrape data from a url, etc), is it possible for me to have it timeout after 5 seconds?

I don't want it to hang and then crash if the remote service is down, rather try for x seconds, if its down, then just carry on to the next remote service call.

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

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

发布评论

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

评论(5

我也只是我 2024-09-21 10:16:48

如果它是一个独立的应用程序,那么您可以使用操作系统来安排它(例如 Windows 中的任务计划程序)。如果不是独立的,那么可以将其做成Java Task,使用Java的Timer来调度。

看看 Java.util.Timer
这是一个例子
http://www.java2s.com/Code/Java/Development-类/UsejavautilTimertoscheduleatasktoexecuteonce5secondshavepassed.htm

If it's a stand-alone app then you can use the OS to schedule it (for example Task Scheduler in Windows). If it's not a stand-alone, then you can make it into a Java Task and use Java's Timer to schedule it.

Have a look a Java.util.Timer
and here is an example
http://www.java2s.com/Code/Java/Development-Class/UsejavautilTimertoscheduleatasktoexecuteonce5secondshavepassed.htm

俯瞰星空 2024-09-21 10:16:48

RPC 的客户端库通常具有可选的超时参数。如果请求未在要求的时间内完成,客户端库将简单地关闭连接。这些协议本身可能没有关于超时的内容。

你的 RPC 课程学得怎么样?你正在使用什么库?

Client libraries for RPC usually have optional timeout parameter available. If the request is not finished in required time, the client library simply closes the connection. There is probably nothing about timeouts in those protocols themselves.

How are you doing your RPC class? What libs you are using?

递刀给你 2024-09-21 10:16:48

您使用什么协议?是http还是RMI?最好的方法是在连接通道(如 Socket)上实现超时。如果这是您想做的事情,请查看 http://download.oracle.com/javase/1.5.0/docs/guide/net/properties.html

What protocol are you using? It is http or RMI? The best way will be to implement timeout on the connection channel (like Socket). If this is something you want to do take a look at http://download.oracle.com/javase/1.5.0/docs/guide/net/properties.html

盗心人 2024-09-21 10:16:48

如果您使用 Apache HTTP 组件,请执行以下操作:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);

If you use Apache HTTP Components then do this:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文