RFR: 8267140: Support closing the HttpClient by making it auto-closable [v3]
Andrey Turbanov
aturbanov at openjdk.org
Fri Mar 17 07:40:28 UTC 2023
On Tue, 14 Mar 2023 19:46:07 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 incrementally with one additional commit since the last revision:
>
> typo
test/jdk/java/net/httpclient/AsyncShutdownNow.java line 158:
> 156: while (t != null) {
> 157: out.println(what + ": checking " + t);
> 158: if (t instanceof IOException io && hasExpectedMessage(io)) {
Suggestion:
if (t instanceof IOException io && hasExpectedMessage(io)) {
test/jdk/java/net/httpclient/HttpClientShutdown.java line 160:
> 158: while (t != null) {
> 159: out.println(now() + what + ": checking " + t);
> 160: if (t instanceof IOException io && hasExpectedMessage(io)) {
Suggestion:
if (t instanceof IOException io && hasExpectedMessage(io)) {
test/jdk/java/net/httpclient/HttpClientShutdown.java line 451:
> 449: for (int i=0; i<bytes.length; i++) {
> 450: long now = System.nanoTime();
> 451: long sincePrevious = Duration.ofNanos(now - previous).toMillis();
Suggestion:
long sincePrevious = Duration.ofNanos(now - previous).toMillis();
test/jdk/java/net/httpclient/ShutdownNow.java line 145:
> 143: while (t != null) {
> 144: out.println(what + ": checking " + t);
> 145: if (t instanceof IOException io && hasExpectedMessage(io)) {
Suggestion:
if (t instanceof IOException io && hasExpectedMessage(io)) {
-------------
PR: https://git.openjdk.org/jdk/pull/13019
More information about the net-dev
mailing list