We deploy java spring boot applications on Apache tomcat 8 and we try to write a proper java code.
But we have an old, low traffic and not maintainable code which has un-closed CloseableHttpClient
and CloseableHttpResponse
CloseableHttpClient httpClient = HttpClients.custom().build();
try {
CloseableHttpResponse response = httpClient.execute(target, post);
...(no finally clause)
According to apache of course both should be closed although in quickstart demo httpclient isn't closed:
CloseableHttpClient
CloseableHttpClient httpclient = HttpClients.createDefault();
...
Code is working on production without any known issue.
Can I know the real effect of not closing such resources in order to know how critical it's to fix?
Does one or more of the following may handle/close those http (using code or configuration):
Tomcat/spring boot or different code which uses http client in a proper way ?
Depends, normally you keep your httpclient around until you shut down the server. Since you are using spring boot, just inject it as a spring bean. That's also a good opportunity to configure things like timeouts, some of which default to dangerous values (like 0, which means infinite) and align those with whatever load-balancer timeouts you have.
In that case, closing your httpclient won't really matter because whatever resources it has will be released on application exit.
But if you are creating it on a need to have basis (why?) you might want to close it. Depending on how you set it up, it might be running some executors, have a pool of connections, etc. Not cleaning that up and then repeatedly creating more httpclients, could cause you to run out of file handles.
The thing with that kind of bug is that you will know you have a problem only after you run out of file handles, which depending on your server traffic might be quite a while. I've dealt with servers crashing because of unreleased file handles, hard to debug when it happens. In short, if it has a close method and implements closable, you better have a good reason to not call it in a finally block or in a try with resources.