hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1587883 - in /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn: BasicHttpClientConnectionManager.java PoolingHttpClientConnectionManager.java
Date Wed, 16 Apr 2014 11:52:16 GMT
Author: olegk
Date: Wed Apr 16 11:52:16 2014
New Revision: 1587883

URL: http://svn.apache.org/r1587883
Log:
HTTPCLIENT-1495: shut down standard connection managers only once

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java?rev=1587883&r1=1587882&r2=1587883&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
Wed Apr 16 11:52:16 2014
@@ -32,6 +32,7 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.Date;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -108,8 +109,7 @@ public class BasicHttpClientConnectionMa
     @GuardedBy("this")
     private ConnectionConfig connConfig;
 
-    @GuardedBy("this")
-    private volatile boolean shutdown;
+    private final AtomicBoolean isShutdown;
 
     private static Registry<ConnectionSocketFactory> getDefaultRegistry() {
         return RegistryBuilder.<ConnectionSocketFactory>create()
@@ -141,6 +141,7 @@ public class BasicHttpClientConnectionMa
         this.expiry = Long.MAX_VALUE;
         this.socketConfig = SocketConfig.DEFAULT;
         this.connConfig = ConnectionConfig.DEFAULT;
+        this.isShutdown = new AtomicBoolean(false);
     }
 
     public BasicHttpClientConnectionManager(
@@ -256,7 +257,7 @@ public class BasicHttpClientConnectionMa
     }
 
     synchronized HttpClientConnection getConnection(final HttpRoute route, final Object state)
{
-        Asserts.check(!this.shutdown, "Connection manager has been shut down");
+        Asserts.check(!this.isShutdown.get(), "Connection manager has been shut down");
         if (this.log.isDebugEnabled()) {
             this.log.debug("Get connection for route " + route);
         }
@@ -284,8 +285,7 @@ public class BasicHttpClientConnectionMa
         if (this.log.isDebugEnabled()) {
             this.log.debug("Releasing connection " + conn);
         }
-        if (this.shutdown) {
-            shutdownConnection();
+        if (this.isShutdown.get()) {
             return;
         }
         try {
@@ -357,7 +357,7 @@ public class BasicHttpClientConnectionMa
 
     @Override
     public synchronized void closeExpiredConnections() {
-        if (this.shutdown) {
+        if (this.isShutdown.get()) {
             return;
         }
         if (!this.leased) {
@@ -368,7 +368,7 @@ public class BasicHttpClientConnectionMa
     @Override
     public synchronized void closeIdleConnections(final long idletime, final TimeUnit tunit)
{
         Args.notNull(tunit, "Time unit");
-        if (this.shutdown) {
+        if (this.isShutdown.get()) {
             return;
         }
         if (!this.leased) {
@@ -385,11 +385,9 @@ public class BasicHttpClientConnectionMa
 
     @Override
     public synchronized void shutdown() {
-        if (this.shutdown) {
-            return;
+        if (this.isShutdown.compareAndSet(false, true)) {
+            shutdownConnection();
         }
-        this.shutdown = true;
-        shutdownConnection();
     }
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java?rev=1587883&r1=1587882&r2=1587883&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
Wed Apr 16 11:52:16 2014
@@ -35,6 +35,7 @@ import java.util.concurrent.ExecutionExc
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -92,6 +93,7 @@ public class PoolingHttpClientConnection
     private final ConfigData configData;
     private final CPool pool;
     private final HttpClientConnectionOperator connectionOperator;
+    private final AtomicBoolean isShutDown;
 
     private static Registry<ConnectionSocketFactory> getDefaultRegistry() {
         return RegistryBuilder.<ConnectionSocketFactory>create()
@@ -157,11 +159,12 @@ public class PoolingHttpClientConnection
         final HttpClientConnectionOperator httpClientConnectionOperator,
         final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
         final long timeToLive, final TimeUnit tunit) {
-      super();
-      this.configData = new ConfigData();
-      this.pool = new CPool(
-          new InternalConnectionFactory(this.configData, connFactory), 2, 20, timeToLive,
tunit);
-      this.connectionOperator = Args.notNull(httpClientConnectionOperator, "HttpClientConnectionOperator");
+        super();
+        this.configData = new ConfigData();
+        this.pool = new CPool(new InternalConnectionFactory(
+                this.configData, connFactory), 2, 20, timeToLive, tunit);
+        this.connectionOperator = Args.notNull(httpClientConnectionOperator, "HttpClientConnectionOperator");
+        this.isShutDown = new AtomicBoolean(false);
     }
 
     /**
@@ -177,6 +180,7 @@ public class PoolingHttpClientConnection
         this.pool = pool;
         this.connectionOperator = new DefaultHttpClientConnectionOperator(
                 socketFactoryRegistry, schemePortResolver, dnsResolver);
+        this.isShutDown = new AtomicBoolean(false);
     }
 
     @Override
@@ -368,13 +372,15 @@ public class PoolingHttpClientConnection
 
     @Override
     public void shutdown() {
-        this.log.debug("Connection manager is shutting down");
-        try {
-            this.pool.shutdown();
-        } catch (final IOException ex) {
-            this.log.debug("I/O exception shutting down connection manager", ex);
+        if (this.isShutDown.compareAndSet(false, true)) {
+            this.log.debug("Connection manager is shutting down");
+            try {
+                this.pool.shutdown();
+            } catch (final IOException ex) {
+                this.log.debug("I/O exception shutting down connection manager", ex);
+            }
+            this.log.debug("Connection manager shut down");
         }
-        this.log.debug("Connection manager shut down");
     }
 
     @Override



Mime
View raw message