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

发布于 2024-11-06 03:48:51 字数 136 浏览 5 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(5

謌踐踏愛綪 2024-11-13 03:48:51

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

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

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

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

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

ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);

它又可以用来构建 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:

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:

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.

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.

神爱温柔 2024-11-13 03:48:51

如果您更喜欢构建器模式,请执行以下操作:

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

取自此处: 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:

 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

甚是思念 2024-11-13 03:48:51

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

对于早期版本的 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);

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:

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);
生死何惧 2024-11-13 03:48:51

如果您使用带有 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>

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

<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>
坠似风落 2024-11-13 03:48:51

鉴于 builtConnectionTimeoutsocketTimeout 均已弃用。

redhat 对 jboss v7.3 的解释网站

以下符合 ClientBuilder 规范的方法取代了某些已弃用的 RESTEasy 方法:

  • connectTimeout 方法取代了 assessmentConnectionTimeout 方法。

    • connectTimeout 方法确定客户端在建立新的服务器连接时必须等待多长时间。
  • readTimeout 方法取代了 socketTimeout 方法。

    • readTimeout 方法确定客户端必须等待服务器响应的时间。

这对我来说适用于 RestEASY 3.12.1.Final

    private Client clientBuilder() {
        return new ResteasyClientBuilder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .build();
    }

Given that both establishConnectionTimeout and socketTimeout are deprecated.

With this explanation on jboss v7.3 by redhat website :

The following ClientBuilder specification-compliant methods replace certain deprecated RESTEasy methods:

  • The connectTimeout method replaces the establishConnectionTimeout method.

    • The connectTimeout method determines how long the client must wait when making a new server connection.
  • The readTimeout method replaces the socketTimeout method.

    • The readTimeout method determines how long the client must wait for a response from the server.

This worked for me with RestEASY 3.12.1.Final:

    private Client clientBuilder() {
        return new ResteasyClientBuilder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .build();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文