59

In jersey 1 we had a function setConnectTimeout in the class com.sun.jersey.api.client.Client.

In jersey 2 the javax.ws.rs.client.Client class is used where this function is missing.

How to set connection timeout and read timeout in jersey 2.x?

Amit
  • 30,756
  • 6
  • 57
  • 88
David Michael Gang
  • 7,107
  • 8
  • 53
  • 98

3 Answers3

79

The code below works for me in Jersey 2.3.1 (inspiration found here: https://stackoverflow.com/a/19541931/1617124)

public static void main(String[] args) {
    Client client = ClientBuilder.newClient();

    client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
    client.property(ClientProperties.READ_TIMEOUT,    1000);

    WebTarget target = client.target("http://1.2.3.4:8080");

    try {
        String responseMsg = target.path("application.wadl").request().get(String.class);
        System.out.println("responseMsg: " + responseMsg);
    } catch (ProcessingException pe) {
        pe.printStackTrace();
    }
}
Community
  • 1
  • 1
Lars
  • 1,311
  • 1
  • 11
  • 11
  • 3
    I doubt that this works. .property(...) returns a client instance (builder pattern). The settings will not be used when you invoke the .target(). – mkuff Jul 11 '14 at 09:17
  • 6
    Actually it works. The builder pattern does not say another instance should be created. Just look at the source code, the return value is the actual client (just to make easier for us to do subsequent calls). – Mateus Viccari Jan 10 '15 at 10:26
  • just wanted to know What will happen if the timeout happens, Will we receive gateway timeout (504) or will it throw an exception? – Hardik Patel May 18 '18 at 12:39
  • @HardikPatel If the connect attempt times out it will throw an exception. 'Gateway timeout 504' comes from the gateway, over the connection, so it can only happen if the connect succeeded. – user207421 Jul 04 '18 at 02:48
40

You may also specify a timeout per request :

public static void main(String[] args) {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://1.2.3.4:8080");

    // default timeout value for all requests
    client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
    client.property(ClientProperties.READ_TIMEOUT,    1000);

    try {
        Invocation.Builder request = target.request();

        // overriden timeout value for this request
        request.property(ClientProperties.CONNECT_TIMEOUT, 500);
        request.property(ClientProperties.READ_TIMEOUT, 500);

        String responseMsg = request.get(String.class);
        System.out.println("responseMsg: " + responseMsg);
    } catch (ProcessingException pe) {
        pe.printStackTrace();
    }
}
Siggen
  • 2,147
  • 1
  • 19
  • 19
  • Im not sure, but I think you will need to reassign the client back to itself like this: `client = client.property(...)`, because the property method returns updated configurable instance. Same with the `request`. – W0lfw00ds Oct 15 '20 at 10:54
1

Starting from jersey 2.26 (which uses JAX-RS 2.1) there are new methods for that:

ClientBuilder builder = ClientBuilder.newBuilder()
        .connectTimeout(5000, TimeUnit.MILLISECONDS)
        .readTimeout(5000, TimeUnit.MILLISECONDS);
        //some more calls if necesary, e.g.
        //.register(LoggingFilter.class);
            
        Client restClient = builder.build();
Lonzak
  • 9,334
  • 5
  • 57
  • 88