httpclient4.x the concept of no connection pool before, the number of requests will be established how many Io, in the event of a large number of access to the server IO may be exhausted. The newest HttpClient4.2 has the connection pool thing inside, uses the Poolingclientconnectionmanager, can refer to the official document specifically, inside has the detailed introduction.
The original article about Poolingclientconnectionmanager is this:
Poolingclientconnectionmanager is a more complex implementation that manages a pool of client connections and are able to s Ervice connection requests from multiple execution threads. Connections are pooled on a/route basis. A request for a route to which the manager already has a persistent connection available in the pool would be serviced by Leasing a connection from the pool rather than creating a brand new connection.
Poolingclientconnectionmanager maintains a maximum limit of connections on a per-route basis and at total. Per default This implementation'll create no more than 2 concurrent connections/given route and no more connectio NS in total. For many real-world applications such limits may prove too constraining, especially if they use HTTP as a transport proto Col for their services. Connection limits can be adjusted using the appropriate HTTP parameters.
Example:
Httpconnectionmanager.java
public class Httpconnectionmanager {private static httpparams httpparams;
private static Poolingclientconnectionmanager cm;
/** * Maximum Connection number * * Public final static int max_total_connections = 200;
/** * Get the maximum waiting time for the connection/public final static int wait_timeout = 60000;
/** * The maximum number of connections per route * * Public final static int max_route_connections = 300;
/** * Connection Timeout/public final static int connect_timeout = 10000;
/** * Read Timeout/public final static int read_timeout = 10000;
static {Schemeregistry schemeregistry = new Schemeregistry ();
Schemeregistry.register (New Scheme ("http", 80,plainsocketfactory.getsocketfactory ());
Schemeregistry.register (New Scheme ("https", 443, Sslsocketfactory.getsocketfactory ());
CM = new Poolingclientconnectionmanager (schemeregistry);
Cm.setmaxtotal (200);
Cm.setdefaultmaxperroute (80);
Httpparams params = new Basichttpparams ();
Params.setparameter (coreconnectionpnames.connection_timeout,connect_timeout); ParaMs.setparameter (Coreconnectionpnames.so_timeout, read_timeout);
public static HttpClient Gethttpclient () {return new defaulthttpclient (cm, httpparams); }
}
Downloadtask.java
Import java.io.IOException;
Import Java.io.InputStream;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.HttpStatus;
Import org.apache.http.client.ClientProtocolException;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.params.CoreConnectionPNames; public class Downloadtask implements Runnable {private HttpClient httpclient = Httpconnectionpoolmanager.gethttpclient (
);
@Override public void Run () {String URL = "Http://www.baidu.com";
Download (URL);
public void download (String url) {HttpGet httpget = null;
InputStream in = null;
try {httpget = new HttpGet (URL); Httpget.setheader ("User-agent", "mozilla/5.0" (Windows NT 6.1;
WOW64) applewebkit/537.1 (khtml, like Gecko) chrome/21.0.1180.79 safari/537.1 ");
Httpget.setheader ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); Httpget.getparams (). Setparameter (Coreconnectionpnames.so_timeout,1000 * 20);
HttpResponse resp = Httpclient.execute (HttpGet);
int respcode = Resp.getstatusline (). Getstatuscode ();
if (Respcode = = HTTPSTATUS.SC_OK) {httpentity entity = resp.getentity ();
if (entity!= null) {in = Entity.getcontent ();
Sleep (1000);
SYSTEM.OUT.PRINTLN ("The request succeeded ...") + Thread.CurrentThread (). GetName () + URL);
} else {System.out.println ("request failed ..." + thread.currentthread (). GetName () + URL);
if (httpget!=null) Httpget.abort ();
} catch (Clientprotocolexception e) {e.printstacktrace ();
if (httpget!=null) Httpget.abort ();
catch (IOException e) {e.printstacktrace ();
if (httpget!=null) Httpget.abort ();
catch (Exception e) {//Capture the largest exception e.printstacktrace ();
if (httpget!=null) Httpget.abort ();
Finally {if (in!= null) {try {in.close ();
catch (IOException e) {e.printstacktrace (); }}} public VOID sleep (int time) throws Interruptedexception {Thread.Sleep (time);
}
}
Test class
public class Test {
/**
* @param args
*
/public static void main (string[] args) {for
(int i=0;i<8; i++) {
new Thread (New Downloadtask ()). Start ();