JDK-8267140 : Support closing the HttpClient by making it auto-closable
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 16
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-05-13
  • Updated: 2025-08-13
  • Resolved: 2023-04-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 21
21 b18Fixed
Related Reports
CSR :  
Causes :  
Relates :  
Sub Tasks
JDK-8305196 :  
Description
A DESCRIPTION OF THE PROBLEM :
The documentation for java.net.http.HttpClient does not explain if / when underlying resources are closed. This is causing confusion (see https://stackoverflow.com/q/53919721 ) because users are normally used to closing the client when they are done with their HTTP requests, see for example:
- Apache HttpClient: https://hc.apache.org/httpcomponents-client-5.1.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/classic/CloseableHttpClient.html
- Ktor: https://api.ktor.io/ktor-client/ktor-client-core/io.ktor.client/-http-client/close.html

Therefore it would be good to explain in the HttpClient documentation when resources are closed.
Alternatively / additionally (depending on how reliable the current implementation is), it might be good to provide a method to close the HttpClient manually (e.g. by implementing Closeable).



Comments
Changeset: 6580c4e6 Author: Daniel Fuchs <dfuchs@openjdk.org> Date: 2023-04-06 10:05:20 +0000 URL: https://git.openjdk.org/jdk/commit/6580c4e6311b6f87cec7c5c5537351bec9b703db
06-04-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/13019 Date: 2023-03-14 14:18:49 +0000
14-03-2023

The HttpClient will terminate and release all its resources when: 1. all referenced to the HttpClient are released 2. all operations are finished (all request responses have been received and relayed to calling code) Adding a close() method may be envisaged but the semantics and behaviour of closing a client when it is still in use and some operations are still in flight must be carefully analyzed, in particular WRT what should happen to the requests that may not have completed yet. Close might be the wrong primitive here: stop/shutdown might be preferable. The implementation of a close/stop/shutdown method that behaves correctly is not trivial.
14-05-2021

Moved to JDK to discuss the more clarifications of closing underlying resources are closed of HttpClient.
14-05-2021