adam bien's blog

Setting Timeout For The JAX-RS 2.0 / Jersey Client 📎

Timeouts are crucial for robustness of the communication between reasonable WARs (also known as micro services).

Timeout settings are not standardized and have to be passed as "proprietary" properties of the JAX-RS client:


import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.client.ClientProperties;

        Client client = ClientBuilder.newClient();
        client.property(ClientProperties.CONNECT_TIMEOUT, 100);
        client.property(ClientProperties.READ_TIMEOUT, 10);


The "provided" dependency highlighted above can be omitted by using the String representation of the constants:

        client.property("jersey.config.client.connectTimeout", 100);
        client.property("jersey.config.client.readTimeout", 10);


Timeouts will cause: java.net.SocketTimeoutException: Read timed out exception which can be easily handled. A few years ago we would talk about exception handling and robustness, in the age of micro services we can call it now "resilience" :-).

See you at Java EE Workshops at Munich Airport, Terminal 2 or at a virtual, dedicated workshop!