You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Fang Lin <Fa...@u.washington.edu> on 2011/07/28 01:46:02 UTC

Only create 2 connection per route

I am now puzzled by the following two cases with my ThreadSafeClientConnManager :
1.  getConnectionsInPool (route) call shows that the value increased from 1 to 2 quickly, but never goes beyond 2.
2.  getConnectionsInPool (route) always return zero for the https routes.
Any ideas?

I did the following:
1. setDefaultMaxPerRoute to 5;  the getDefaultMaxPerRoute call confirmed it.
2. Also set individual route's max connections via setMaxForRoute (route) call;  getMaxForRoute (route) confirmed the value was set correctly.

Did the following for response handling:
1. If exception occurs, abort the current http get request
2. Otherwise, make sure the response entity is consumed in both successful response and response with an error code

The code of my ConnPoolManager class:

public class ConnPoolManager {
  private final static Logger log =   LoggerFactory.getLogger("ConnPoolManager");
  private static ContentEncodingHttpClient client;
  private static ThreadSafeClientConnManager manager;

  static {
    try {
       SchemeRegistry registry = new SchemeRegistry();
      registry.register(new Scheme("http", 
				   PlainSocketFactory.getSocketFactory(), 
				   80));
      registry.register(new Scheme("https", 
				   new MySocketFactory(),
				   443));

      HttpParams params = new BasicHttpParams();
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
      HttpProtocolParams.setContentCharset(params, "UTF-8");
      HttpProtocolParams.setUseExpectContinue(params, true);
      
      params.setBooleanParameter
	(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
      params.setBooleanParameter
	(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
      params.setIntParameter
	(CoreConnectionPNames.CONNECTION_TIMEOUT, 
	 PoolProperties.CONNECTION_TIMEOUT);
      params.setIntParameter
	(CoreConnectionPNames.SO_TIMEOUT, 
	 PoolProperties.SOCKET_TIMEOUT);
      params.setBooleanParameter
	(CoreConnectionPNames.SO_REUSEADDR, true);
      params.setIntParameter
	(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 
	 PoolProperties.SOCKET_BUFFER_SIZE);
      params.setIntParameter(ClientPNames.MAX_REDIRECTS, 1);

      manager = new ThreadSafeClientConnManager (params, registry);
      manager.setMaxTotal (PoolProperties.MAX_POOL_SIZE);
      manager.setDefaultMaxPerRoute (PoolProperties.MAX_PER_ROUTE);

      client = new ContentEncodingHttpClient (manager, params);
      client.setReuseStrategy (new ConnectionReuseStrategy() {
	  public boolean keepAlive(HttpResponse response,
				   HttpContext context) {
	    return true;
	  }
	});
      client.setKeepAliveStrategy (new ConnectionKeepAliveStrategy() {
	  public long getKeepAliveDuration(HttpResponse response, 
					       HttpContext context) {
	    return PoolProperties.KEEP_ALIVE_DURATION;
	  }
	});
    } catch (Exception e) {
      log.error ("Failed to set up ConnPoolManager", e);
    }
  }
				
  private ConnPoolManager () { }
  
 public static void setMaxForRoute (HttpRoute route,
				        int maxConn)
  {
      manager.setMaxForRoute (route, maxConn);
  }

  public static int getMaxForRoute (HttpRoute route)
  {
    return manager.getMaxForRoute (route);
  }

  public static int getConnectionsInPool (HttpRoute route)
  {
    return manager.getConnectionsInPool (route);
  }

  public static int getConnectionsInPool ()
  {
    return manager.getConnectionsInPool ();
  }

  public static int getMaxTotal ()
  {
    return manager.getMaxTotal();
  }

  public static HttpResponse execute (HttpRoute route,
				           HttpUriRequest request,
				           HttpContext localContext)
    throws IOException
  {
    return (localContext == null
	    ? client.execute(route.getTargetHost(), request)
	    : client.execute(route.getTargetHost(), request, localContext));
  }

  public static void logCon (HttpRoute route)
  {
    log.info (route.getTargetHost().toURI() + " has " +
	      getConnectionsInPool  (route) + "/" +
	      getMaxForRoute (route) +
	      " in-use/max of this route.");
    logStatus();
  }

  private static String poolStatus()
  {
    return ("Pool has total active httpclient connections/max: " + 
	    getConnectionsInPool() + "/" + getMaxTotal ());
  }
  public static void logStatus()
  {
    log.info (poolStatus ());
  }

  public static void shutdown()
  {
    manager.shutdown();
  }
}
-----Original Message-----
From: Sam Crawford [mailto:samcrawford@gmail.com] 
Sent: Friday, July 22, 2011 5:55 AM
To: HttpClient User Discussion
Subject: Re: ConnectionPoolTimeoutException

My experience leads me to agree with Oleg. I would suggest adding logging for connection management (see http://hc.apache.org/httpcomponents-client-ga/logging.html).

Make sure you're always consuming the content when handling responses.
When I first began working with HttpClient I was aborting our response handling process if I saw a non-2xx/3xx response code *without* consuming the response - this led to the connection not being evicted.

Thanks,

Sam


On 22 July 2011 13:26, Oleg Kalnichevski <ol...@apache.org> wrote:
> On Thu, 2011-07-21 at 18:47 +0000, Fang Lin wrote:
>> Often getting org.apache.http.conn.ConnectionPoolTimeoutException: 
>> Timeout waiting for connection
>>         at 
>> org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(Conn
>> PoolByRoute.java:417)
>>         at 
>> org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(ConnPo
>> olByRoute.java:300)
>>         at 
>> org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConn
>> ection(ThreadSafeClientConnManager.java:224)
>>         at 
>> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultReq
>> uestDirector.java:391)
>>         at 
>> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
>> ient.java:820) when there were only two active connections in a 
>> ConnPoolByRoute whose  max connection is set to 10.
>> This case happened with some routes but not all routes. Once it started, only restarting tomcat would fix the issue.
>> Any suggestion?
>>
>>
>> Httpclient 4.1.1, httpcore 4.1.2
>>
>> Java(TM) SE Runtime Environment (build 1.6.0_23-b05) Java HotSpot(TM) 
>> 64-Bit Server VM (build 19.0-b09, mixed mode)
>>
>
> I suspect your code is leaking connections.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Only create 2 connection per route

Posted by Fang Lin <Fa...@u.washington.edu>.
Thanks for the advice, Oleg!

-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@apache.org] 
Sent: Thursday, July 28, 2011 7:47 AM
To: HttpClient User Discussion
Subject: Re: Only create 2 connection per route

