You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/05/07 12:18:11 UTC

svn commit: r535835 - /jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java

Author: olegk
Date: Mon May  7 03:18:07 2007
New Revision: 535835

URL: http://svn.apache.org/viewvc?view=rev&rev=535835
Log:
* Fixed bug in the stale connection check
* Tweaked the process of connection allocation for a slightly better readability

Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java?view=diff&rev=535835&r1=535834&r2=535835
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/client/DefaultClientRequestDirector.java Mon May  7 03:18:07 2007
@@ -48,6 +48,7 @@
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.conn.BasicManagedEntity;
 import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.ConnectionPoolTimeoutException;
 import org.apache.http.conn.HttpRoute;
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.RouteDirector;
@@ -144,22 +145,15 @@
     public HttpResponse execute(RoutedRequest roureq, HttpContext context)
         throws HttpException, IOException {
 
-        //@@@ link parameters? Let's rely on the request executor for now.
-
         HttpResponse response = null;
         boolean done = false;
 
-        //@@@ where to put the retry handling and counter?
-        //@@@ here, or retry in a calling method, or retry in an inner loop?
-        //@@@ Retry in a calling method would allow for selecting an alternate
-        //@@@ proxy, but it would not be aware of redirects that have already
-        //@@@ been followed. Would the retry counter apply for each request
-        //@@@ if redirects are followed, or for the whole sequence?
-
         try {
             int execCount = 0;
             while (!done) {
-                allocateConnection(roureq.getRoute());
+                if (managedConn == null) {
+                    managedConn = allocateConnection(roureq.getRoute());
+                }
                 establishRoute(roureq.getRoute(), context);
 
                 context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST,
@@ -176,22 +170,22 @@
 
                 context.setAttribute(HttpExecutionContext.HTTP_REQUEST,
                         prepreq);
+
+                if (HttpConnectionParams.isStaleCheckingEnabled(params)) {
+                    // validate connection
+                    LOG.debug("Stale connection check");
+                    if (managedConn.isStale() || execCount == 1) {
+                        LOG.debug("Stale connection detected");
+                        managedConn.close();
+                        continue;
+                    }
+                }
                 
                 execCount++;
                 try {
                     if (LOG.isDebugEnabled()) {
-                        LOG.debug("Attempt number " + execCount + " to execute request");
+                        LOG.debug("Attempt " + execCount + " to execute request");
                     }
-
-                    if (HttpConnectionParams.isStaleCheckingEnabled(params)) {
-                        // validate connection
-                        LOG.debug("Stale connection check");
-                        if (managedConn.isStale()) {
-                            LOG.debug("Stale connection detected");
-                            managedConn.close();
-                        }
-                    }
-                    
                     response = requestExec.execute(prepreq, managedConn, context);
                     
                 } catch (IOException ex) {
@@ -248,17 +242,11 @@
      *
      * @throws HttpException    in case of a problem
      */
-    protected void allocateConnection(HttpRoute route)
-        throws HttpException, IOException {
-
-        // we assume that the connection would have been released
-        // if it was not appropriate for the route of the followup
-        if (managedConn != null) {
-            return;
-        }
+    protected ManagedClientConnection allocateConnection(HttpRoute route)
+        throws HttpException, ConnectionPoolTimeoutException {
 
         long timeout = HttpClientParams.getConnectionManagerTimeout(params);
-        managedConn = connManager.getConnection(route, timeout);
+        return connManager.getConnection(route, timeout);
 
     } // allocateConnection