You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ri...@apache.org on 2007/12/18 19:37:36 UTC
svn commit: r605288 - in /geronimo/sandbox/AsyncHttpClient/src:
main/java/org/apache/ahc/AsyncHttpClient.java
test/java/org/apache/ahc/RetryTest.java
Author: rickmcguire
Date: Tue Dec 18 10:37:33 2007
New Revision: 605288
URL: http://svn.apache.org/viewvc?rev=605288&view=rev
Log:
GERONIMO-3617 AsyncHttpClient should support retries on connection failures
Added:
geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java (with props)
Modified:
geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=605288&r1=605287&r2=605288&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java Tue Dec 18 10:37:33 2007
@@ -88,12 +88,18 @@
/** The Default TCP No Delay. */
private static final boolean DEFAULT_TCP_NO_DELAY = false;
+
+ /** The default number of connection retries */
+ private static final int DEFAULT_CONNECTION_RETRIES = 0;
/** The SSL protocol. */
private String sslProtocol = DEFAULT_SSL_PROTOCOL;
/** The connection timeout. */
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
+
+ /** The default number of retries */
+ private int connectionRetries = DEFAULT_CONNECTION_RETRIES;
/** The connector. */
private final SocketConnector connector;
@@ -307,6 +313,27 @@
}
/**
+ * Returns the connection retry count set for this
+ * client.
+ *
+ * @return The current connection retry count.
+ */
+ public int getConnectionRetries() {
+ return connectionRetries;
+ }
+
+ /**
+ * Sets the number of retries that will be attempted
+ * on connection failures.
+ *
+ * @param retries The new retry count.
+ */
+ public void setConnectionRetries(int retries) {
+ connectionRetries = retries;
+ }
+
+
+ /**
* Instantiates a new AsyncHttpClient. It will use a single threaded model and is good for
* use in one-off connections.
*/
@@ -428,10 +455,25 @@
future = openConnection(message);
}
ResponseFuture response = message.getResponseFuture();
- future.addListener(new FutureListener(message, response));
+ future.addListener(new FutureListener(message, response, connectionRetries));
return response;
}
+ /**
+ * Retry a connection after a failure. This will
+ * create a new connection and try again.
+ *
+ * @param message The message request we're sending.
+ * @param response The response future for the message.
+ * @param retries The number of retries to make for the next connection
+ * attempt. This should be one less than the count
+ * used for the previous attempt.
+ */
+ private void retryConnection(HttpRequestMessage message, ResponseFuture response, int retries) {
+ ConnectFuture future = openConnection(message);
+ future.addListener(new FutureListener(message, response, retries));
+ }
+
private ConnectFuture openConnection(HttpRequestMessage message) {
return connector.connect(new InetSocketAddress(message.getHost(), message.getPort()), handler);
}
@@ -504,6 +546,9 @@
final HttpRequestMessage request;
/** The response future. */
final ResponseFuture response;
+
+ /** The count of additional retries for the connection */
+ int retries = 0;
/**
* Instantiates a new future listener for a connection.
@@ -511,9 +556,10 @@
* @param request the <code>HttpRequestMessage</code> request that is to be sent.
* @param response the response future object.
*/
- public FutureListener(HttpRequestMessage request, ResponseFuture response) {
+ public FutureListener(HttpRequestMessage request, ResponseFuture response, int retries) {
this.request = request;
this.response = response;
+ this.retries = retries;
}
/**
@@ -561,12 +607,18 @@
sess.write(request);
} else {
- try {
- future.getSession();
- response.setException(new AsyncHttpClientException("Connection failed."));
- } catch (RuntimeIOException e) {
- //Set the future exception
- response.setException(e);
+ if (retries > 0) {
+ // go retry this connection
+ retryConnection(request, response, --retries);
+ }
+ else {
+ try {
+ future.getSession();
+ response.setException(new AsyncHttpClientException("Connection failed."));
+ } catch (RuntimeIOException e) {
+ //Set the future exception
+ response.setException(e);
+ }
}
}
}
Added: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java?rev=605288&view=auto
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java (added)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java Tue Dec 18 10:37:33 2007
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.ahc;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.ahc.codec.HttpRequestMessage;
+import org.apache.ahc.codec.HttpResponseMessage;
+
+public class RetryTest extends AbstractTest {
+
+
+ public void testHtmlConnection() throws Exception {
+ TestCallback callback = new TestCallback();
+ doGetConnection(callback, "http://localhost:8284/");
+ Thread.sleep(5000);
+ assertTrue(callback.isException());
+ }
+
+ public void testSSLHtmlConnection() throws Exception {
+ TestCallback callback = new TestCallback();
+ doGetConnection(callback, "https://localhost:8385/");
+
+ Thread.sleep(5000);
+ assertTrue(callback.isException());
+ }
+
+ private void doGetConnection(TestCallback callback, String url)
+ throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL(url), callback);
+
+ request.setParameter("TEST1", "Test One");
+ request.setParameter("TEST2", "Test Two");
+ doConnection(request, callback);
+ }
+
+ private void doConnection(HttpRequestMessage request,
+ TestCallback callback) throws Exception {
+ AsyncHttpClient ahc = new AsyncHttpClient();
+ ahc.setTcpNoDelay(true);
+ ahc.setConnectionRetries(3);
+ // set a short timeout
+ request.setTimeOut(200);
+
+ ahc.sendRequest(request);
+
+ //We are done...Thread would normally end...
+ //So this little wait simulates the thread going back in the pool
+ //5 second timeout due to no response
+ callback.await(5, TimeUnit.SECONDS);
+ }
+}
+
Propchange: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/RetryTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain