Return-Path: X-Original-To: apmail-hc-httpclient-users-archive@www.apache.org Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DD5FC101E0 for ; Tue, 25 Mar 2014 09:45:18 +0000 (UTC) Received: (qmail 4951 invoked by uid 500); 25 Mar 2014 09:45:17 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 4558 invoked by uid 500); 25 Mar 2014 09:45:16 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 4539 invoked by uid 99); 25 Mar 2014 09:45:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Mar 2014 09:45:13 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy includes SPF record at spf.trusted-forwarder.org) Received: from [5.148.180.21] (HELO kalnich2.nine.ch) (5.148.180.21) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Mar 2014 09:45:06 +0000 Received: from [192.168.42.191] (unknown [213.55.184.177]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by kalnich2.nine.ch (Postfix) with ESMTPSA id DCF0816009C for ; Tue, 25 Mar 2014 09:44:45 +0000 (UTC) Message-ID: <1395740683.10953.12.camel@ubuntu> Subject: Re: Connection leakage from Migrating 4.2.3 -> 4.3.3 From: Oleg Kalnichevski To: HttpClient User Discussion Date: Tue, 25 Mar 2014 10:44:43 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.8.4-0ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org On Mon, 2014-03-24 at 21:09 -0400, Nick Chang wrote: > Hello, > > We are using httpclient to build a web proxy application in our product. We > use 4.2.3 version and it has been working flawlessly. Now I am trying to > migrate the product's httpclient library from 4.2.3 to 4.3.3 to achieve > SNI-Extension support with JDK1.7. We have to re-factor several deprecated > methods and classes in 4.2.3, such as changing > PoolingClientConnectionManager to PoolingHttpClientConnectionManager, > changing DefaultHttpClient to CloseableHttpClient. > > However, I am running into an issue that connection is not being released > and eventually execute() got the exception in waiting for a connection > (Exception Text:org.apache.http.conn.ConnectionPoolTimeoutException: > Timeout waiting for connection from pool). > > Below is the code sniplet in a worker thread. > > The 4.2.3 code > > m_httpClient = new DefaultHttpClient(m_connectionManager, params); > ... > m_httpResponse = m_httpClient.execute(m_httpCommand, localContext); > ... > BasicManagedEntity entity = > (BasicManagedEntity)m_httpResponse.getEntity(); > if (entity!= null) { > m_inputStream = entity.getContent(); > } > .. process inputStream ... > if (m_inputStream != null) { > m_inputStream.close(); > } > > m_httpClient.getConnectionManager().closeIdleConnections(5, > TimeUnit.SECONDS); > > The 4.3.3 code > ... > m_httpClient = HttpClients.custom() > .setConnectionManager(m_connectionManager) > .setDefaultCredentialsProvider(credentialsProvider) > .setDefaultRequestConfig(m_defaultRequestConfig) > .disableCookieManagement() > .disableRedirectHandling() > .disableAutomaticRetries() > .setKeepAliveStrategy(keepAliveStrategy) > .build(); > ... > m_httpResponse = m_httpClient.execute(m_httpCommand); > ... > m_entity = (HttpEntity)m_httpResponse.getEntity(); > if (m_entity!= null) { > m_inputStream = m_entity.getContent(); > } > .. process inputStream ... > try { > EntityUtils.consume(m_entity); > } catch (IOException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } > ... > try { > if (m_inputStream != null) { > m_inputStream.close(); > } > > } catch (IOException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } > ... > try { > m_httpResponse.close(); > } catch (IOException e) { > // TODO Auto-generated catch block > e.printStackTrace(); > } > > (I've followed the instruction in this email. > http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/201211.mbox/%3C1353079080.20463.12.camel%40ubuntu%3Eto > close the connection.) > > The 4.2.3 code works flawlessly. The number of leased connection always > goes back to zero. But in the 4.3.3 code, the number of leased connection > gradually increased. > > I added IdleConnectionMonitorThread class as in > http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/connmgmt.html#d5e405It > did help reducing the number of leased > connection, but the number of leased connections still gradually increased. > > Hence, here are my questions: > > 1. How would I track down this connection leakage? Connection context logging would probably be what I would use facing the same problem. See section on context logging for connection management / request execution: http://hc.apache.org/httpcomponents-client-4.3.x/logging.html > 2. Is there a suggestion or best practice to migrate the deprecated classes > and methods in 4.2.3 to the current ones in 4.3.3? There is no migration guide as such. HttpClient tutorial should be the most complete source of information on HttpClient best practices. http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/index.html > 3. Is a separate thread, IdleConnectionMonitorThread, required to clean up > the leased connection in 4.3.3? If so, why is it not needed in 4.2.3? > IdleConnectionMonitorThread does not touch leased connections in any form or fashion. It works with available (idle) connections. It has no relevance for connection lease / release cycle, which is entirely in the consumer's responsibility. Oleg --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org