Java HttpClient and Load Balancer bad interactions

Working for a very big customer, I found a very nasty interaction between Sun HttpClient (JDK 1.4) and Http  Load Balancers.

In a complex network environment, sometimes you can experience low level TCP/IP comunication errors, because sometimes HttpClient get confused and hangs.

The bad behavior of Sun HttpClient is well known: some guys suggested me to use the Axis Web Client. Anyway you can solve the issue adding these three parameters to the JVM launch line

-Dsun.net.client.defaultConnectTimeout=5000
-Dsun.net.client.defaultReadTimeout=5000
-Dhttp.keepAlive=false

The first two parameters set globally the socket timeout to 5 seconds.
The last parameter forces the JVM to avoid reusing http connections when doing http request.

To be honest, http.keepAlive=false is not always effective and could have huge performance impacts, so be very carful adopting it.

But if you stick on the two sun.net.client.default*  properties (doing some tests) you can solve the issue.

References

From Java Plug-in Control Panel:

[…]
Networking properties description:

sun.net.client.defaultConnectTimeout

sun.net.client.defaultReadTimeout

These properties specify, respectively, the default connect and read timeout values for the protocol handlers used by java.net.URLConnection. The default values set by the protocol handlers is -1, which means there is no timeout set.

sun.net.client.defaultConnectTimeout specifies the timeout (in milliseconds) to establish the connection to the host. For example, for http connections it is the timeout when establishing the connection to the http server. For ftp connections it is the timeout when establishing the connection to ftp servers.

sun.net.client.defaultReadTimeout specifies the timeout (in milliseconds) when reading from an input stream when a connection is established to a resource.

For the official description of these properties, see Networking Properties.

[…]