Issue 69: corrected retry/error guice bindings

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1463 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-26 12:34:28 +00:00
parent 383b9bf2c6
commit 44d4c37a90
8 changed files with 74 additions and 41 deletions

View File

@ -24,15 +24,16 @@
package org.jclouds.http; package org.jclouds.http;
/** /**
* // TODO: Adrian: Document this! * Responsible for setting an exception on the command relevant to the unrecoverable error in the
* HttpResponse.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public interface HttpErrorHandler { public interface HttpErrorHandler {
public static final HttpErrorHandler NOOP = new HttpErrorHandler() { public static final HttpErrorHandler NOOP = new HttpErrorHandler() {
public void handleError(HttpFutureCommand<?> command, HttpResponse response) { public void handleError(HttpFutureCommand<?> command, HttpResponse response) {
} }
}; };
void handleError(HttpFutureCommand<?> command, HttpResponse response); void handleError(HttpFutureCommand<?> command, HttpResponse response);
} }

View File

@ -40,20 +40,26 @@ import com.google.inject.Inject;
*/ */
public class DelegatingErrorHandler implements HttpErrorHandler { public class DelegatingErrorHandler implements HttpErrorHandler {
@VisibleForTesting
@Inject(optional=true)
@Redirection @Redirection
@Inject(optional = true) HttpErrorHandler redirectionHandler;
@VisibleForTesting
HttpErrorHandler redirectionHandler = new CloseContentAndSetExceptionErrorHandler();
@VisibleForTesting
@Inject(optional=true)
@ClientError @ClientError
@Inject(optional = true) HttpErrorHandler clientErrorHandler;
@VisibleForTesting
HttpErrorHandler clientErrorHandler = new CloseContentAndSetExceptionErrorHandler();
@ServerError
@Inject(optional = true)
@VisibleForTesting @VisibleForTesting
HttpErrorHandler serverErrorHandler = new CloseContentAndSetExceptionErrorHandler(); @Inject(optional=true)
@ServerError
HttpErrorHandler serverErrorHandler;
public DelegatingErrorHandler() {
this.redirectionHandler = new CloseContentAndSetExceptionErrorHandler();
this.clientErrorHandler = redirectionHandler;
this.serverErrorHandler = redirectionHandler;
}
public void handleError(HttpFutureCommand<?> command, org.jclouds.http.HttpResponse response) { public void handleError(HttpFutureCommand<?> command, org.jclouds.http.HttpResponse response) {
int statusCode = response.getStatusCode(); int statusCode = response.getStatusCode();

View File

@ -29,6 +29,7 @@ import org.jclouds.http.annotation.ClientError;
import org.jclouds.http.annotation.Redirection; import org.jclouds.http.annotation.Redirection;
import org.jclouds.http.annotation.ServerError; import org.jclouds.http.annotation.ServerError;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
/** /**
@ -39,17 +40,26 @@ import com.google.inject.Inject;
*/ */
public class DelegatingRetryHandler implements HttpRetryHandler { public class DelegatingRetryHandler implements HttpRetryHandler {
@VisibleForTesting
@Inject(optional = true)
@Redirection @Redirection
@Inject(optional = true) HttpRetryHandler redirectionRetryHandler;
private HttpRetryHandler redirectionRetryHandler = new RedirectionRetryHandler(5);
@VisibleForTesting
@Inject(optional = true)
@ClientError @ClientError
@Inject(optional = true) HttpRetryHandler clientErrorRetryHandler;
private HttpRetryHandler clientErrorRetryHandler = new CannotRetryHandler();
@ServerError @VisibleForTesting
@Inject(optional = true) @Inject(optional = true)
private HttpRetryHandler serverErrorRetryHandler = new BackoffLimitedRetryHandler(5); @ServerError
HttpRetryHandler serverErrorRetryHandler;
public DelegatingRetryHandler() {
this.redirectionRetryHandler = new RedirectionRetryHandler(5);
this.clientErrorRetryHandler = new CannotRetryHandler();
this.serverErrorRetryHandler = new BackoffLimitedRetryHandler(5);
}
public boolean shouldRetryRequest(HttpFutureCommand<?> command, public boolean shouldRetryRequest(HttpFutureCommand<?> command,
org.jclouds.http.HttpResponse response) { org.jclouds.http.HttpResponse response) {

View File

@ -29,13 +29,11 @@ import java.util.List;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.HttpFutureCommand; import org.jclouds.http.HttpFutureCommand;
import org.jclouds.http.HttpFutureCommandClient; import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter; import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpRetryHandler;
import org.jclouds.http.handlers.DelegatingErrorHandler; import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler; import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
@ -44,17 +42,20 @@ import com.google.inject.Inject;
public abstract class BaseHttpFutureCommandClient<Q> implements HttpFutureCommandClient { public abstract class BaseHttpFutureCommandClient<Q> implements HttpFutureCommandClient {
private final DelegatingRetryHandler retryHandler;
private final DelegatingErrorHandler errorHandler;
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
@Inject(optional = true) @Inject(optional = true)
protected List<HttpRequestFilter> requestFilters = Collections.emptyList(); protected List<HttpRequestFilter> requestFilters = Collections.emptyList();
@Inject(optional = true) protected BaseHttpFutureCommandClient(DelegatingRetryHandler retryHandler,
private HttpRetryHandler retryHandler = new DelegatingRetryHandler(); DelegatingErrorHandler errorHandler) {
this.retryHandler = retryHandler;
@Inject(optional = true) this.errorHandler = errorHandler;
private HttpErrorHandler errorHandler = new DelegatingErrorHandler(); }
public void submit(HttpFutureCommand<?> command) { public void submit(HttpFutureCommand<?> command) {
HttpRequest request = command.getRequest(); HttpRequest request = command.getRequest();

View File

@ -37,6 +37,10 @@ import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpFutureCommandClient; import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import com.google.inject.Inject;
/** /**
* Basic implementation of a {@link HttpFutureCommandClient}. * Basic implementation of a {@link HttpFutureCommandClient}.
@ -45,6 +49,12 @@ import org.jclouds.http.HttpResponse;
*/ */
public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient<HttpURLConnection> { public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient<HttpURLConnection> {
@Inject
public JavaUrlHttpFutureCommandClient(DelegatingRetryHandler retryHandler,
DelegatingErrorHandler errorHandler) {
super(retryHandler, errorHandler);
}
@Override @Override
protected HttpResponse invoke(HttpURLConnection connection) throws IOException { protected HttpResponse invoke(HttpURLConnection connection) throws IOException {
HttpResponse response = new HttpResponse(); HttpResponse response = new HttpResponse();

View File

@ -30,7 +30,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
@ -41,6 +40,8 @@ import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpHeaders; import org.jclouds.http.HttpHeaders;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.internal.BaseHttpFutureCommandClient; import org.jclouds.http.internal.BaseHttpFutureCommandClient;
import com.google.appengine.api.urlfetch.FetchOptions; import com.google.appengine.api.urlfetch.FetchOptions;
@ -61,7 +62,9 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient<HTTPReque
private final URLFetchService urlFetchService; private final URLFetchService urlFetchService;
@Inject @Inject
public URLFetchServiceClient(URLFetchService urlFetchService) throws MalformedURLException { public URLFetchServiceClient(URLFetchService urlFetchService,
DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler) {
super(retryHandler, errorHandler);
this.urlFetchService = urlFetchService; this.urlFetchService = urlFetchService;
} }

View File

@ -43,6 +43,8 @@ import org.jclouds.http.HttpHeaders;
import org.jclouds.http.HttpMethod; import org.jclouds.http.HttpMethod;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters; import org.testng.annotations.Parameters;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -64,7 +66,9 @@ public class URLFetchServiceClientTest {
@BeforeTest @BeforeTest
void setupClient() throws MalformedURLException { void setupClient() throws MalformedURLException {
endPoint = URI.create("http://localhost:80"); endPoint = URI.create("http://localhost:80");
client = new URLFetchServiceClient(createNiceMock(URLFetchService.class)); client = new URLFetchServiceClient(createNiceMock(URLFetchService.class),
createNiceMock(DelegatingRetryHandler.class),
createNiceMock(DelegatingErrorHandler.class));
} }
@Test @Test

View File

@ -35,10 +35,8 @@ import org.apache.http.HttpResponse;
import org.apache.http.nio.entity.ConsumingNHttpEntity; import org.apache.http.nio.entity.ConsumingNHttpEntity;
import org.apache.http.nio.protocol.NHttpRequestExecutionHandler; import org.apache.http.nio.protocol.NHttpRequestExecutionHandler;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.HttpFutureCommand; import org.jclouds.http.HttpFutureCommand;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRetryHandler;
import org.jclouds.http.handlers.DelegatingErrorHandler; import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler; import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.httpnio.util.HttpNioUtils; import org.jclouds.http.httpnio.util.HttpNioUtils;
@ -53,27 +51,27 @@ import com.google.inject.Inject;
*/ */
public class HttpNioFutureCommandExecutionHandler implements NHttpRequestExecutionHandler { public class HttpNioFutureCommandExecutionHandler implements NHttpRequestExecutionHandler {
private final ExecutorService executor; private final ExecutorService executor;
@Resource
protected Logger logger = Logger.NULL;
private final ConsumingNHttpEntityFactory entityFactory; private final ConsumingNHttpEntityFactory entityFactory;
private final DelegatingRetryHandler retryHandler;
private final DelegatingErrorHandler errorHandler;
/** /**
* inputOnly: nothing is taken from this queue. * inputOnly: nothing is taken from this queue.
*/ */
private final BlockingQueue<HttpFutureCommand<?>> resubmitQueue; private final BlockingQueue<HttpFutureCommand<?>> resubmitQueue;
@Inject(optional = true) @Resource
private HttpRetryHandler retryHandler = new DelegatingRetryHandler(); protected Logger logger = Logger.NULL;
@Inject(optional = true)
private HttpErrorHandler errorHandler = new DelegatingErrorHandler();
@Inject @Inject
public HttpNioFutureCommandExecutionHandler(ConsumingNHttpEntityFactory entityFactory, public HttpNioFutureCommandExecutionHandler(ConsumingNHttpEntityFactory entityFactory,
ExecutorService executor, BlockingQueue<HttpFutureCommand<?>> resubmitQueue) { ExecutorService executor, BlockingQueue<HttpFutureCommand<?>> resubmitQueue,
DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler) {
this.executor = executor; this.executor = executor;
this.entityFactory = entityFactory; this.entityFactory = entityFactory;
this.resubmitQueue = resubmitQueue; this.resubmitQueue = resubmitQueue;
this.retryHandler = retryHandler;
this.errorHandler = errorHandler;
} }
public interface ConsumingNHttpEntityFactory { public interface ConsumingNHttpEntityFactory {