Implemented HTTP request retry handling

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@535794 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-05-07 09:07:44 +00:00
parent 4a6d656ee2
commit c3dcd7651b
3 changed files with 110 additions and 18 deletions

View File

@ -44,6 +44,7 @@ import org.apache.http.HttpResponseInterceptor;
import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.auth.AuthSchemeRegistry;
import org.apache.http.client.ClientRequestDirector; import org.apache.http.client.ClientRequestDirector;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.HttpState; import org.apache.http.client.HttpState;
import org.apache.http.client.RoutedRequest; import org.apache.http.client.RoutedRequest;
import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManager;
@ -93,6 +94,9 @@ public abstract class AbstractHttpClient
/** The HTTP processor. */ /** The HTTP processor. */
private BasicHttpProcessor httpProcessor; private BasicHttpProcessor httpProcessor;
/** The request retry handler. */
private HttpRequestRetryHandler retryHandler;
/** The default HTTP state. */ /** The default HTTP state. */
private HttpState defaultState; private HttpState defaultState;
@ -131,6 +135,9 @@ public abstract class AbstractHttpClient
protected abstract BasicHttpProcessor createHttpProcessor(); protected abstract BasicHttpProcessor createHttpProcessor();
protected abstract HttpRequestRetryHandler createHttpRequestRetryHandler();
protected abstract HttpState createHttpState(); protected abstract HttpState createHttpState();
@ -209,6 +216,19 @@ public abstract class AbstractHttpClient
} }
public synchronized final HttpRequestRetryHandler getHttpRequestRetryHandler() {
if (retryHandler == null) {
retryHandler = createHttpRequestRetryHandler();
}
return retryHandler;
}
public synchronized void setHttpRequestRetryHandler(final HttpRequestRetryHandler retryHandler) {
this.retryHandler = retryHandler;
}
public synchronized final HttpState getState() { public synchronized final HttpState getState() {
if (defaultState == null) { if (defaultState == null) {
defaultState = createHttpState(); defaultState = createHttpState();
@ -367,6 +387,7 @@ public abstract class AbstractHttpClient
getConnectionManager(), getConnectionManager(),
getConnectionReuseStrategy(), getConnectionReuseStrategy(),
getHttpProcessor().copy(), getHttpProcessor().copy(),
getHttpRequestRetryHandler(),
getParams()); getParams());
} }

View File

