I am using Spring RestTemplate to make a HTTP Calls to my RestService. I am
using spring framework 3.1.1 version of RestTemplate. I cannot upgrade this
since that's what we are using in our company.
Now machine which is running my code (which uses RestTemplate) to make call
to my RestService, I see lot of TIME_WAIT connections opened on that
particular machine?
- Is there any reason why it happens with RestTemplate or the way I am
using it?
- Also, what is the connection between TIME_WAIT and HTTP Client calls to
my RestService? I am not able to understand this as well. Does HTTP Calls
has any significance on TIME_WAIT? There might be some theoretical
explanation for this I guess.
Below is how I am using RestTemplate in my code base:
public class DataClient implements Client {
private final RestTemplate restTemplate = new RestTemplate();
private ExecutorService executor = Executors.newFixedThreadPool(10);
// for synchronous call
@Override
public String getSyncData(DataKey key) {
String response = null;
Future<String> handler = null;
try {
handler = getAsyncData(key);
response = handler.get(key.getTimeout(),
TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
// log an exception
handler.cancel(true);
} catch (Exception ex) {
// log an exception
}
return response;
}
// for asynchronous call
@Override
public Future<String> getAsyncData(DataKey key) {
Future<String> future = null;
try {
Task task = new Task(key, restTemplate);
future = executor.submit(task);
} catch (Exception ex) {
// log an exception
}
return future;
}
}
And below is my simple Task class
class Task implements Callable<String> {
private final RestTemplate restTemplate;
private final DataKey key;
public Task(DataKey key, RestTemplate restTemplate) {
this.key = key;
this.restTemplate = restTemplate;
}
public String call() throws Exception {
ResponseEntity<String> response = null;
String url = "some_url_created_by_using_key";
// handling all try catch here
response = restTemplate.exchange(url, HttpMethod.GET, null,
String.class);
return response.getBody();
}
}
And here is netstat details on my box which is running my code to make HTTP
Calls to my RestService. I am always seeing very high number of TIME_WAIT
connections.
72 ESTABLISHED
2 FIN_WAIT1
17 LISTEN
3405 TIME_WAIT