javax.ws.rs.client.Client 如何配置readTimeOut?

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

javax.ws.rs.client.Client how to configure readTimeOut?

javaweb-servicesrest

提问by DrHyper

Going from com.sun.jersey.api.client.Clientto javax.ws.rs.client.Clienthow do I configure Client?

com.sun.jersey.api.client.Clientjavax.ws.rs.client.Client如何配置客户端?

FROM:

从:

import com.sun.jersey.api.client.Client;

Client client = Client.create();
client.setReadTimeout(1000 * 60 * 20);
client.setConnectTimeout(1000 * 20);
webResource = client.resource("someWhereOverTheRainbow");
..etc.

TO:

到:

import javax.ws.rs.client.*;

Client client = ClientBuilder.newClient();
// **now what?** client.getConfiguration().getProperties().put("isThisTheWayToDoIt", 1000 * 60 * 2);

WebTarget target = client.target("someWhereOverTheRainbow");
..etc.

I am using javax.ws.rs-api-2.0.jar

我正在使用javax.ws.rs-api-2.0.jar

回答by longhua

I assume you are using jax-rs-ri. For this, you can use ClientProperties.CONNECT_TIMEOUTand ClientProperties.READ_TIMEOUT.

我假设您正在使用 jax-rs-ri。为此,您可以使用ClientProperties.CONNECT_TIMEOUTClientProperties.READ_TIMEOUT

Example:

例子:

ClientConfig configuration = new ClientConfig();
configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration = configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
WebTarget target = client.target(
        "http://developer.github.com/v3/");
String content = target.request().get(String.class);
System.out.println(content);

EDIT:

编辑:

I read the API document for ClientConfig.property. And @Gili is right.

我阅读了ClientConfig.property的 API 文档。@Gili 是对的。