Monday, June 13, 2016

Java URL Connection Timeout (http,ftp,scp etc) setting in system level

Sometimes we face issues like the thread , which trying to connect the url ,hangs for infinite time. The connection may be over http,ftp or scp protocol.But really it is painful to debug the issue.But there are some system level configuration provided by java, so that we can  we can solve this problem.

So lets start with some simple definitions.

ConnectionTimeOut:


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 connection it is the timeout when establishing the connection to ftp servers.For scp connection it is the time out for establishing the scp connection.

The property provided by sun for connectionTimeOut is

sun.net.client.defaultConnectTimeout (default: -1)

Note that here -1 means infinite timeout.

ReadTimeOut:


The timeout (in milliseconds) when reading from input stream when a connection is established to a resource.It is the timeout between two consecutive packets from the socket.

The property provided by sun for readTimeOut is

sun.net.client.defaultReadTimeout (default: -1)


Retry If Post Fails:


It determines if an unsuccessful HTTP POST request will be automatically resent to the server. Unsuccessful post means  in this case  the server did not send a valid HTTP response or an IOException occurred.And it defaults to true in system level.

The property provided by sun for retry post fails is  
sun.net.http.retryPost (default: true)
 
 
 We can use it in system level to configure a global connection time out or  read time out setting.We can give the time out in the client , which is used to make the http call.For example apache http client.But it is important to note  that , these are sun implementation specific properties and these properties may not be supported in future releases.

We can set the property like

-Dsun.net.client.defaultReadTimeout =timeinmiliseconds
-Dsun.net.client.defaultReadTimeout =timeinmiliseconds
-Dsun.net.http.retryPost =false

For more details please follow the oracle doc.

No comments:

Post a Comment