17

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

assylias
  • 321,522
  • 82
  • 660
  • 783
DrHyper
  • 355
  • 1
  • 3
  • 12

1 Answers1

26

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.

Martin
  • 2,573
  • 28
  • 22
longhua
  • 4,142
  • 21
  • 28
  • 1
    `ClientConfig` may be immutable. As such, `configuration.property()` is not guaranteed to return `this`. It is your responsibility to assign the result back to `configuration` or you risk the property getting lost. – Gili Nov 15 '14 at 01:36
  • 4
    is ClientConfig and ClientProperties not jersey specific classes? I think question is asking about generic way of setting the connection timeout and read timeout through JAX-RS API – sarabdeep singh Mar 06 '15 at 04:36
  • @sarabdeepsingh it is, I really need JAX-RS api compatible solution – Jdruwe Jun 03 '15 at 13:01
  • 1
    @Gili Where did read that ClientConfig may be immutable? I think they just designed the API this way so you can chain the calls like this: `configuration.property(...).property(...).property(...)...` This is not uncommon. – Puce Feb 19 '16 at 09:33
  • @Puce I don't remember where I read it (I vaguely remember seeing an immutable implementation) but either way the API clearly states that the method returns the updated configuration but does not guarantee that the current object is the one being updated. – Gili Feb 20 '16 at 10:45