来自客户端的超时 Web 服务调用
我正在使用 RestEasy 客户端调用网络服务。一项要求是,如果调用运行时间超过 5 秒,则中止/超时调用。我如何使用 RestEasy 客户端实现这一目标?我只看到服务器端超时,即如果在一定时间内未完成请求,Rest Easy Web 服务将使请求超时。
I'm calling a webservice using RestEasy Client. One requirement is to abort/timeout the call if it runs for more that 5 seconds. How would I achieve this with RestEasy Client? I have only seen server side timeout, i.e. the Rest Easy websevice will timeout the request if it's not fulfilled within a certain time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
RESTEasy 客户端通常使用 Apache HttpClient 来处理网络对话。
您可以使用自己的自定义超时参数覆盖 HttpClient 属性:
第一个参数允许您指定建立初始连接的超时,第二个参数允许您指定套接字在没有发送数据时等待的最长时间。
您可以使用修改后的 HttpClient 来构建 ClientExecutor:
它又可以用来构建 ClientRequest 对象。或者,如果您使用 RESTEasy 的 Spring 配置,则可以将其注入 RestClientProxyFactoryBean 中。
它与绝对 5 秒超时并不完全相同,但根据您想要实现的目标,调整这两个属性通常可以满足要求。
A RESTEasy client typically uses Apache HttpClient to handle the network conversation.
You can override the HttpClient properties with your own custom timeout parameters:
The first param allows you to specify timeout establishing the initial connection and the second allows you to specify the maximum period of time in which a socket will wait while no data is sent.
You can use the modified HttpClient to build your ClientExecutor:
Which can be used in turn to build a ClientRequest object. Or you can inject it into a RestClientProxyFactoryBean if you are using a Spring configuration for RESTEasy.
It's not exactly the same as an absolute 5 second timeout, but depending on what you are trying to accomplish, tweaking these two properties will usually fill the bill.
如果您更喜欢构建器模式,请执行以下操作:
取自此处: http://blog.eisele.net/2014/12/setting-timeout-for-jax-rs-20-resteasy-client.html
If you prefer the builder pattern here is how you do it:
taken from here: http://blog.eisele.net/2014/12/setting-timeout-for-jax-rs-20-resteasy-client.html
Carter Page 的答案对于 Apache HttpClient 版本 >= 4.0 是正确的。
对于早期版本的 HttpClient(例如 3.1),代码略有不同:
The answer by Carter Page is correct for Apache HttpClient version >= 4.0.
For earlier versions of HttpClient (e.g. 3.1) the code is slightly different:
如果您使用带有 spring 集成的 Resteasy 客户端框架 (文档),以下是设置超时值的方法:
If you are using resteasy client framework with spring integration (documentation), the following is the way to set timeout values:
鉴于 builtConnectionTimeout 和 socketTimeout 均已弃用。
redhat 对 jboss v7.3 的解释网站:
这对我来说适用于 RestEASY 3.12.1.Final:
Given that both establishConnectionTimeout and socketTimeout are deprecated.
With this explanation on jboss v7.3 by redhat website :
This worked for me with RestEASY 3.12.1.Final: