Skip to content
Snippets Groups Projects
Commit b9d35965 authored by pkupczyk's avatar pkupczyk
Browse files

LMS-2675 - InfectX: retry API methods when they fail - handle more communication exceptions

SVN: 23920
parent d4844242
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
package ch.systemsx.cisd.common.retry; package ch.systemsx.cisd.common.retry;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.RemoteConnectFailureException; import org.springframework.remoting.RemoteConnectFailureException;
import ch.systemsx.cisd.common.retry.config.DefaultRetryConfiguration; import ch.systemsx.cisd.common.retry.config.DefaultRetryConfiguration;
...@@ -72,21 +76,41 @@ public abstract class RetryCaller<T, E extends Throwable> ...@@ -72,21 +76,41 @@ public abstract class RetryCaller<T, E extends Throwable>
{ {
T result = call(); T result = call();
return result; return result;
} catch (RemoteConnectFailureException e) } catch (RuntimeException e)
{ {
if (shouldRetry()) if (isCommunicationException(e))
{ {
System.err.println("Call failed - will retry"); if (shouldRetry())
waitForRetry(); {
System.err.println("Communication failed - will retry");
waitForRetry();
} else
{
System.err.println("Communication failed - will NOT retry");
throw e;
}
} else } else
{ {
System.err.println("Call failed - will NOT retry");
throw e; throw e;
} }
} }
} }
} }
private boolean isCommunicationException(RuntimeException e)
{
if (e instanceof RemoteConnectFailureException)
{
return true;
}
if (e instanceof RemoteAccessException)
{
return e.getCause() instanceof SocketTimeoutException
|| e.getCause() instanceof SocketException;
}
return false;
}
private boolean shouldRetry() private boolean shouldRetry()
{ {
return retryCounter < configuration.getMaximumNumberOfRetries(); return retryCounter < configuration.getMaximumNumberOfRetries();
......
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
package ch.systemsx.cisd.common.retry; package ch.systemsx.cisd.common.retry;
import java.net.SocketTimeoutException;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
import org.jmock.api.Invocation; import org.jmock.api.Invocation;
import org.jmock.lib.action.CustomAction; import org.jmock.lib.action.CustomAction;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.RemoteConnectFailureException; import org.springframework.remoting.RemoteConnectFailureException;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
...@@ -78,7 +81,7 @@ public class RetryCallerTest ...@@ -78,7 +81,7 @@ public class RetryCallerTest
} }
@Test @Test
public void testCallWithCommunicationErrorShouldBeRetried() throws Throwable public void testCallWithConnectErrorShouldBeRetried() throws Throwable
{ {
mockery.checking(new Expectations() mockery.checking(new Expectations()
{ {
...@@ -93,6 +96,22 @@ public class RetryCallerTest ...@@ -93,6 +96,22 @@ public class RetryCallerTest
mockery.assertIsSatisfied(); mockery.assertIsSatisfied();
} }
@Test
public void testCallWithTimeoutErrorShouldBeRetried() throws Throwable
{
mockery.checking(new Expectations()
{
{
oneOf(runnable).run();
will(throwException(new RemoteAccessException("", new SocketTimeoutException())));
oneOf(runnable).run();
}
});
caller.callWithRetry();
mockery.assertIsSatisfied();
}
@Test @Test
void testCallWithManyCommunicationErrorsShouldBeRetriedWithIncreasingWaitingTime() void testCallWithManyCommunicationErrorsShouldBeRetriedWithIncreasingWaitingTime()
throws Throwable throws Throwable
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment