Search in sources :

Example 1 with Execution

use of net.jodah.failsafe.Execution 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 2 with Execution

use of net.jodah.failsafe.Execution 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)

Aggregations

Execution (net.jodah.failsafe.Execution)2 RetryPolicy (net.jodah.failsafe.RetryPolicy)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1