Search in sources :

Example 1 with ContextualRunnable

use of dev.failsafe.function.ContextualRunnable 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);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Future(java.util.concurrent.Future) RetryPolicy(dev.failsafe.RetryPolicy) Duration(java.time.Duration) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) ContextualRunnable(dev.failsafe.function.ContextualRunnable) Timeout(dev.failsafe.Timeout) Function(java.util.function.Function) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) ContextualRunnable(dev.failsafe.function.ContextualRunnable) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

Failsafe (dev.failsafe.Failsafe)1 RetryPolicy (dev.failsafe.RetryPolicy)1 Timeout (dev.failsafe.Timeout)1 ContextualRunnable (dev.failsafe.function.ContextualRunnable)1 Duration (java.time.Duration)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Test (org.testng.annotations.Test)1