Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 875
DrHyper Avatar asked Oct 23 '13 11:10

DrHyper


1 Answers

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.

like image 73
longhua Avatar answered Sep 26 '22 06:09

longhua