Java 为 JAX-RS 2.0 Client API 设置请求超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22672664/
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-08-13 17:13:22  来源:igfitidea点击:

Setting request timeout for JAX-RS 2.0 Client API

javarestjax-rs

提问by sarabdeep singh

I have written simple REST web service client class which uses the JAX-RS 2.0 client API to make REST requests. I am trying to figure out how to set a request timeout for each invocation. Here is the code for a request:

我编写了简单的 REST web 服务客户端类,它使用 JAX-RS 2.0 客户端 API 来发出 REST 请求。我想弄清楚如何为每次调用设置请求超时。这是请求的代码:

Client client = ClientBuilder.newBuilder().build();
WebTarget resourceTarget = client.target(restServiceUrl)
        .path("{regsysID}/{appointmentID}/")
        .resolveTemplate("regsysID", regSysId)
        .resolveTemplate("appointmentID", apptId);

Invocation invocation = resourceTarget.request(MediaType.APPLICATION_JSON).buildPut(null);
String createSessionJson = invocation.invoke(String.class);

采纳答案by Jeroen

You can do this by creating a ClientConfig first and providing it as an argument when creating the new client.

您可以通过首先创建一个 ClientConfig 并在创建新客户端时将其作为参数提供来做到这一点。

import org.glassfish.jersey.client.ClientProperties;

ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);

回答by codylerum

With Resteasy this can be accomplished by building your Client as such.

使用 Resteasy,这可以通过构建您的客户端来完成。

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

I have not seen a list of standard configuration properties you could set via ClientBuilder.newClient(Configuration configuration)which would be needed to make this portable.

我还没有看到您可以设置的标准配置属性列表,这些属性ClientBuilder.newClient(Configuration configuration)是使这个可移植所需的。

回答by Maxim Karavaev

First, you have to add relevant dependencies (here is for the WildFly 10.1):

首先,您必须添加相关的依赖项(这里是 WildFly 10.1):

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.14.Final</version>
    <scope>provided</scope>
</dependency>

Next - create a normal Apache HttpClient and push it the RestEasy Enginge with overriding one method, which causes the problem:

接下来 - 创建一个普通的 Apache HttpClient 并使用覆盖一个方法将其推送到 RestEasy Enginge,这会导致问题:

// create here a normal Apache HttpClient with all parameters, that you need
HttpClient httpClient = createHttpClient(connectTimeout,
                                         socketTimeout,
                                         connectionRequestTimeout,
                                         maxTotalHTTPConnections);
// Deprecated Apache classes cleanup https://issues.jboss.org/browse/RESTEASY-1357
// Client Framework not honoring connection timeouts Apache Client 4.3 https://issues.jboss.org/browse/RESTEASY-975
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient) {
        @Override
        protected void loadHttpMethod(ClientInvocation request, HttpRequestBase httpMethod) throws Exception {
            super.loadHttpMethod(request, httpMethod);
            httpMethod.setParams(new BasicHttpParams());
        }
    };

return new ResteasyClientBuilder().httpEngine(engine).build();

Have a look at https://issues.jboss.org/browse/RESTEASY-975Seems, that the problem was just resolved in the version 3.1.0.Final.

看看https://issues.jboss.org/browse/RESTEASY-975看来,这个问题刚刚在 3.1.0.Final 版本中解决了。

回答by Pravat

Note: this is a new method available on JAX-RS 2.1

注意:这是 JAX-RS 2.1 上可用的新方法

This is a very old post but the below code will work for both jersey and resteasy.

这是一篇很老的帖子,但下面的代码适用于 jersey 和 resteasy。

ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder.connectTimeout(10, TimeUnit.SECONDS);
clientBuilder.readTimeout(12, TimeUnit.SECONDS);
Client client = clientBuilder.build();

回答by dliber

For people stuck with older JAX-RS 2.0 API and old Resteasy implementation, you may use this method:

对于那些坚持使用较旧的 JAX-RS 2.0 API 和旧的 Resteasy 实现的人,您可以使用以下方法:

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

Despite the name, socketTimeout stands for "read timeout", since by the docs, it stands for "The timeout for waiting for data".

尽管名称如此,socketTimeout 代表“读取超时”,因为根据文档,它代表“等待数据的超时”。

回答by Nagaraju Beesu

If you are using Jersey 2.x Here it is the simple solution it's work for me

如果您使用的是 Jersey 2.x 这是对我有用的简单解决方案

import com.jclient.JClient;

Client c = Client.create();
WebResource webResource = c.resource("requestUrl");
c.setConnectTimeout(yourMins*60*1000);