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

时间:2013-10-23 11:05:59

标签: java web-services rest

com.sun.jersey.api.client.Client 转到 javax.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.

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

1 个答案:

答案 0 :(得分:20)

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

示例:

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);

编辑:

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