legacy-knowledge-base
Publicado Sep. 25, 2010

ConnectionPoolTimeoutException: Timeout waiting for connection from pool exception when using HttpUtil.URLtoString method from a custom development

written-by

Jorge Diaz

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

legacy-article

learn-legacy-article-disclaimer-text

Issue

We are using Liferay URLtoString  method from  com.liferay.portal.kernel.util.HttpUtil in our custom development to call an external web service

We are getting ConnectionPoolTimeoutException: Timeout waiting for connection from pool  exception when our custom development calls the external web service:

java.io.IOException: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
    at com.liferay.portal.util.HttpImpl.URLtoInputStream(HttpImpl.java:1902)
    at com.liferay.portal.util.HttpImpl.URLtoByteArray(HttpImpl.java:1588)
    at com.liferay.portal.util.HttpImpl.URLtoByteArray(HttpImpl.java:1178)
    at com.liferay.portal.util.HttpImpl.URLtoString(HttpImpl.java:1240)
    at com.liferay.portal.kernel.util.HttpUtil.URLtoString(HttpUtil.java:315)

[...omitted...]

    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:313)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:279)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:191)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at com.liferay.portal.util.HttpImpl.URLtoInputStream(HttpImpl.java:1783)
    ... 235 more

How can we avoid this error?

Environment

  • Any Liferay version

Resolution

The HttpUtil class from Liferay core internally makes use of the Apache HttpClient library.
By default this Apache HttpClient library is configured with the following configuration:
  • max. connections per host: 2
  • max total connections: 20
  • timeout: 10 seconds
So if there are more than 2 concurrent calls to a host or 20 total calls using the HttpClient, the error you report occurs:
Caused by: org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:313)
 
In order to avoid the problem in your custom development, the solution would be to modify the values configured for the HttpClient library, this can be done from your portal-ext.properties file through the properties:
com.liferay.portal.util.HttpImpl.max.connections.per.host=2
com.liferay.portal.util.HttpImpl.max.total.connections=20
com.liferay.portal.util.HttpImpl.timeout=10000
 
Note that the properties com.liferay.portal.util.HttpImpl.max.connections.per.host and com.liferay.portal.util.HttpImpl.timeout can be specified for a particular host, for example, if we want to restrict to www.google.com:
com.liferay.portal.util.HttpImpl.max.connections.per.host[www.google.com]=2
 
There is more information about how to configure these properties in the following page:
Warning: these properties will affect to all places where Liferay uses the HttpUtil utility so be careful and if you increase it, verify that the new value does not affect the overall performance of your system.

Additional Information

 

did-this-article-resolve-your-issue

legacy-knowledge-base