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 eviction policy ===
  
+     One of the major shortcoming of the classic blocking I/O model is that 
the network socket can react to I/O events only when blocked in an I/O 
operation. When a connection is released back to the manager, it can be kept 
alive however it is unable to monitor the status of the socket and react to any 
I/O events. If the connection gets closed on the server side, the client side 
connection is unable to detect the change in the connection state and react 
appropriately by closing the socket on its end. 
+ 
+     HttpClient tries to mitigate the problem by testing whether the 
connection is 'stale', that is no longer valid because it was closed on the 
server side. The stale connection check is not 100% reliable and adds 10 to 30 
ms overhead to each request execution. The only feasible solution that does not 
involve a one thread per socket model for idle connections is a dedicated 
monitor thread used to evict connections that are considered expired due to a 
long period of inactivity. The monitor thread can periodically call 
ClientConnectionManager#closeExpiredConnections() method to close all expired 
connections and evict closed connections from the pool. It can also optionally 
call ClientConnectionManager#closeIdleConnections() method to close all 
connections that have been idle over a given period of time. 
+     
+ {{{
+ public static class IdleConnectionMonitorThread extends Thread {
+     
+     private final ClientConnectionManager connMgr;
+     private volatile boolean shutdown;
+     
+     public IdleConnectionMonitorThread(ClientConnectionManager connMgr) {
+         super();
+         this.connMgr = connMgr;
+     }
+ 
+     @Override
+     public void run() {
+         try {
+             while (!shutdown) {
+                 synchronized (this) {
+                     wait(5000);
+                     // Close expired connections
+                     connMgr.closeExpiredConnections();
+                     // Optionally, close connections
+                     // that have been idle longer than 30 sec
+                     connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
+                 }
+             }
+         } catch (InterruptedException ex) {
+             // terminate
+         }
+     }
+     
+     public void shutdown() {
+         shutdown = true;
+         synchronized (this) {
+             notifyAll();
+         }
+     }
+     
+ }
+ }}}
+     
  === Connection keep alive strategy ===
+ 
+     The HTTP specification does not specify how long a persistent connection 
may be and should be kept alive. Some HTTP servers use non-standard 
{{{Keep-Alive}}} header to communicate to the client the period of time in 
seconds they intend to keep the connection alive on the server side. HttpClient 
makes use of this information if available. If the {{{Keep-Alive}}} header is 
not present in the response, HttpClient assumes the connection can be kept 
alive indefinitely. However, many HTTP servers out there are configured to drop 
persistent connections after a certain period of inactivity in order to 
conserve system resources, quite often without informing the client. In case 
the default strategy turns out to be too optimistic, one may want to provide a 
custom keep-alive strategy.
+ 
+ {{{
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
+ 
+     public long getKeepAliveDuration(HttpResponse response, HttpContext 
context) {
+         // Honor 'keep-alive' header
+         HeaderElementIterator it = new BasicHeaderElementIterator(
+                 response.headerIterator(HTTP.CONN_KEEP_ALIVE));
+         while (it.hasNext()) {
+             HeaderElement he = it.nextElement();
+             String param = he.getName(); 
+             String value = he.getValue();
+             if (value != null && param.equalsIgnoreCase("timeout")) {
+                 try {
+                     return Long.parseLong(value) * 1000;
+                 } catch(NumberFormatException ignore) {
+                 }
+             }
+         }
+         HttpHost target = (HttpHost) context.getAttribute(
+                 ExecutionContext.HTTP_TARGET_HOST);
+         if ("www.naughty-server.com".equalsIgnoreCase(target.getHostName())) {
+             // Keep alive for 5 seconds only
+             return 5 * 1000;
+         } else {
+             // otherwise keep alive for 30 seconds
+             return 30 * 1000;
+         }
+     }
+     
+ });
+ }}}
  
  = HTTP state management =
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to