SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate?
By default RestTemplate uses SimpleClientHttpRequestFactory
which depends on default configuration of HttpURLConnection
.
You can configure them by using below attributes:
-Dsun.net.client.defaultConnectTimeout=TimeoutInMiliSec
-Dsun.net.client.defaultReadTimeout=TimeoutInMiliSec
If you want to use HttpComponentsClientHttpRequestFactory
- it has a connection pooling configuration which SimpleClientHttpRequestFactory
does not have.
A sample code for using HttpComponentsClientHttpRequestFactory
:
public class TimeoutThreadExample {
private ExecutorService executor = Executors.newFixedThreadPool(10);
private static final RestTemplate restTemplate = createRestTemplate();
private static RestTemplate createRestTemplate(){
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(READ_TIME_OUT);
requestFactory.setConnectTimeout(CONNECTION_TIME_OUT);
return new RestTemplate(requestFactory);
}
public String getData() {
Future<String> future = executor.submit(new Task(restTemplate));
String response = null;
try {
response = future.get(500, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return response;
}
}