From 6ba9b4acdcba411a035503e3eae9e17b64bcc59b Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sat, 11 Dec 2021 16:23:12 +0100 Subject: [PATCH] Method to check if the connection manager has been shut down and is closed or not. --- .../io/BasicHttpClientConnectionManager.java | 20 ++++++++++++++----- .../PoolingHttpClientConnectionManager.java | 1 - .../PoolingAsyncClientConnectionManager.java | 11 ++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java index 05d8ac4e9..0c515c66f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/BasicHttpClientConnectionManager.java @@ -299,7 +299,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan } synchronized ManagedHttpClientConnection getConnection(final HttpRoute route, final Object state) throws IOException { - Asserts.check(!this.closed.get(), "Connection manager has been shut down"); + Asserts.check(!isClosed(), "Connection manager has been shut down"); if (LOG.isDebugEnabled()) { LOG.debug("{} Get connection for route {}", id, route); } @@ -336,7 +336,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan if (LOG.isDebugEnabled()) { LOG.debug("{} Releasing connection {}", id, conn); } - if (this.closed.get()) { + if (isClosed()) { return; } try { @@ -345,7 +345,6 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan } this.updated = System.currentTimeMillis(); if (!this.conn.isOpen() && !this.conn.isConsistent()) { - this.conn = null; this.route = null; this.conn = null; this.expiry = Long.MAX_VALUE; @@ -427,7 +426,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan } public synchronized void closeExpired() { - if (this.closed.get()) { + if (isClosed()) { return; } if (!this.leased) { @@ -437,7 +436,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan public synchronized void closeIdle(final TimeValue idleTime) { Args.notNull(idleTime, "Idle time"); - if (this.closed.get()) { + if (isClosed()) { return; } if (!this.leased) { @@ -556,4 +555,15 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan } + /** + * Method that can be called to determine whether the connection manager has been shut down and + * is closed or not. + * + * @return {@code true} if the connection manager has been shut down and is closed, otherwise + * return {@code false}. + */ + boolean isClosed() { + return this.closed.get(); + } + } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java index 626fe9831..426a85133 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/PoolingHttpClientConnectionManager.java @@ -686,5 +686,4 @@ public class PoolingHttpClientConnectionManager } } - } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java index 3975859c4..7d377f0ca 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java @@ -727,4 +727,15 @@ public class PoolingAsyncClientConnectionManager implements AsyncClientConnectio } + /** + * Method that can be called to determine whether the connection manager has been shut down and + * is closed or not. + * + * @return {@code true} if the connection manager has been shut down and is closed, otherwise + * return {@code false}. + */ + boolean isClosed() { + return this.closed.get(); + } + }