Saturday, August 29, 2009

Commons Apache Http Client (Disable connection pooling)

Commons Apache Http Client (Disable connection pooling)

In some cases we want to disable default connection pooling of http client e.g. in case when your application is using ThreadContextClassLoader so statically managed http connection pool will not help instead it will take the pool some time to remove its single connection because its hoping that someone else will try to reuse it. Turn-off the pool, otherwise your http Server will have more unused sockets opened than required for more duration.

Note that close() method does not close underlying http connection instead it’s responsibility of connection manager to close or reuse in releaseConnection().

Use org.apache.commons.httpclient.SimpleHttpConnectionManager which physically closes http connection on close().

HttpClient client = new HttpClient(new SimpleHttpConnectionManager(true));

No comments: