hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1587507 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/impl/client/ test/java/org/apache/http/impl/client/
Date Tue, 15 Apr 2014 10:53:50 GMT
Author: olegk
Date: Tue Apr 15 10:53:49 2014
New Revision: 1587507

URL: http://svn.apache.org/r1587507
Log:
Support for shared connection managers

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestInternalHttpClient.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java?rev=1587507&r1=1587506&r2=1587507&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
Tue Apr 15 10:53:49 2014
@@ -28,6 +28,7 @@
 package org.apache.http.impl.client;
 
 import java.io.Closeable;
+import java.io.IOException;
 import java.net.ProxySelector;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -160,6 +161,7 @@ public class HttpClientBuilder {
     private LayeredConnectionSocketFactory sslSocketFactory;
     private SSLContext sslcontext;
     private HttpClientConnectionManager connManager;
+    private boolean connManagerShared;
     private SchemePortResolver schemePortResolver;
     private ConnectionReuseStrategy reuseStrategy;
     private ConnectionKeepAliveStrategy keepAliveStrategy;
@@ -332,7 +334,26 @@ public class HttpClientBuilder {
      */
     public final HttpClientBuilder setConnectionManager(
             final HttpClientConnectionManager connManager) {
+        return setConnectionManager(connManager, false);
+    }
+
+    /**
+     * Assigns {@link HttpClientConnectionManager} instance.
+     * <p/>
+     * If the connection manager is shared its life-cycle is expected
+     * to be managed by the caller and it will not be shut down
+     * if the client is closed.
+     *
+     * @param connManager connection manager
+     * @param shared defines whether or not the connection manager can be shared
+     *  by multiple clients.
+     *
+     * @since 4.4
+     */
+    public final HttpClientBuilder setConnectionManager(
+            final HttpClientConnectionManager connManager, final boolean shared) {
         this.connManager = connManager;
+        this.connManagerShared = shared;
         return this;
     }
 
@@ -990,6 +1011,22 @@ public class HttpClientBuilder {
             }
         }
 
+        List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables)
: null;
+        if (!this.connManagerShared) {
+            if (closeablesCopy == null) {
+                closeablesCopy = new ArrayList<Closeable>(1);
+            }
+            final HttpClientConnectionManager cm = connManagerCopy;
+            closeablesCopy.add(new Closeable() {
+
+                @Override
+                public void close() throws IOException {
+                    cm.shutdown();
+                }
+
+            });
+        }
+
         return new InternalHttpClient(
                 execChain,
                 connManagerCopy,
@@ -999,7 +1036,7 @@ public class HttpClientBuilder {
                 defaultCookieStore,
                 defaultCredentialsProvider,
                 defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
-                closeables != null ? new ArrayList<Closeable>(closeables) : null);
+                closeablesCopy);
     }
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java?rev=1587507&r1=1587506&r2=1587507&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
Tue Apr 15 10:53:49 2014
@@ -189,7 +189,6 @@ class InternalHttpClient extends Closeab
 
     @Override
     public void close() {
-        this.connManager.shutdown();
         if (this.closeables != null) {
             for (final Closeable closeable: this.closeables) {
                 try {

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestInternalHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestInternalHttpClient.java?rev=1587507&r1=1587506&r2=1587507&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestInternalHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestInternalHttpClient.java
Tue Apr 15 10:53:49 2014
@@ -188,7 +188,6 @@ public class TestInternalHttpClient {
     public void testClientClose() throws Exception {
         client.close();
 
-        Mockito.verify(connManager).shutdown();
         Mockito.verify(closeable1).close();
         Mockito.verify(closeable2).close();
     }
@@ -199,7 +198,6 @@ public class TestInternalHttpClient {
 
         client.close();
 
-        Mockito.verify(connManager).shutdown();
         Mockito.verify(closeable1).close();
         Mockito.verify(closeable2).close();
     }



Mime
View raw message