diff --git a/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java b/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java index 80692a633ffebb046532a664d77a4954458a202a..65d6cdda658bb3575540c1369c389ea8de7fa6d2 100644 --- a/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java +++ b/common/source/java/ch/systemsx/cisd/common/process/CallableExecutor.java @@ -29,26 +29,26 @@ import ch.systemsx.cisd.common.exceptions.StopException; */ public final class CallableExecutor { - private final int maxRetryOnFailure; + private final int maxInvocationsOnFailure; private final long millisToSleepOnFailure; public CallableExecutor() { - this(Constants.MAXIMUM_RETRY_COUNT, Constants.MILLIS_TO_SLEEP_BEFORE_RETRYING); + this(Constants.MAXIMUM_INVOCATIONS_ON_FAILURE, Constants.MILLIS_TO_SLEEP_BEFORE_RETRYING); } - public CallableExecutor(final int maxRetryOnFailure, final long millisToSleepOnFailure) + public CallableExecutor(final int maxInvocationsOnFailure, final long millisToSleepOnFailure) { assert millisToSleepOnFailure > -1 : "Negative value"; - assert maxRetryOnFailure > -1 : "Negative value"; - this.maxRetryOnFailure = maxRetryOnFailure; + assert maxInvocationsOnFailure > -1 : "Negative value"; + this.maxInvocationsOnFailure = maxInvocationsOnFailure; this.millisToSleepOnFailure = millisToSleepOnFailure; } /** * Executes given <var>callable</var> until it returns a non-<code>null</code> value (or - * until <code>maxRetryOnFailure</code> is reached). + * until <code>maxInvocationsOnFailure</code> is reached). */ public final <T> T executeCallable(final Callable<T> callable) throws StopException { @@ -60,11 +60,15 @@ public final class CallableExecutor { StopException.check(); result = callable.call(); - if (counter > 0 && millisToSleepOnFailure > 0) + if (result == null) { - Thread.sleep(millisToSleepOnFailure); + ++counter; + if (counter < maxInvocationsOnFailure && millisToSleepOnFailure > 0) + { + Thread.sleep(millisToSleepOnFailure); + } } - } while (counter++ < maxRetryOnFailure && result == null); + } while (result == null && counter < maxInvocationsOnFailure); } catch (final Exception ex) { throw CheckedExceptionTunnel.wrapIfNecessary(ex); diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java index d6ba0ed43b6005ade488158677a11e86227fd06f..6a97a96f438721394b50e281a200e346d226a12f 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/process/CallableExecutorTest.java @@ -95,7 +95,13 @@ public final class CallableExecutorTest context.checking(new Expectations() { { - exactly(maxRetryOnFailure + 1).of(callable).call(); + if (maxRetryOnFailure == 0) + { + one(callable).call(); + } else + { + exactly(maxRetryOnFailure).of(callable).call(); + } will(returnValue(null)); } });