JDK-8305196 : Release Note: The `java.net.http.HttpClient` Is Now AutoCloseable
  • Type: Sub-task
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 21
  • Priority: P4
  • Status: Resolved
  • Resolution: Delivered
  • Submitted: 2023-03-29
  • Updated: 2023-07-28
  • 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
21Resolved
Description
The following methods have been added to the API:

 - `void close()`: closes the client gracefully, waiting for submitted requests to complete.
 - `void shutdown()`: initiates a graceful shutdown, then returns immediately without waiting for the client to terminate.
 - `void shutdownNow()`: initiates an immediate shutdown, trying to interrupt active operations, and returns immediately without waiting for the client to terminate.
 - `boolean awaitTermination(Duration duration)`, waits for the client to terminate, within the given duration; returns true if the client is terminated, false otherwise.
 - `boolean isTerminated()`: returns true if the client is terminated.

The instances returned by `HttpClient.newHttpClient()`, and the instances built from `HttpClient.newBuilder()`, provide a best effort implementation for these methods. They allow the reclamation of resources allocated by the `HttpClient` early, without waiting for its garbage collection.

Note that an `HttpClient` instance typically manages its own pools of connections, which it may then reuse when necessary. Connection pools are typically not shared between `HttpClient` instances. Creating a new client for each operation, though possible, will usually prevent reusing such connections.

Comments
Removed first sentence as it just repeats the title.
28-07-2023