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

偶尔善良 提交于 2021-02-06 09:58:24

问题


Going from com.sun.jersey.api.client.Client to javax.ws.rs.client.Client how do I configure 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


回答1:


I assume you are using jax-rs-ri. For this, you can use ClientProperties.CONNECT_TIMEOUT and ClientProperties.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.



来源:https://stackoverflow.com/questions/19539970/javax-ws-rs-client-client-how-to-configure-readtimeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!