Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ repositories {
}

dependencies {
compile 'com.google.guava:guava:[10.+,)'
compile 'com.google.guava:guava:20.0'
compile 'com.google.code.findbugs:jsr305:2.0.2'

// junit testing
Expand Down
90 changes: 81 additions & 9 deletions src/main/java/com/github/rholder/retry/Retryer.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,42 @@ public V call(Callable<V> callable) throws ExecutionException, RetryException {
if (!rejectionPredicate.apply(attempt)) {
return attempt.get();
}
if (stopStrategy.shouldStop(attempt)) {
stopOrBlock(attemptNumber, attempt);
}
}

public void run(Runnable runnable) throws ExecutionException, RetryException {
long startTime = System.nanoTime();
for (int attemptNumber = 1; ; attemptNumber++) {
Attempt<V> attempt;
try {
runnable.run();
attempt = new VoidResultAttempt<V>(attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
} catch (Throwable t) {
attempt = new ExceptionAttempt<V>(t, attemptNumber, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
}

for (RetryListener listener : listeners) {
listener.onRetry(attempt);
}

if (!rejectionPredicate.apply(attempt)) {
return;
}
stopOrBlock(attemptNumber, attempt);
}
}

private void stopOrBlock(int attemptNumber, Attempt<V> attempt) throws RetryException {
if (stopStrategy.shouldStop(attempt)) {
throw new RetryException(attemptNumber, attempt);
} else {
long sleepTime = waitStrategy.computeSleepTime(attempt);
try {
blockStrategy.block(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RetryException(attemptNumber, attempt);
} else {
long sleepTime = waitStrategy.computeSleepTime(attempt);
try {
blockStrategy.block(sleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RetryException(attemptNumber, attempt);
}
}
}
}
Expand Down Expand Up @@ -292,6 +318,52 @@ public long getDelaySinceFirstAttempt() {
}
}

@Immutable
static final class VoidResultAttempt<Void> implements Attempt<Void> {
private final long attemptNumber;
private final long delaySinceFirstAttempt;

public VoidResultAttempt(long attemptNumber, long delaySinceFirstAttempt) {
this.attemptNumber = attemptNumber;
this.delaySinceFirstAttempt = delaySinceFirstAttempt;
}

@Override
public Void get() throws ExecutionException {
throw new IllegalStateException("The attempt resulted in a void result");
}

@Override
public boolean hasResult() {
return false;
}

@Override
public boolean hasException() {
return false;
}

@Override
public Void getResult() throws IllegalStateException {
throw new IllegalStateException("The attempt resulted in a void result");
}

@Override
public Throwable getExceptionCause() throws IllegalStateException {
throw new IllegalStateException("The attempt resulted in a void result, not in an exception");
}

@Override
public long getAttemptNumber() {
return attemptNumber;
}

@Override
public long getDelaySinceFirstAttempt() {
return delaySinceFirstAttempt;
}
}

/**
* A {@link Callable} which wraps another {@link Callable} in order to add
* retrying behavior from a given {@link Retryer} instance.
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/github/rholder/retry/RetryerBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,30 @@ public Boolean call() throws Exception {
}
};
}

@Test
public void testRunnable() throws Exception {
Runnable runnable = new Runnable() {
@Override
public void run() {
throw new NullPointerException();
}
};

Retryer retryer = RetryerBuilder.newBuilder()
.retryIfRuntimeException()
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.retryIfRuntimeException()
.build();

try {
retryer.run(runnable);
fail("RetryException expected");
} catch (RetryException e) {
assertEquals(3, e.getNumberOfFailedAttempts());
assertTrue(e.getLastFailedAttempt().hasException());
assertTrue(e.getLastFailedAttempt().getExceptionCause() instanceof NullPointerException);
assertTrue(e.getCause() instanceof NullPointerException);
}
}
}