来自客户端的超时webservice呼叫 [英] Timeout webservice call from client side
问题描述
我正在使用RestEasy Client调用Web服务。一个要求是,如果呼叫运行超过5秒,则中止/超时。我如何使用RestEasy Client实现这一目标?我只看到服务器端超时,即如果在一定时间内未满足,则Rest Easy websevice将超时请求。
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.
推荐答案
RESTEasy客户端通常使用Apache HttpClient来处理网络会话。
A RESTEasy client typically uses Apache HttpClient to handle the network conversation.
您可以使用自己的自定义超时参数覆盖HttpClient属性:
You can override the HttpClient properties with your own custom timeout parameters:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
第一个参数允许您指定建立初始连接的超时,第二个参数允许您指定最大值在没有数据发送的情况下套接字将等待的时间段。
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.
您可以使用修改后的HttpClient构建ClientExecutor:
You can use the modified HttpClient to build your ClientExecutor:
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);
可以依次使用它来构建ClientRequest对象。或者你可以将它注入到RestClientProxyFactoryBean中,如果你使用REST配置的Spring配置。
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.
它与绝对的5秒超时不完全相同,但取决于你正在努力完成,调整这两个属性通常会填补这笔费用。
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.
这篇关于来自客户端的超时webservice呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!