use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Java8Example method main.
@SuppressWarnings("unused")
public static void main(String... args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
RetryPolicy<Object> retryPolicy = RetryPolicy.ofDefaults();
// Create a retryable functional interface
Function<String, String> bar = value -> Failsafe.with(retryPolicy).get(() -> value + "bar");
// Create a retryable Stream operation
Failsafe.with(retryPolicy).get(() -> Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).collect(Collectors.toList()));
// Create an individual retryable Stream operation
Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);
// Create a retryable CompletableFuture
Failsafe.with(retryPolicy).with(executor).getStageAsync(() -> CompletableFuture.supplyAsync(() -> "foo").thenApplyAsync(value -> value + "bar").thenAccept(System.out::println));
// Create an individual retryable CompletableFuture stages
CompletableFuture.supplyAsync(() -> Failsafe.with(retryPolicy).get(() -> "foo")).thenApplyAsync(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).thenAccept(System.out::println);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue5Test method test.
/**
* Asserts that a failure is handled as expected by a listener registered via whenFailure.
*/
public void test() throws Throwable {
Waiter waiter = new Waiter();
RetryPolicy<Object> retryPolicy = RetryPolicy.builder().withDelay(Duration.ofMillis(100)).withMaxDuration(Duration.ofSeconds(2)).withMaxRetries(3).handleResult(null).build();
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
Failsafe.with(retryPolicy).with(executor).onFailure(e -> {
waiter.assertNull(e.getResult());
waiter.assertNull(e.getException());
waiter.resume();
}).getAsync(() -> null);
waiter.await(1000);
executor.shutdownNow();
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue36Test method failedAttemptListener_WithFailedResponses_ShouldBeCalled.
@Test
public void failedAttemptListener_WithFailedResponses_ShouldBeCalled() {
AtomicInteger listenerCallbacks = new AtomicInteger();
RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onFailedAttempt(e -> listenerCallbacks.incrementAndGet()).build();
Failsafe.with(policy).get(() -> false);
assertEquals(listenerCallbacks.get(), 4);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue192Test method testAsync.
/**
* Asserts the handling of multiple retry policies with an async execution.
*/
public void testAsync() {
AtomicInteger exceptionA = new AtomicInteger();
AtomicInteger exceptionB = new AtomicInteger();
AtomicInteger exceptionC = new AtomicInteger();
RetryPolicy<Object> policyA = RetryPolicy.builder().handle(ExceptionA.class).withMaxRetries(5).onRetry(evt -> exceptionA.incrementAndGet()).build();
RetryPolicy<Object> policyB = RetryPolicy.builder().handle(ExceptionB.class).withMaxRetries(3).onRetry(evt -> exceptionB.incrementAndGet()).build();
RetryPolicy<Object> policyC = RetryPolicy.builder().handle(ExceptionC.class).withMaxRetries(2).onRetry(evt -> exceptionC.incrementAndGet()).build();
Asserts.assertThrows(() -> Failsafe.with(policyA, policyB, policyC).getAsyncExecution(execution -> Testing.futureException(executor, new ExceptionB()).whenComplete((result, failure) -> {
// System.out.println("Result = " + result + "; failure = " + failure);
execution.record(result, failure);
})).get(), ExecutionException.class, ExceptionB.class);
Assert.assertEquals(exceptionA.get(), 0);
Assert.assertEquals(exceptionB.get(), 3);
Assert.assertEquals(exceptionC.get(), 0);
}
use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.
the class Issue260Test method test.
public void test() throws Throwable {
ExecutorService executor = Executors.newSingleThreadExecutor();
Timeout<Object> timeout = Timeout.builder(Duration.ofMillis(300)).withInterrupt().onFailure(e -> System.out.println("Interrupted")).build();
RetryPolicy<Object> rp = RetryPolicy.builder().onRetry(e -> System.out.println("Retrying")).onSuccess(e -> System.out.println("Success")).build();
Function<Integer, ContextualRunnable> task = (taskId) -> ctx -> {
System.out.println("Starting execution of task " + taskId);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("Interrupted task " + taskId);
throw e;
}
};
Future<?> f1 = Failsafe.with(rp, timeout).with(executor).runAsync(task.apply(1));
Future<?> f2 = Failsafe.with(rp, timeout).with(executor).runAsync(task.apply(2));
Future<?> f3 = Failsafe.with(rp, timeout).with(executor).runAsync(task.apply(3));
f1.get(1, TimeUnit.SECONDS);
f2.get(1, TimeUnit.SECONDS);
f3.get(1, TimeUnit.SECONDS);
}
Aggregations