@ -33,6 +33,8 @@ package org.apache.http.impl.client;
import java.io.IOException; import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.ConnectionReuseStrategy; import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpException; import org.apache.http.HttpException;
@ -40,22 +42,23 @@ import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.client.ClientRequestDirector; import org.apache.http.client.ClientRequestDirector;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.RoutedRequest; import org.apache.http.client.RoutedRequest;
import org.apache.http.client.methods.AbortableHttpRequest; import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.BasicManagedEntity; import org.apache.http.conn.BasicManagedEntity;
import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpRoute; import org.apache.http.conn.HttpRoute;
import org.apache.http.conn.ManagedClientConnection; import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.RouteDirector; import org.apache.http.conn.RouteDirector;
import org.apache.http.message.BasicHttpRequest; import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpExecutionContext; import org.apache.http.protocol.HttpExecutionContext;
import org.apache.http.protocol.HttpProcessor; import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor; import org.apache.http.protocol.HttpRequestExecutor;
/** /**
* Default implementation of a client-side request director. * Default implementation of a client-side request director.
* <br/> * <br/>
@ -72,6 +75,8 @@ import org.apache.http.protocol.HttpRequestExecutor;
public class DefaultClientRequestDirector public class DefaultClientRequestDirector
implements ClientRequestDirector { implements ClientRequestDirector {
private static final Log LOG = LogFactory.getLog(DefaultClientRequestDirector.class);
/** The connection manager. */ /** The connection manager. */
protected final ClientConnectionManager connManager; protected final ClientConnectionManager connManager;
@ -84,19 +89,45 @@ public class DefaultClientRequestDirector
/** The HTTP protocol processor. */ /** The HTTP protocol processor. */
protected final HttpProcessor httpProcessor; protected final HttpProcessor httpProcessor;
/** The request retry handler. */
protected final HttpRequestRetryHandler retryHandler;
/** The HTTP parameters. */
protected final HttpParams params;
/** The currently allocated connection. */ /** The currently allocated connection. */
protected ManagedClientConnection managedConn; protected ManagedClientConnection managedConn;
public DefaultClientRequestDirector(ClientConnectionManager conman, public DefaultClientRequestDirector(
ConnectionReuseStrategy reustrat, final ClientConnectionManager conman,
HttpProcessor httpProcessor, final ConnectionReuseStrategy reustrat,
HttpParams params) { final HttpProcessor httpProcessor,
final HttpRequestRetryHandler retryHandler,
final HttpParams params) {
if (conman == null) {
throw new IllegalArgumentException("Client connection manager may not be null");
}
if (reustrat == null) {
throw new IllegalArgumentException("Connection reuse strategy may not be null");
}
if (httpProcessor == null) {
throw new IllegalArgumentException("HTTP protocol processor may not be null");
}
if (retryHandler == null) {
throw new IllegalArgumentException("HTTP request retry handler may not be null");
}
if (params == null) {
throw new IllegalArgumentException("HTTP parameters may not be null");
}
this.connManager = conman; this.connManager = conman;
this.reuseStrategy = reustrat; this.reuseStrategy = reustrat;
this.requestExec = new HttpRequestExecutor(params);
this.httpProcessor = httpProcessor; this.httpProcessor = httpProcessor;
this.retryHandler = retryHandler;
this.params = params;
this.requestExec = new HttpRequestExecutor(params);
this.managedConn = null; this.managedConn = null;
//@@@ authentication? //@@@ authentication?
@ -126,6 +157,7 @@ public class DefaultClientRequestDirector
//@@@ if redirects are followed, or for the whole sequence? //@@@ if redirects are followed, or for the whole sequence?
try { try {
int execCount = 0;
while (!done) { while (!done) {
allocateConnection(roureq.getRoute()); allocateConnection(roureq.getRoute());
establishRoute(roureq.getRoute(), context); establishRoute(roureq.getRoute(), context);
@ -145,7 +177,40 @@ public class DefaultClientRequestDirector
context.setAttribute(HttpExecutionContext.HTTP_REQUEST, context.setAttribute(HttpExecutionContext.HTTP_REQUEST,
prepreq); prepreq);
response = requestExec.execute(prepreq, managedConn, context); execCount++;
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Attempt number " + 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) {
LOG.debug("Closing the connection.");
managedConn.close();
if (retryHandler.retryRequest(ex, execCount, context)) {
if (LOG.isInfoEnabled()) {
LOG.info("I/O exception ("+ ex.getClass().getName() +
") caught when processing request: "
+ ex.getMessage());
}
if (LOG.isDebugEnabled()) {
LOG.debug(ex.getMessage(), ex);
}
LOG.info("Retrying request");
continue;
}
throw ex;
}
finalizeResponse(response, context); finalizeResponse(response, context);
@ -184,15 +249,16 @@ public class DefaultClientRequestDirector
* @throws HttpException in case of a problem * @throws HttpException in case of a problem
*/ */
protected void allocateConnection(HttpRoute route) protected void allocateConnection(HttpRoute route)
throws HttpException { throws HttpException, IOException {
// we assume that the connection would have been released // we assume that the connection would have been released
// if it was not appropriate for the route of the followup // if it was not appropriate for the route of the followup
if (managedConn != null) if (managedConn != null) {
return; return;
}
//@@@ use connection manager timeout long timeout = HttpClientParams.getConnectionManagerTimeout(params);
managedConn = connManager.getConnection(route); managedConn = connManager.getConnection(route, timeout);
} // allocateConnection } // allocateConnection
@ -439,9 +505,7 @@ public class DefaultClientRequestDirector
if (success) { if (success) {
// Not in exception handling, there probably is a response. // Not in exception handling, there probably is a response.
// The connection is in or can be brought to a re-usable state. // The connection is in or can be brought to a re-usable state.
boolean reuse = false; boolean reuse = reuseStrategy.keepAlive(response, context);
if (reuseStrategy != null)
reuse = reuseStrategy.keepAlive(response, context);
// check for entity, release connection if possible // check for entity, release connection if possible
if ((response == null) || (response.getEntity() == null) || if ((response == null) || (response.getEntity() == null) ||
@ -462,9 +526,10 @@ public class DefaultClientRequestDirector
//@@@ for now, just shut it down //@@@ for now, just shut it down
try { try {
mcc.abortConnection(); mcc.abortConnection();
} catch (IOException ignore) { } catch (IOException ex) {
// can't allow exception while handling exception if (LOG.isDebugEnabled()) {
//@@@ log as debug or warning? LOG.debug(ex.getMessage(), ex);
}
} }
} }
} // cleanupConnection } // cleanupConnection

View File

@ -37,6 +37,7 @@ import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.auth.AuthSchemeRegistry;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.HttpState; import org.apache.http.client.HttpState;
import org.apache.http.client.RoutedRequest; import org.apache.http.client.RoutedRequest;
import org.apache.http.client.params.AuthPolicy; import org.apache.http.client.params.AuthPolicy;
@ -205,6 +206,11 @@ public class DefaultHttpClient extends AbstractHttpClient {
} }
protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
return new DefaultHttpRequestRetryHandler();
}
protected HttpState createHttpState() { protected HttpState createHttpState() {
return new HttpState(); return new HttpState();
} }