On Wed, 2011-07-27 at 23:46 +0000, Fang Lin wrote:
> I am now puzzled by the following two cases with my ThreadSafeClientConnManager :
> 1.  getConnectionsInPool (route) call shows that the value increased from 1 to 2 quickly, but never goes beyond 2.
> 2.  getConnectionsInPool (route) always return zero for the https routes.
> Any ideas?
> 

Context logging is your best friend.

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Only create 2 connection per route

Posted by Fang Lin <Fa...@u.washington.edu>.
Yes, I have stopped using the deprecated ThreadSafeClientConnManager constructor.

I was using HttpRoute(HttpHost target)! I should have respected the javadoc and used this one:  HttpRoute(HttpHost target, InetAddress local, boolean secure). 

Thank you so much, Sam!

-----Original Message-----
From: Sam Crawford [mailto:samcrawford@gmail.com] 
Sent: Monday, August 01, 2011 3:48 PM
To: HttpClient User Discussion
Subject: Re: Only create 2 connection per route

If the non-deprecated constructor is working for you, then why not stick with that? (At least for now - I realise it could be a valid HttpClient bug)

With regards to the SSL issue, have you definitely got the "secure"
boolean set to true when constructing the HttpRoute?

I may have missed an earlier post, but if you had a sample bit of code that reproduced this it'd be helpful.

