I am using HttpClient 4.5.10
and below is the code snippet that is used to make a GET
request:
public String executeRequest(HttpRequestBase request, Header... headers) {
try (CloseableHttpResponse response = execute(request, headers)) {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new RuntimeException("Failed to execute request: " + request.toString(), e);
}
}
I use lsof
to monitor connections and this results in a new connection in ESTABLISHED
state. Once the request gets executed, the connection moves into CLOSE_WAIT
state but doesn't go away. Subsequent calls keep adding more connections to the list.
The question is, how do I make sure that the connection gets released back to pool (I use PoolingHttpClientConnectionManager
)? Do I need to explicit close the request as well, e.g.:
public String executeRequest(HttpRequestBase request, Header... headers) {
try (CloseableHttpResponse response = execute(request, headers)) {
return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new RuntimeException("Failed to execute request: " + request.toString(), e);
} finally {
request.releaseConnection();
}
}
Update
I have applied the above approach (i.e. request.releaseConnection()
). However, I still see the open connections usin lsof
. Looks like the connections are still there.
Is there anything else I need to do to explicitly close the connections?