To whom it may concern, The new HttpClient looks promising and exciting. I’m currently writing a shared Java module or driver component that will be distributed via Maven and shared among many teams within my organization.
I have a concern regarding Http clients in general and hope you can provide some comments or general advice. The driver utility I’m writing has a feature which allows an interested party to request an InputStream which transmits bytes of information over the network both small and very large (think large binary file). It looks like this: InputStream m = client.payload(UUID id); The concrete implementation of the InputStream, shown in the code above, comes from the Apache HttpClient library, which is irrelevant. Just note that the bytes coming from the InputStream are originating over the network from a HttpEntity (body) provided by the Apache HttpClient library. Good developers and aware people usually know to always close an InputStream to cleanup and free resources and avoid a memory leaks. But unfortunately not everyone does this. Currently, if users of the Shared utility I’m writing, forget the close the provided InputStream, a connection leaks which eventually exhausts all available connections inside a pool—leaving no resources to establish new connections (different then a program using all file descriptors and is instead is a maintained connection pool in Java). Currently, I’m wrapping the original InputStream provided by the Http Client library with my own concrete class implementation of an InputStream that essentially keeps track of them internally. If the user invokes .close() on my wrapping InputStream implementation, I remove my reference (allowing garage collection to cleanup the objects) and I propagate that .close() to the original InputStream provided by the HttpEntity, which then frees up the connection to be further utilized. Otherwise, If a client forgets to invoke .close() on the InputStream, I have a background task that periodically reviews the list of InputStream’s I’ve noted and tracked internally and evicts them cleaning up the held connection and releasing it to be utilized further. Here is the question: Is this checking and reviewing necessary? Should I have written a background task that reviews all InputStream’s created by the HttpClient and calls .close() if the user forgets. The intent here is to free up these resources. Without tracking and reviewing the InputsStream objects created by the HttpClient, If the user forgets to call .close() on the InputStream objects they request, it eventually affects the stability of the core library due to the fact it cannot get new Http connections to perform other work. What’s your take? As always, Best regard, Joshua