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!


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

This proprietary time out setting and a crude way to add robustnes show JEE at times to be very much not "Enterprise" spec. Robustness and the abstraction of this to a common API should have been one of the top features for all technology that is based around remote communications. Not having this in the spec allows inexperienced developers to write a nice rest client and think the world is great until you get a failure in that service in live and you system comes failing down. Anyone not using something like Hystrix (or similar patterns) for making remote calls more robust is asking for trouble.

Posted by Paul Grove on December 11, 2014 at 09:49 PM CET #

Hello ,

I was getting the following exception whenever i hit my restapi ,

javax.ws.rs.ProcessingException: java.net.SocketTimeoutException: connect timed out
Caused by: java.net.SocketTimeoutException: connect timed out

Caused by: java.net.SocketTimeoutException: connect timed out
javax.ws.rs.ProcessingException: java.net.SocketTimeoutException: connect timed out

I have added the following in my code as per your solution ,

Client client = ClientBuilder.newClient();

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

Still i am getting same exception , can you please help with this ? please expecting your reply

Posted by Chandrakanth N B on November 16, 2016 at 10:02 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License