hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Httpcomponents Wiki] Update of "HttpClientTutorial" by OlegKalnichevski
Date Sun, 26 Apr 2009 18:40:20 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Httpcomponents Wiki" for change notification.

The following page has been changed by OlegKalnichevski:
http://wiki.apache.org/HttpComponents/HttpClientTutorial

------------------------------------------------------------------------------
  }
  }}}
     
+ === Connection manager parameters ===
+ 
+     These are parameters that be used to customize standard HTTP connection manager implementations:
+ 
+     * '''http.conn-manager.timeout''': Defines the timeout in milliseconds used when retrieving
an instance of ManagedClientConnection from the ClientConnectionManager This parameter expects
a value of type java.lang.Long.
+ 
+     * '''http.conn-manager.max-per-route''': Defines the maximum number of connections per
route. This limit is interpreted by client connection managers and applies to individual manager
instances. This parameter expects a value of type ConnPerRoute.
+ 
+     * '''http.conn-manager.max-total''': Defines the maximum number of connections in total.
This limit is interpreted by client connection managers and applies to individual manager
instances. This parameter expects a value of type java.lang.Integer.
+    
  === Simple connection manager ===
  
      SingleClientConnManager is a simple connection manager that maintains only one connection
at a time. Even though this class is thread-safe it ought to be used by one execution thread
only. SingleClientConnManager will make an effort to reuse the connection for subsequent requests
with the same route. It will, however, close the existing connection and open it for the given
route, if the route of the persistent connection does not match that of the connection request.
If the connection has been already been allocated IllegalStateException is thrown. 
@@ -945, +955 @@

  ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
  HttpClient httpClient = new DefaultHttpClient(cm, params);
  }}}
+ 
+ === Connection manager shutdown ===
+ 
+     When an HttpClient instance is no longer needed and is about to go out of scope it is
important to shut down its connection manager to ensure that all connections kept alive by
the manager get closed and system resources allocated by those connections are released.
+ 
+ {{{
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ HttpGet httpget = new HttpGet("http://www.google.com/");
+ HttpResponse response = httpclient.execute(httpget);
+ HttpEntity entity = response.getEntity();
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+     entity.consumeContent();
+ }
+ httpclient.getConnectionManager().shutdown();
+ }}}
+ 
+ === Multithreaded request execution ===
+ 
+     When equipped with a pooling connection manager such as ThreadSafeClientConnManager
HttpClient can be used to execute multiple requests simultaneously using multiple threads
of execution. DefaultHttpClient plus ThreadSafeClientConnManager is fully thread safe.
+ 
+     ThreadSafeClientConnManager will allocate connections based on its configuration. If
all connections for a given route has already been leased, a request for connection will block
until a connection is released back to the pool. One can ensure the connection manager does
not block indefinitely in the connection request operation by setting 'http.conn-manager.timeout'
to a positive value. If the connection request cannot be serviced within the given time period
ConnectionPoolTimeoutException will be thrown.
+ 
+ {{{
+ HttpParams params = new BasicHttpParams();
+ SchemeRegistry schemeRegistry = new SchemeRegistry();
+ schemeRegistry.register(
+         new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
+ 
+ ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
+ HttpClient httpClient = new DefaultHttpClient(cm, params);
+ 
+ // URIs to perform GETs on
+ String[] urisToGet = {
+     "http://www.domain1.com/",
+     "http://www.domain2.com/",
+     "http://www.domain3.com/",
+     "http://www.domain4.com/"
+ };
+ 
+ // create a thread for each URI
+ GetThread[] threads = new GetThread[urisToGet.length];
+ for (int i = 0; i < threads.length; i++) {
+     HttpGet httpget = new HttpGet(urisToGet[i]);
+     threads[i] = new GetThread(httpClient, httpget);
+ }
+ 
+ // start the threads
+ for (int j = 0; j < threads.length; j++) {
+     threads[j].start();
+ }
+ 
+ // join the threads
+ for (int j = 0; j < threads.length; j++) {
+     threads[j].join();
+ }
+ 
+ }}}
+ 
+ {{{
+ static class GetThread extends Thread {
+     
+     private final HttpClient httpClient;
+     private final HttpContext context;
+     private final HttpGet httpget;
+     
+     public GetThread(HttpClient httpClient, HttpGet httpget) {
+         this.httpClient = httpClient;
+         this.context = new BasicHttpContext();
+         this.httpget = httpget;
-     
+     }
- == Stateful connections ==
- 
-   NTLM connections. SSL connections with client authentication.
+     
+     @Override
+     public void run() {
+         try {
+             HttpResponse response = this.httpClient.execute(this.httpget, this.context);
+             HttpEntity entity = response.getEntity();
+             if (entity != null) {
+                 // do something useful with the entity
+                 // ...
+                 // ensure the connection gets released to the manager
+                 entity.consumeContent();
+             }
+         } catch (Exception ex) {
+             this.httpget.abort();
+         }
+     }
-   
+    
+ }
+ }}}
+ 
+ === Connection eviction policy ===
+ 
+ === Connection keep alive strategy ===
+ 
  = HTTP state management =
  
  == HTTP cookies ==
@@ -1028, +1128 @@

  
    Custom redirect handling.
  
+ = Advanced topics =
+ 
+ == Stateful connections ==
+ 
+   NTLM connections. SSL connections with client authentication.
+ 
+ == Custom client connections ==
+ 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org


Mime
View raw message