Author: ron.sigal(a)jboss.com
Date: 2008-05-10 17:52:59 -0400 (Sat, 10 May 2008)
New Revision: 4165
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/transport/http/HTTPClientInvoker.java
Log:
JBREM-979: Added retry facility.
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/transport/http/HTTPClientInvoker.java
===================================================================
---
remoting2/branches/2.x/src/main/org/jboss/remoting/transport/http/HTTPClientInvoker.java 2008-05-10
04:10:29 UTC (rev 4164)
+++
remoting2/branches/2.x/src/main/org/jboss/remoting/transport/http/HTTPClientInvoker.java 2008-05-10
21:52:59 UTC (rev 4165)
@@ -63,9 +63,6 @@
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.URL;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -97,9 +94,16 @@
*/
public static final int MAX_NUM_TIMEOUT_THREADS_DEFAULT = 10;
+ /**
+ * Specifies the number of attempts to get a functioning connection
+ * to the http server. Defaults to 1.
+ */
+ public static final String NUMBER_OF_CALL_ATTEMPTS =
"numberOfCallAttempts";
+
protected static final Logger log = Logger.getLogger(HTTPClientInvoker.class);
protected boolean noThrowOnError;
+ protected int numberOfCallAttempts = 1;
private Object timeoutThreadPoolLock = new Object();
private ThreadPool timeoutThreadPool;
@@ -130,7 +134,7 @@
throws IOException, ConnectionFailedException
{
// need to check the url and make sure it compatible protocol
- String validatedUrl = validateURL(getLocator().getLocatorURI());
+ final String validatedUrl = validateURL(getLocator().getLocatorURI());
if (metadata == null)
{
@@ -143,7 +147,7 @@
if (simulatedTimeout <= 0)
{
- return useHttpURLConnection(conn, invocation, metadata, marshaller,
unmarshaller);
+ return makeInvocation(conn, validatedUrl, invocation, metadata, marshaller,
unmarshaller, true);
}
else
{
@@ -158,7 +162,7 @@
{
try
{
- resultHolder.value = useHttpURLConnection(conn, invocation,
finalMetadata, marshaller, unmarshaller);
+ resultHolder.value = makeInvocation(conn, validatedUrl, invocation,
finalMetadata, marshaller, unmarshaller, false);
if (log.isTraceEnabled()) log.trace("result: " +
resultHolder.value);
}
catch (Exception e)
@@ -202,6 +206,39 @@
}
}
+ protected Object makeInvocation(HttpURLConnection conn, String url, Object
invocation,
+ Map metadata, Marshaller marshaller, UnMarshaller
unmarshaller,
+ boolean setTimeout)
+ throws IOException
+ {
+ Throwable savedException = null;
+
+ for (int i = 0; i < numberOfCallAttempts; i++)
+ {
+ try
+ {
+ Object o = useHttpURLConnection(conn, invocation, metadata, marshaller,
unmarshaller);
+ if (log.isTraceEnabled()) log.trace("result: " + o);
+ return o;
+ }
+ catch (CannotConnectException e)
+ {
+ savedException = e.getCause();
+ String suffix = (i < (numberOfCallAttempts - 1) ? ": will retry"
: "");
+ log.debug("Cannot connect on attempt " + (i + 1) + suffix);
+ conn = createURLConnection(url, metadata);
+ if (setTimeout)
+ {
+ getSimulatedTimeout(configuration, metadata, conn);
+ }
+ }
+ }
+
+ String msg = "Can not connect http client invoker after " +
numberOfCallAttempts + " attempt(s)";
+ throw new CannotConnectException(msg, savedException);
+ }
+
+
private Object useHttpURLConnection(HttpURLConnection conn, Object invocation, Map
metadata,
Marshaller marshaller, UnMarshaller unmarshaller)
throws WebServerError
{
@@ -916,6 +953,22 @@
val + " to a boolean value.");
}
}
+
+ val = configuration.get(NUMBER_OF_CALL_ATTEMPTS);
+ if (val != null)
+ {
+ try
+ {
+ numberOfCallAttempts = Integer.valueOf((String)val).intValue();
+ log.debug(this + " setting numberOfCallRetries to " +
numberOfCallAttempts);
+ }
+ catch (Exception e)
+ {
+ log.warn(this + " could not convert " +
+ NUMBER_OF_CALL_ATTEMPTS + " value of " +
+ val + " to an int value.");
+ }
+ }
}
/**