Search in sources :

Example 1 with Scheduler

use of dev.failsafe.spi.Scheduler in project failsafe by jhalterman.

the class VertxExample method main.

/**
 * A Vert.x sender and retryable receiver example.
 */
public static void main(String... args) throws Throwable {
    // Receiver that fails 2 times then succeeds
    AtomicInteger failures = new AtomicInteger();
    vertx.eventBus().consumer("ping-address", message -> {
        if (failures.getAndIncrement() < 2)
            message.fail(1, "Failed");
        else {
            message.reply("pong!");
        }
    });
    // Retryable sender
    Failsafe.with(retryPolicy).with(scheduler).getAsyncExecution(execution -> vertx.eventBus().send("ping-address", "ping!", reply -> {
        if (reply.succeeded())
            execution.recordResult(reply.result());
        else
            execution.recordFailure(reply.cause());
    }));
    Thread.sleep(5000);
    System.exit(0);
}
Also used : RetryPolicy(dev.failsafe.RetryPolicy) DefaultScheduledFuture(dev.failsafe.spi.DefaultScheduledFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) Failsafe(dev.failsafe.Failsafe) Scheduler(dev.failsafe.spi.Scheduler) ReplyException(io.vertx.core.eventbus.ReplyException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with Scheduler

use of dev.failsafe.spi.Scheduler in project failsafe by jhalterman.

the class DelegatingSchedulerTest method shouldClearInterruptFlagInForkJoinPoolThreads.

/**
 * Asserts that ForkJoinPool clears interrupt flags.
 */
public void shouldClearInterruptFlagInForkJoinPoolThreads() throws Throwable {
    Scheduler scheduler = new DelegatingScheduler(new ForkJoinPool(1));
    AtomicReference<Thread> threadRef = new AtomicReference<>();
    Waiter waiter = new Waiter();
    // Create interruptable execution
    scheduler.schedule(() -> {
        threadRef.set(Thread.currentThread());
        waiter.resume();
        Thread.sleep(10000);
        return null;
    }, 0, TimeUnit.MILLISECONDS);
    waiter.await(1000);
    threadRef.get().interrupt();
    // Check for interrupt flag
    scheduler.schedule(() -> {
        waiter.assertFalse(Thread.currentThread().isInterrupted());
        waiter.resume();
        return null;
    }, 0, TimeUnit.MILLISECONDS);
    waiter.await(1000);
}
Also used : Scheduler(dev.failsafe.spi.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Waiter(net.jodah.concurrentunit.Waiter)

Aggregations

Scheduler (dev.failsafe.spi.Scheduler)2 Failsafe (dev.failsafe.Failsafe)1 RetryPolicy (dev.failsafe.RetryPolicy)1 DefaultScheduledFuture (dev.failsafe.spi.DefaultScheduledFuture)1 Vertx (io.vertx.core.Vertx)1 Message (io.vertx.core.eventbus.Message)1 ReplyException (io.vertx.core.eventbus.ReplyException)1 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)1 Duration (java.time.Duration)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Waiter (net.jodah.concurrentunit.Waiter)1