java 来自客户端的超时 Web 服务调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5965917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:43:44  来源:igfitidea点击:

Timeout webservice call from client side

javaresttimeoutresteasy

提问by n002213f

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 Client 调用网络服务。一项要求是,如果呼叫运行超过 5 秒,则中止/超时呼叫。我将如何使用 RestEasy Client 实现这一目标?我只见过服务器端超时,即如果在特定时间内未完成,Rest Easy websevice 将超时请求。

回答by Carter Page

A RESTEasy client typically uses Apache HttpClient to handle the network conversation.

RESTEasy 客户端通常使用 Apache HttpClient 来处理网络对话。

You can override the HttpClient properties with your own custom timeout parameters:

您可以使用自己的自定义超时参数覆盖 HttpClient 属性:

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.

第一个参数允许您指定建立初始连接的超时时间,第二个参数允许您指定套接字在没有数据发送时等待的最长时间。

You can use the modified HttpClient to build your ClientExecutor:

您可以使用修改后的 HttpClient 来构建您的 ClientExecutor:

ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);

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.

它可以依次用于构建 ClientRequest 对象。或者,如果您使用的是 RESTEasy 的 Spring 配置,则可以将其注入 RestClientProxyFactoryBean。

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.

它与绝对 5 秒超时并不完全相同,但取决于您要完成的任务,调整这两个属性通常会满足要求。

回答by Malik Atalla

If you prefer the builder pattern here is how you do it:

如果您更喜欢这里的构建器模式,您可以这样做:

 Client client = new ResteasyClientBuilder()
            .establishConnectionTimeout(5, TimeUnit.SECONDS)
            .socketTimeout(5, TimeUnit.SECONDS)
            .build();

taken from here: http://blog.eisele.net/2014/12/setting-timeout-for-jax-rs-20-resteasy-client.html

取自这里:http: //blog.eisele.net/2014/12/setting-timeout-for-jax-rs-20-resteasy-client.html

回答by shonky linux user

The answer by Carter Page is correct for Apache HttpClient version >= 4.0.

Carter Page 的回答对于 Apache HttpClient 版本 >= 4.0 是正确的。

For earlier versions of HttpClient (e.g. 3.1) the code is slightly different:

对于早期版本的 HttpClient(例如 3.1),代码略有不同:

HttpClient httpClient = new HttpClient();
HttpConnectionParams params = httpClient.getHttpConnectionManager().getParams();
params.setConnectionTimeout(connectionTimeoutMillis);
params.setSoTimeout(socketTimeoutMillis);

ClientExecutor executor = new ApacheHttpClientExecutor(httpClient);
MyService service = ProxyFactory.create(MyService.class, URL, executor);

回答by Nazar Annagurban

If you are using resteasy client framework with spring integration (documentation), the following is the way to set timeout values:

如果您使用带有 spring 集成的 resteasy 客户端框架(文档),以下是设置超时值的方法:

<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient">
    <property name="params" ref="httpParams"/>
</bean>

<bean id="httpParams" class="org.apache.http.params.BasicHttpParams"/>

<bean id="httpConnectionParams" class="org.apache.http.params.HttpConnectionParamBean">
    <constructor-arg ref="httpParams"/>
    <property name="connectionTimeout" value="10000"/>
    <property name="soTimeout" value="30000"/>
</bean>