engma
engma

Reputation: 1959

Jersey Client gives read timeout but Apache HTTP Client connects normally

I was using Jersey 2.25 client with Jackson, I configured everything correctly in Jersey, it worked normally on my development machine when I ran it in a test class, but Jersey client could never connect to a certain host that we have when deployed on our STG environment and always throws a read timeout exception.

I also know that the problem is not in our environment because I can connect using curl

But when switched to HTTPClient it worked normally.

This is how we created our Jersey Client:

Client client = ClientBuilder.newBuilder()
            .register(JacksonFeature.class)
            .property(ClientProperties.CONNECT_TIMEOUT,5000)
            .property(ClientProperties.READ_TIMEOUT,15000)
            .build();

The only difference here is the flow of the app, and also the major change that happens in the flow that could affect the connection is that somewhere before calling the Jersey client another class sets a proxy in the system config:

System.setProperty("http.proxyHost",strProxyHost);
System.setProperty("http.proxyPort",strProxyPort);
System.setProperty("https.proxyHost",strProxyHost);
System.setProperty("https.proxyPort",strProxyPort);

However we can establish a connection normally using HTTPClient:

 HttpConnectionManagerParams params = new HttpConnectionManagerParams();
 params.setConnectionTimeout(5000);
 params.setSoTimeout(10000);
 HttpConnectionManager manager = new SimpleHttpConnectionManager();
 manager.setParams(params);
 HttpClient httpClient = new HttpClient(manager);

We are using HTTPClient 3 because part of this app is legacy and we cannot update the version, but it works normally.

What could be causing this connection problem with Jersey? is there something global that Jersey reads when it's trying to connect?

Upvotes: 1

Views: 1298

Answers (1)

Vikas Sachdeva
Vikas Sachdeva

Reputation: 5813

Jersey by default uses HttpURLConnection and HttpURLConnection uses following global settings for proxy configuration -

System.setProperty("http.proxyHost",strProxyHost);
System.setProperty("http.proxyPort",strProxyPort);
System.setProperty("https.proxyHost",strProxyHost);
System.setProperty("https.proxyPort",strProxyPort);

It means if these system variables are set, Jersey will send all the requests through this configured proxy. Check Details here

However, Apache HttpClient does not follow these settings. For using proxy in Apache HttpClient, you have to use HostConfiguration class. Check details here

So, now to your problem, It looks that your STG environment is not able to connect to specified proxy but able to connect with the service directly.

So, while using Jersey, client is not able to connect to proxy and hence ReadTimeoutException is occurring. Since, you haven't configured HttpClient for using any proxy, it is able to connect with the service directly.

Upvotes: 1

Related Questions