Connection Timeout In Java HTTPClient, RestTemplate and URLConnection


Connection timeout is the time for which an HTTP client or Socket client waits, if the server doesn’t respond in that time or not found then the client closes the connection. HTTP also uses sockets internally. Connection timeout is different from the Connection Request timeout or connection read timeout.

In many situations, the client doesn’t want to wait for a long time in that case a connection time out or Request connection timeout or both can be set.

Here are an example of the most used Java HTTP clients.

HttpURLConnection Timeout Example

Here is an example of setting connection time out and connection read time out.

try {
	final URL url = new URL("http://134.234.3.17");

	final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

	HttpURLConnection.setFollowRedirects(false);
	// Sets a specified timeout value, in milliseconds, to be used when opening a
	// communications link to the resource reference by this URLConnection. If the
	// timeout expires before the connection can be established,
	// a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted
	// as an infinite timeout.
	// Here 5 second time out is set
	urlConn.setConnectTimeout(5 * 1000);
	urlConn.setReadTimeout(5 * 1000);
	urlConn.setRequestMethod("GET");
	urlConn.setRequestProperty("User-Agent",
			"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
	urlConn.connect();
} catch (final Exception e) {
	e.printStackTrace();
}

Apache HttpClient Timeout example

Here is an example of setting Connection time out and Request Connection time out. Not Connection Read timeout is the same as Request connection timeout.

try {
	final int timeout = 5000;// 5 seconds
	final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout)
			.setConnectionRequestTimeout(timeout).build();
	final HttpClientBuilder builder = HttpClientBuilder.create();
	builder.setDefaultRequestConfig(config);
	final HttpClient client = builder.build();

	final HttpGet request = new HttpGet("http://134.234.3.17"); // GET Request

	final HttpResponse response = client.execute(request);
} catch (final Exception e) {
	e.printStackTrace();
}

Spring RestTemplate Timeout Example

Here is an example of the RestTemplate timeouts. It will set 5 seconds connection timeout and 5 seconds of reading time out.

RestTemplate template =  new RestTemplate();
HttpComponentsClientHttpRequestFactory rf =
	    (HttpComponentsClientHttpRequestFactory) template.getRequestFactory();
	rf.setReadTimeout(5 * 1000);
	rf.setConnectTimeout(5 * 1000);

You can define it as given below in Spring config

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) 
{
    return restTemplateBuilder
       .setConnectTimeout(...)
       .setReadTimeout(...)
       .build();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.