Search code examples
javareactive-streamscyclops-react

cyclops-react and async-retry: How to retry on timeout?


I'm starting to use cyclops-react with async-retry. I'm still a little bit lost with it.

I'm using SimpleReact and simulating a timeout from the server but I never receive a timeout with something like this:

private List<Object> executeParallel() {
    List<Object> result = new SimpleReact(mainThreadPool)
            .of(getSupplier())
            .withRetrier(new AsyncRetryExecutor(retryThreadPool)
                    .abortIf((t) -> !TimeoutException.class.isAssignableFrom(t.getClass()))
            )
            .retry(retrySupplier())
            .block()
            .collect(Collectors.toList());
    return result;
}

private Supplier getSupplier() {
    return () -> someOperationThatTimesOut();
}

private Function<Supplier, Object> retrySupplier() {
    return supplier -> supplier.get();
}

What is missing there?


Solution

  • This version works

     AtomicInteger count = new AtomicInteger(0);
    
    @Test
    public void executeParallel() {
        List<Object> result = new SimpleReact(Executors.newFixedThreadPool(1))
                .of(getSupplier())
                .withRetrier(new AsyncRetryExecutor(Executors.newScheduledThreadPool(1))
                .retry(Supplier::get)
                .block()
                .collect(Collectors.toList());
        System.out.println(result);
    }
    
    private Supplier<String> getSupplier() {
        return () -> {
            System.out.println("Attempt " + count.incrementAndGet());
            if(count.get()<4)
                throw ExceptionSoftener.throwSoftenedException(new TimeoutException());
            return "success";
        };
    }
    

    It will print out

    Attempt 1
    Attempt 2
    Attempt 3
    Attempt 4
    [success]
    

    I suspect you don't need the abortIf on the async-retrier, and I'm not sure what is going inside someOperationThatTimesOut() - that may be the key here.