Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8267140 Support closing the HttpClient by making it auto-closable
  3. JDK-8305196

Release Note: The `java.net.http.HttpClient` Is Now AutoCloseable

XMLWordPrintable

      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.

            dfuchs Daniel Fuchs
            dfuchs Daniel Fuchs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: