Search in sources :

Example 16 with RetryPolicy

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);
}
Also used : Execution(net.jodah.failsafe.Execution) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 17 with RetryPolicy

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);
}
Also used : Execution(net.jodah.failsafe.Execution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 18 with RetryPolicy

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()));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 19 with RetryPolicy

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()));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 20 with RetryPolicy

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()));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Aggregations

RetryPolicy (net.jodah.failsafe.RetryPolicy)34 Failsafe (net.jodah.failsafe.Failsafe)12 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7 Test (org.testng.annotations.Test)7 Executors (java.util.concurrent.Executors)6 Assert.assertEquals (org.testng.Assert.assertEquals)6 ConnectException (java.net.ConnectException)5 Assert.fail (org.testng.Assert.fail)5 Future (java.util.concurrent.Future)4 TimeUnit (java.util.concurrent.TimeUnit)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Waiter (net.jodah.concurrentunit.Waiter)3 Paths (java.nio.file.Paths)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2