Thanks,

Sam


On 1 August 2011 23:11, Fang Lin <Fa...@u.washington.edu> wrote:
> The context logging is very helpful! Found two issues:
>
> I noticed that when ThreadSafeClientConnManager is created without the BasicHttpParams, the setMaxForRoute (with value 15) works!
> manager = new ThreadSafeClientConnManager (registry);
> Log:
> 2011/08/01 13:19:21:690 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 15 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:19:21:698 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 14 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:19:21:699 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 13 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:19:21:699 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 12 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
>
> To verify the case, I changed back to use the deprecated constructor:
> manager = new ThreadSafeClientConnManager (params, registry); Both 
> setDefaultMaxPerRoute (with value 5)and setMaxForRoute (with value 15) had NO effect.
> Log:
> 2011/08/01 13:54:00:627 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 2 out of 2 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:54:00:636 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 1 out of 2 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:54:00:636 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 0 out of 2 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:54:00:638 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 0 out of 2 [HttpRoute[{}->http://localhost:80]][null]
>
> The same thing happened to https routes - only allocated 2 connections (total) for a https route when using the deprecated constructor.
> However, using ThreadSafeClientConnManager (registry), only setDefaultMaxPerRoute (with value 5) worked but setMaxForRoute (with value 15) did not work on a https route. And the total connection allocated is 5 instead of 15.
> Log:
> 2011/08/01 14:09:03:621 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 5 out of 5 
> [HttpRoute[{s}->https://...washington.edu:443]][null]
> 2011/08/01 14:09:03:629 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 4 out of 5 
> [HttpRoute[{s}->https://....washington.edu:443]][null]
> 2011/08/01 14:09:03:629 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 3 out of 5 
> [HttpRoute[{s}->https://....washington.edu:443]][null]
> 2011/08/01 14:09:03:630 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 2 out of 5 
> [HttpRoute[{s}->https://....washington.edu:443]][null]
> 2011/08/01 14:09:04:632 PDT [DEBUG] ConnPoolByRoute - Available 
> capacity: 1 out of 5 
> [HttpRoute[{s}->https://....washington.edu:443]][null]
> ......
> ConnPoolByRoute - [HttpRoute[{s}->https://...s.washington.edu:443]] 
> total kept alive: 2, total issued: 3, total allocated: 5 out of 120
>
> Any suggestion?
>
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org]
> Sent: Thursday, July 28, 2011 7:47 AM
> To: HttpClient User Discussion
> Subject: Re: Only create 2 connection per route
>
> On Wed, 2011-07-27 at 23:46 +0000, Fang Lin wrote:
>> I am now puzzled by the following two cases with my ThreadSafeClientConnManager :
>> 1.  getConnectionsInPool (route) call shows that the value increased from 1 to 2 quickly, but never goes beyond 2.
>> 2.  getConnectionsInPool (route) always return zero for the https routes.
>> Any ideas?
>>
>
> Context logging is your best friend.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Only create 2 connection per route

Posted by Sam Crawford <sa...@gmail.com>.
If the non-deprecated constructor is working for you, then why not
stick with that? (At least for now - I realise it could be a valid
HttpClient bug)

With regards to the SSL issue, have you definitely got the "secure"
boolean set to true when constructing the HttpRoute?

I may have missed an earlier post, but if you had a sample bit of code
that reproduced this it'd be helpful.

Thanks,

Sam


On 1 August 2011 23:11, Fang Lin <Fa...@u.washington.edu> wrote:
> The context logging is very helpful! Found two issues:
>
> I noticed that when ThreadSafeClientConnManager is created without the BasicHttpParams, the setMaxForRoute (with value 15) works!
> manager = new ThreadSafeClientConnManager (registry);
> Log:
> 2011/08/01 13:19:21:690 PDT [DEBUG] ConnPoolByRoute - Available capacity: 15 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:19:21:698 PDT [DEBUG] ConnPoolByRoute - Available capacity: 14 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:19:21:699 PDT [DEBUG] ConnPoolByRoute - Available capacity: 13 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:19:21:699 PDT [DEBUG] ConnPoolByRoute - Available capacity: 12 out
>  of 15 [HttpRoute[{}->http://localhost:80]][null]
>
> To verify the case, I changed back to use the deprecated constructor:
> manager = new ThreadSafeClientConnManager (params, registry);
> Both setDefaultMaxPerRoute (with value 5)and setMaxForRoute (with value 15) had NO effect.
> Log:
> 2011/08/01 13:54:00:627 PDT [DEBUG] ConnPoolByRoute - Available capacity: 2 out
> of 2 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:54:00:636 PDT [DEBUG] ConnPoolByRoute - Available capacity: 1 out
> of 2 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:54:00:636 PDT [DEBUG] ConnPoolByRoute - Available capacity: 0 out
> of 2 [HttpRoute[{}->http://localhost:80]][null]
> 2011/08/01 13:54:00:638 PDT [DEBUG] ConnPoolByRoute - Available capacity: 0 out
> of 2 [HttpRoute[{}->http://localhost:80]][null]
>
> The same thing happened to https routes - only allocated 2 connections (total) for a https route when using the deprecated constructor.
> However, using ThreadSafeClientConnManager (registry), only setDefaultMaxPerRoute (with value 5) worked but setMaxForRoute (with value 15) did not work on a https route. And the total connection allocated is 5 instead of 15.
> Log:
> 2011/08/01 14:09:03:621 PDT [DEBUG] ConnPoolByRoute - Available capacity: 5 out
> of 5 [HttpRoute[{s}->https://...washington.edu:443]][null]
> 2011/08/01 14:09:03:629 PDT [DEBUG] ConnPoolByRoute - Available capacity: 4 out
> of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
> 2011/08/01 14:09:03:629 PDT [DEBUG] ConnPoolByRoute - Available capacity: 3 out
> of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
> 2011/08/01 14:09:03:630 PDT [DEBUG] ConnPoolByRoute - Available capacity: 2 out
> of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
> 2011/08/01 14:09:04:632 PDT [DEBUG] ConnPoolByRoute - Available capacity: 1 out
> of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
> ......
> ConnPoolByRoute - [HttpRoute[{s}->https://...s.washington.edu:443]] total kept alive: 2, total issued: 3, total allocated: 5 out of 120
>
> Any suggestion?
>
> -----Original Message-----
> From: Oleg Kalnichevski [mailto:olegk@apache.org]
> Sent: Thursday, July 28, 2011 7:47 AM
> To: HttpClient User Discussion
> Subject: Re: Only create 2 connection per route
>
> On Wed, 2011-07-27 at 23:46 +0000, Fang Lin wrote:
>> I am now puzzled by the following two cases with my ThreadSafeClientConnManager :
>> 1.  getConnectionsInPool (route) call shows that the value increased from 1 to 2 quickly, but never goes beyond 2.
>> 2.  getConnectionsInPool (route) always return zero for the https routes.
>> Any ideas?
>>
>
> Context logging is your best friend.
>
> Oleg
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Only create 2 connection per route

Posted by Fang Lin <Fa...@u.washington.edu>.
The context logging is very helpful! Found two issues:

I noticed that when ThreadSafeClientConnManager is created without the BasicHttpParams, the setMaxForRoute (with value 15) works!
manager = new ThreadSafeClientConnManager (registry);
Log:
2011/08/01 13:19:21:690 PDT [DEBUG] ConnPoolByRoute - Available capacity: 15 out
 of 15 [HttpRoute[{}->http://localhost:80]][null]
2011/08/01 13:19:21:698 PDT [DEBUG] ConnPoolByRoute - Available capacity: 14 out
 of 15 [HttpRoute[{}->http://localhost:80]][null]
2011/08/01 13:19:21:699 PDT [DEBUG] ConnPoolByRoute - Available capacity: 13 out
 of 15 [HttpRoute[{}->http://localhost:80]][null]
2011/08/01 13:19:21:699 PDT [DEBUG] ConnPoolByRoute - Available capacity: 12 out
 of 15 [HttpRoute[{}->http://localhost:80]][null]

To verify the case, I changed back to use the deprecated constructor:
manager = new ThreadSafeClientConnManager (params, registry);
Both setDefaultMaxPerRoute (with value 5)and setMaxForRoute (with value 15) had NO effect.
Log:
2011/08/01 13:54:00:627 PDT [DEBUG] ConnPoolByRoute - Available capacity: 2 out 
of 2 [HttpRoute[{}->http://localhost:80]][null]
2011/08/01 13:54:00:636 PDT [DEBUG] ConnPoolByRoute - Available capacity: 1 out 
of 2 [HttpRoute[{}->http://localhost:80]][null]
2011/08/01 13:54:00:636 PDT [DEBUG] ConnPoolByRoute - Available capacity: 0 out 
of 2 [HttpRoute[{}->http://localhost:80]][null]
2011/08/01 13:54:00:638 PDT [DEBUG] ConnPoolByRoute - Available capacity: 0 out 
of 2 [HttpRoute[{}->http://localhost:80]][null] 

The same thing happened to https routes - only allocated 2 connections (total) for a https route when using the deprecated constructor.
However, using ThreadSafeClientConnManager (registry), only setDefaultMaxPerRoute (with value 5) worked but setMaxForRoute (with value 15) did not work on a https route. And the total connection allocated is 5 instead of 15.  
Log:
2011/08/01 14:09:03:621 PDT [DEBUG] ConnPoolByRoute - Available capacity: 5 out 
of 5 [HttpRoute[{s}->https://...washington.edu:443]][null]
2011/08/01 14:09:03:629 PDT [DEBUG] ConnPoolByRoute - Available capacity: 4 out 
of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
2011/08/01 14:09:03:629 PDT [DEBUG] ConnPoolByRoute - Available capacity: 3 out 
of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
2011/08/01 14:09:03:630 PDT [DEBUG] ConnPoolByRoute - Available capacity: 2 out 
of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
2011/08/01 14:09:04:632 PDT [DEBUG] ConnPoolByRoute - Available capacity: 1 out 
of 5 [HttpRoute[{s}->https://....washington.edu:443]][null]
......
ConnPoolByRoute - [HttpRoute[{s}->https://...s.washington.edu:443]] total kept alive: 2, total issued: 3, total allocated: 5 out of 120

Any suggestion?

-----Original Message-----
From: Oleg Kalnichevski [mailto:olegk@apache.org] 
Sent: Thursday, July 28, 2011 7:47 AM
To: HttpClient User Discussion
Subject: Re: Only create 2 connection per route

On Wed, 2011-07-27 at 23:46 +0000, Fang Lin wrote:
> I am now puzzled by the following two cases with my ThreadSafeClientConnManager :
> 1.  getConnectionsInPool (route) call shows that the value increased from 1 to 2 quickly, but never goes beyond 2.
> 2.  getConnectionsInPool (route) always return zero for the https routes.
> Any ideas?
> 

Context logging is your best friend.

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Only create 2 connection per route

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2011-07-27 at 23:46 +0000, Fang Lin wrote:
> I am now puzzled by the following two cases with my ThreadSafeClientConnManager :
> 1.  getConnectionsInPool (route) call shows that the value increased from 1 to 2 quickly, but never goes beyond 2.
> 2.  getConnectionsInPool (route) always return zero for the https routes.
> Any ideas?
> 

Context logging is your best friend.

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org