Search in sources :

Example 36 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 37 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 38 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 39 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)

Example 40 with RetryPolicy

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

the class Issue36 method retryListener_WithExceptions_ShouldBeCalled.

@Test
public void retryListener_WithExceptions_ShouldBeCalled() throws Exception {
    RetryPolicy policy = new RetryPolicy().retryIf((Boolean response) -> response != null && !response).retryOn(Exception.class).withMaxRetries(3);
    AtomicInteger listenerCallbacks = new AtomicInteger();
    try {
        Failsafe.<Boolean>with(policy).onRetry((failedResponse, exception, context) -> listenerCallbacks.incrementAndGet()).get(() -> {
            throw new RuntimeException();
        });
    } catch (RuntimeException e) {
    }
    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)56 Failsafe (net.jodah.failsafe.Failsafe)16 IOException (java.io.IOException)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 TimeUnit (java.util.concurrent.TimeUnit)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Executors (java.util.concurrent.Executors)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Test (org.testng.annotations.Test)5 ConnectException (java.net.ConnectException)4 Duration (java.time.Duration)4 Map (java.util.Map)4 FailsafeException (net.jodah.failsafe.FailsafeException)4 Assert.assertEquals (org.testng.Assert.assertEquals)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3