RFR: 8267140: Support closing the HttpClient by making it auto-closable [v4]

Jaikiran Pai jpai at openjdk.org
Mon Mar 20 13:09:37 UTC 2023


On Mon, 20 Mar 2023 10:57:05 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

>> Please find here an RFE that makes the `java.net.HttpClient` auto-closeable.
>> 
>> The API has been modeled on `ExecutorService`.
>> 
>> HttpClient::close() is a graceful shutdown and will wait until all operations are terminated before returning.
>> If a request is in progress, and the caller doesn't pull the corresponding data (for instance, the request was sent with a BodyHandler.ofInputStream(), but the caller stopped reading the input stream) then close() may never return.
>> 
>> Therefore, additional methods similar to those present in `ExecutorService` are also proposed. In summary:
>> 
>> - `shutdown()`: initiate a graceful shutdown, but doesn't wait for termination. 
>> - `shutdownNow()`:  initiate an immediate shutdown, attempting to cancel all operations in progress. Doesn't wait for termination.
>> - `awaitTermination(Duration)`: await for termination within the given delay
>> - `isTerminated()` tells whether the client is terminated
>> 
>> New tests have been added to test the proposed behaviors.
>> 
>> HttpClient tests (new and old) are still stable.
>
> Daniel Fuchs has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 10 additional commits since the last revision:
> 
>  - Merge branch 'master' into HttpClient-close-8267140
>  - Update test/jdk/java/net/httpclient/HttpClientShutdown.java
>    
>    Co-authored-by: Andrey Turbanov <turbanoff at gmail.com>
>  - Update test/jdk/java/net/httpclient/ShutdownNow.java
>    
>    Co-authored-by: Andrey Turbanov <turbanoff at gmail.com>
>  - Update test/jdk/java/net/httpclient/HttpClientShutdown.java
>    
>    Co-authored-by: Andrey Turbanov <turbanoff at gmail.com>
>  - Update test/jdk/java/net/httpclient/AsyncShutdownNow.java
>    
>    Co-authored-by: Andrey Turbanov <turbanoff at gmail.com>
>  - Review feedback
>  - typo
>  - Merge branch 'master' into HttpClient-close-8267140
>  - Throw NPE if duration is null
>  - 8267140

src/java.net.http/share/classes/java/net/http/HttpClient.java line 773:

> 771:      *
> 772:      * @implSpec
> 773:      * The default implementation of this method does nothing and returns true.

It's a bit odd that this default implementation would return `true` (meaning the client has terminated) and then the default implemenation of `isTerminated()` method is returning `false`. I think the implementation of this default method should return `false`, both to match the value returned by `isTerminated` as well as because the default implementation would not really know if the client is terminated.

src/java.net.http/share/classes/java/net/http/HttpClient.java line 790:

> 788: 
> 789:     /**
> 790:      * Returns {@code true} if all operations have completed following shut down.

To be consistent, I think it should be "shutdown" instead of "shut down". More precisely "Returns {@code true} if all operations have completed following a shutdown"

src/java.net.http/share/classes/java/net/http/HttpClient.java line 799:

> 797:      * See the API Note on {@linkplain ##closing closing}.
> 798:      *
> 799:      * @return {@code true} if all tasks have completed following shut down

Same here. I think it should be "following a shutdown"

-------------

PR: https://git.openjdk.org/jdk/pull/13019


More information about the net-dev mailing list