Search in sources :

Example 26 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.

the class RetryPolicyTest method testCanRetryForNull.

public void testCanRetryForNull() {
    RetryPolicy policy = new RetryPolicy();
    assertFalse(policy.canRetryFor(null, null));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 27 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.

the class RetryPolicyTest method testCanAbortForNull.

public void testCanAbortForNull() {
    RetryPolicy policy = new RetryPolicy();
    assertFalse(policy.canAbortFor(null, null));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 28 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.

the class RetryPolicyTest method testCanRetryForCompletionPredicate.

public void testCanRetryForCompletionPredicate() {
    RetryPolicy policy = new RetryPolicy().retryIf((result, failure) -> result == "test" || failure instanceof IllegalArgumentException);
    assertTrue(policy.canRetryFor("test", null));
    // No retries needed for successful result
    assertFalse(policy.canRetryFor(0, null));
    assertTrue(policy.canRetryFor(null, new IllegalArgumentException()));
    assertFalse(policy.canRetryFor(null, new IllegalStateException()));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 29 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.

the class NettyExample method main.

public static void main(String... args) throws Throwable {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = createBootstrap(group);
    RetryPolicy retryPolicy = new RetryPolicy().withDelay(1, TimeUnit.SECONDS);
    Failsafe.with(retryPolicy).with(group).runAsync(execution -> bootstrap.connect(HOST, PORT).addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
            System.out.println("Connected!");
            try {
                channelFuture.sync();
                channelFuture.channel().closeFuture().sync();
            } catch (Exception ignore) {
                group.shutdownGracefully();
            }
        } else if (!execution.retryOn(channelFuture.cause()))
            System.out.println("Connection attempts failed");
    }));
    Thread.sleep(5000);
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) RetryPolicy(net.jodah.failsafe.RetryPolicy) ChannelFutureListener(io.netty.channel.ChannelFutureListener) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 30 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.

the class Issue36 method retryListener_WithFailedResponses_ShouldBeCalled.

@Test
public void retryListener_WithFailedResponses_ShouldBeCalled() throws Exception {
    RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
    AtomicInteger listenerCallbacks = new AtomicInteger();
    Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> false);
    assertEquals(listenerCallbacks.get(), 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Failsafe(net.jodah.failsafe.Failsafe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy) Test(org.testng.annotations.Test)

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