From 39cec467152313230f7ca6a558ba178ddbbced28 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 26 Jul 2019 14:13:43 +0200 Subject: [PATCH] CloseableHttpClient and CloseableHttpAsyncClient to implement ModalClosable --- .../AbstractHttp1IntegrationTestBase.java | 2 +- .../async/AbstractIntegrationTestBase.java | 2 +- .../async/AbstractHttpAsyncClientBase.java | 8 +++++-- .../impl/async/CloseableHttpAsyncClient.java | 14 +++++++---- .../InternalAbstractHttpAsyncClient.java | 23 ++++++++++++------- .../impl/classic/CloseableHttpClient.java | 6 ++--- .../http/impl/classic/InternalHttpClient.java | 21 +++++++++++++---- .../http/impl/classic/MinimalHttpClient.java | 5 ++++ .../examples/AsyncClientAuthentication.java | 2 +- .../AsyncClientConnectionEviction.java | 2 +- .../http/examples/AsyncClientCustomSSL.java | 2 +- .../AsyncClientFullDuplexExchange.java | 2 +- .../AsyncClientH2FullDuplexExchange.java | 2 +- .../examples/AsyncClientH2Multiplexing.java | 2 +- .../examples/AsyncClientH2ServerPush.java | 2 +- .../examples/AsyncClientHttp1Pipelining.java | 2 +- .../examples/AsyncClientHttpExchange.java | 2 +- .../AsyncClientHttpExchangeStreaming.java | 2 +- .../examples/AsyncClientInterceptors.java | 2 +- .../examples/AsyncClientMessageTrailers.java | 2 +- .../http/examples/AsyncClientTlsAlpn.java | 2 +- .../ReactiveClientFullDuplexExchange.java | 2 +- 22 files changed, 72 insertions(+), 37 deletions(-) diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttp1IntegrationTestBase.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttp1IntegrationTestBase.java index 424b7193b..f9867fb47 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttp1IntegrationTestBase.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttp1IntegrationTestBase.java @@ -99,7 +99,7 @@ public abstract class AbstractHttp1IntegrationTestBase extends AbstractServerTes @Override protected void after() { if (httpclient != null) { - httpclient.shutdown(CloseMode.GRACEFUL); + httpclient.close(CloseMode.GRACEFUL); httpclient = null; } } diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractIntegrationTestBase.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractIntegrationTestBase.java index 03d9a4d29..d13a608c9 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractIntegrationTestBase.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractIntegrationTestBase.java @@ -63,7 +63,7 @@ public abstract class AbstractIntegrationTestBase Future execute( final AsyncRequestProducer requestProducer, diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java index 58e56c046..35aedc6f4 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java @@ -30,6 +30,7 @@ import java.io.Closeable; import java.io.IOException; import java.util.List; import java.util.Set; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicBoolean; @@ -64,6 +65,8 @@ import org.apache.hc.core5.http.nio.DataStreamChannel; import org.apache.hc.core5.http.nio.HandlerFactory; import org.apache.hc.core5.http.nio.RequestChannel; import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.io.CloseMode; +import org.apache.hc.core5.io.ModalCloseable; import org.apache.hc.core5.reactor.DefaultConnectingIOReactor; abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBase { @@ -74,7 +77,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa private final CookieStore cookieStore; private final CredentialsProvider credentialsProvider; private final RequestConfig defaultConfig; - private final List closeables; + private final ConcurrentLinkedQueue closeables; InternalAbstractHttpAsyncClient( final DefaultConnectingIOReactor ioReactor, @@ -94,18 +97,22 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa this.cookieStore = cookieStore; this.credentialsProvider = credentialsProvider; this.defaultConfig = defaultConfig; - this.closeables = closeables; + this.closeables = closeables != null ? new ConcurrentLinkedQueue<>(closeables) : null; } @Override - public void close() { - super.close(); - if (closeables != null) { - for (final Closeable closeable: closeables) { + void internalClose(final CloseMode closeMode) { + if (this.closeables != null) { + Closeable closeable; + while ((closeable = this.closeables.poll()) != null) { try { - closeable.close(); + if (closeable instanceof ModalCloseable) { + ((ModalCloseable) closeable).close(closeMode); + } else { + closeable.close(); + } } catch (final IOException ex) { - log.error(ex.getMessage(), ex); + this.log.error(ex.getMessage(), ex); } } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/CloseableHttpClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/CloseableHttpClient.java index fb1d3525f..6495a88ca 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/CloseableHttpClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/CloseableHttpClient.java @@ -27,7 +27,6 @@ package org.apache.hc.client5.http.impl.classic; -import java.io.Closeable; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -44,17 +43,18 @@ import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.io.HttpClientResponseHandler; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.io.ModalCloseable; import org.apache.hc.core5.util.Args; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Base implementation of {@link HttpClient} that also implements {@link Closeable}. + * Base implementation of {@link HttpClient} that also implements {@link ModalCloseable}. * * @since 4.3 */ @Contract(threading = ThreadingBehavior.SAFE) -public abstract class CloseableHttpClient implements HttpClient, Closeable { +public abstract class CloseableHttpClient implements HttpClient, ModalCloseable { private final Logger log = LoggerFactory.getLogger(getClass()); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java index 88b3230e5..0240cc48d 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/InternalHttpClient.java @@ -30,6 +30,7 @@ package org.apache.hc.client5.http.impl.classic; import java.io.Closeable; import java.io.IOException; import java.util.List; +import java.util.concurrent.ConcurrentLinkedQueue; import org.apache.hc.client5.http.ClientProtocolException; import org.apache.hc.client5.http.HttpRoute; @@ -59,6 +60,8 @@ import org.apache.hc.core5.http.config.Lookup; import org.apache.hc.core5.http.impl.io.HttpRequestExecutor; import org.apache.hc.core5.http.protocol.BasicHttpContext; import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.io.CloseMode; +import org.apache.hc.core5.io.ModalCloseable; import org.apache.hc.core5.net.URIAuthority; import org.apache.hc.core5.util.Args; import org.slf4j.Logger; @@ -88,7 +91,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable { private final CookieStore cookieStore; private final CredentialsProvider credentialsProvider; private final RequestConfig defaultConfig; - private final List closeables; + private final ConcurrentLinkedQueue closeables; public InternalHttpClient( final HttpClientConnectionManager connManager, @@ -111,7 +114,7 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable { this.cookieStore = cookieStore; this.credentialsProvider = credentialsProvider; this.defaultConfig = defaultConfig; - this.closeables = closeables; + this.closeables = closeables != null ? new ConcurrentLinkedQueue<>(closeables) : null; } private HttpRoute determineRoute( @@ -186,10 +189,20 @@ class InternalHttpClient extends CloseableHttpClient implements Configurable { @Override public void close() { + close(CloseMode.GRACEFUL); + } + + @Override + public void close(final CloseMode closeMode) { if (this.closeables != null) { - for (final Closeable closeable: this.closeables) { + Closeable closeable; + while ((closeable = this.closeables.poll()) != null) { try { - closeable.close(); + if (closeable instanceof ModalCloseable) { + ((ModalCloseable) closeable).close(closeMode); + } else { + closeable.close(); + } } catch (final IOException ex) { this.log.error(ex.getMessage(), ex); } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java index e316c1dc8..5dfb6623c 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MinimalHttpClient.java @@ -186,4 +186,9 @@ public class MinimalHttpClient extends CloseableHttpClient { this.connManager.close(); } + @Override + public void close(final CloseMode closeMode) { + this.connManager.close(closeMode); + } + } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientAuthentication.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientAuthentication.java index 08278f34c..8925ace3c 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientAuthentication.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientAuthentication.java @@ -83,7 +83,7 @@ public class AsyncClientAuthentication { future.get(); System.out.println("Shutting down"); - httpclient.shutdown(CloseMode.GRACEFUL); + httpclient.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientConnectionEviction.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientConnectionEviction.java index 8f9d2cd32..543f1859d 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientConnectionEviction.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientConnectionEviction.java @@ -121,7 +121,7 @@ public class AsyncClientConnectionEviction { future2.get(); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientCustomSSL.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientCustomSSL.java index 2f1bb9237..8184adca4 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientCustomSSL.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientCustomSSL.java @@ -133,7 +133,7 @@ public class AsyncClientCustomSSL { future.get(); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientFullDuplexExchange.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientFullDuplexExchange.java index 1481e01b2..53f869e92 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientFullDuplexExchange.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientFullDuplexExchange.java @@ -151,7 +151,7 @@ public class AsyncClientFullDuplexExchange { latch.await(1, TimeUnit.MINUTES); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2FullDuplexExchange.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2FullDuplexExchange.java index f3ea5f5cd..4a7727ea1 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2FullDuplexExchange.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2FullDuplexExchange.java @@ -147,7 +147,7 @@ public class AsyncClientH2FullDuplexExchange { latch.await(1, TimeUnit.MINUTES); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2Multiplexing.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2Multiplexing.java index e10af5a07..94c3a67b4 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2Multiplexing.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2Multiplexing.java @@ -104,7 +104,7 @@ public class AsyncClientH2Multiplexing { } System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java index 12ec238c5..4a16dbc66 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientH2ServerPush.java @@ -155,7 +155,7 @@ public class AsyncClientH2ServerPush { future.get(); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java index 9c3d3778c..0b1f3c7f4 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttp1Pipelining.java @@ -103,7 +103,7 @@ public class AsyncClientHttp1Pipelining { } System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchange.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchange.java index 9d4c21228..ff96d024f 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchange.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchange.java @@ -87,7 +87,7 @@ public class AsyncClientHttpExchange { } System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchangeStreaming.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchangeStreaming.java index c7c884f44..dc3e87b8e 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchangeStreaming.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientHttpExchangeStreaming.java @@ -110,7 +110,7 @@ public class AsyncClientHttpExchangeStreaming { } System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientInterceptors.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientInterceptors.java index e1a6d97a2..82b5ff2f7 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientInterceptors.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientInterceptors.java @@ -150,7 +150,7 @@ public class AsyncClientInterceptors { } System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientMessageTrailers.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientMessageTrailers.java index db2b916f8..d9201cea0 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientMessageTrailers.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientMessageTrailers.java @@ -116,7 +116,7 @@ public class AsyncClientMessageTrailers { future.get(); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientTlsAlpn.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientTlsAlpn.java index 412307a21..97baeec6d 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientTlsAlpn.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/AsyncClientTlsAlpn.java @@ -115,7 +115,7 @@ public class AsyncClientTlsAlpn { future.get(); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ReactiveClientFullDuplexExchange.java b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ReactiveClientFullDuplexExchange.java index 9803e2ce4..9a787b90f 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ReactiveClientFullDuplexExchange.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ReactiveClientFullDuplexExchange.java @@ -111,6 +111,6 @@ public class ReactiveClientFullDuplexExchange { requestFuture.get(1, TimeUnit.MINUTES); System.out.println("Shutting down"); - client.shutdown(CloseMode.GRACEFUL); + client.close(CloseMode.GRACEFUL); } }