use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryLoopExample method main.
public static void main(String... args) throws Throwable {
RetryPolicy retryPolicy = new RetryPolicy().retryOn(IllegalStateException.class).withBackoff(10, 40, TimeUnit.MILLISECONDS);
Execution execution = new Execution(retryPolicy);
while (!execution.isComplete()) {
try {
execution.complete(list.size());
} catch (IllegalStateException e) {
execution.recordFailure(e);
// Wait before retrying
Thread.sleep(execution.getWaitTime().toMillis());
}
}
assertEquals(execution.getLastResult(), Integer.valueOf(5));
assertEquals(execution.getExecutions(), 3);
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RxJavaExample method main.
public static void main(String... args) throws Throwable {
AtomicInteger failures = new AtomicInteger();
RetryPolicy retryPolicy = new RetryPolicy().withDelay(1, TimeUnit.SECONDS);
Observable.create((Subscriber<? super String> s) -> {
// Fail 3 times then succeed
if (failures.getAndIncrement() < 3)
s.onError(new RuntimeException());
else
System.out.println("Subscriber completed successfully");
}).retryWhen(attempts -> {
Execution execution = new Execution(retryPolicy);
return attempts.flatMap(failure -> {
System.out.println("Failure detected");
if (execution.canRetryOn(failure))
return Observable.timer(execution.getWaitTime().toNanos(), TimeUnit.NANOSECONDS);
else
return Observable.error(failure);
});
}).toBlocking().forEach(System.out::println);
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanAbortForResult.
public void testCanAbortForResult() {
RetryPolicy policy = new RetryPolicy().abortWhen(10);
assertTrue(policy.canAbortFor(10, null));
assertFalse(policy.canAbortFor(5, null));
assertFalse(policy.canAbortFor(5, new IllegalArgumentException()));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanAbortForCompletionPredicate.
public void testCanAbortForCompletionPredicate() {
RetryPolicy policy = new RetryPolicy().abortIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
assertTrue(policy.canAbortFor("test", null));
assertFalse(policy.canAbortFor(0, null));
assertTrue(policy.canAbortFor(null, new IllegalArgumentException()));
assertFalse(policy.canAbortFor(null, new IllegalStateException()));
}
use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.
the class RetryPolicyTest method testCanRetryForResult.
public void testCanRetryForResult() {
RetryPolicy policy = new RetryPolicy().retryWhen(10);
assertTrue(policy.canRetryFor(10, null));
assertFalse(policy.canRetryFor(5, null));
assertTrue(policy.canRetryFor(5, new Exception()));
}
Aggregations