2 Minutes To Code (β Version)

Apache Commons Http Client - 2 minute tutorial

Efficient usage : How to efficiently use http client from multiple threads

This example shows how to set-up http client for use in a multi-threaded scenario efficiently.

The http-client framework allows one to configure the MultiThreadedHttpConnectionManager with the global maximum number of http conenctions that it can pool and also the host-level maximum number of http conenctions that it can keep in pool through methods setMaxTotalConnections and setDefaultMaxConnectionsPerHost respectively of class org.apache.commons.httpclient.params.HttpConnectionManagerParams. Apart from this , the global values of connection timeout and read timeout can be set.

In addition to this, the commons http-client framework, also gives the flexibility to configure the maximum number of connections and timeouts at specific host level through the class org.apache.commons.httpclient.HostConfiguration as shown below.
package com.techfundaes.httpBag;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpHost;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HostParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpConnectionParams;

public class MultiThreadHttpEfficient
{
	static HttpClient httpClient = null;
	
	public static void main(String[] args)
	{
		MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
		HttpConnectionManagerParams params = new HttpConnectionManagerParams();
			params.setDefaultMaxConnectionsPerHost(5);
			params.setMaxTotalConnections(50);
			params.setConnectionTimeout(5000);
			params.setSoTimeout(5000);
			
		HostConfiguration amazonHostConfig = new HostConfiguration();
			amazonHostConfig.setHost(new HttpHost("www.amazon.com", 80));
			HostParams hostParams = new HostParams();
			hostParams.setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000);
			hostParams.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000);
			amazonHostConfig.setParams(hostParams);
		
		params.setMaxConnectionsPerHost(amazonHostConfig, 20);
		httpConnectionManager.setParams(params);
		httpClient = new HttpClient(httpConnectionManager);
		httpClient.getHostConfiguration().setProxy("proxyHostName", 80);
	
		for(int i = 0; i < 100; i++)
		{
			WorkerThread workerThread = new WorkerThread();
			workerThread.start();
		}
	}
}