Giter Club home page Giter Club logo

Comments (5)

jeffreyschultz avatar jeffreyschultz commented on May 27, 2024

Here is a retry function that I wrote to handle retries that has been adapted to work with Promises.

protected Promise<T> retry(Callable<CompletionStage<T>> callable) throws Exception {
        
        int retries = 0;
        boolean isRetry = false;
        
        do {
            try {
                if (isRetry) {
                    logger.warn(String.format("Retrying previous request (%s).", retries));
                }
                
                return Promises.from(callable.call());
            } catch (final Exception e) {
                logger.error("An exception was thrown while processing request.", e);
    
                isRetry = true;
                if (retries >= maxRetries) {
                    throw e;
                }
            }
            
            //
            // Back off for two seconds to try and not saturate the server with retry attempts.
            //
            Thread.sleep(2000);
        } while (retries++ < maxRetries);
        
        throw new RuntimeException("The number of maximum retries has been exceeded for this request.");
    }

from tascalate-concurrent.

vsilaev avatar vsilaev commented on May 27, 2024

Jeffrey,

If I understand your requirements correctly, then:

  1. You have Callable<CompletionStage<T>> callable that returns a new CompletionStage whenever it's called.
  2. You should use net.tascalate.concurrent.Promises.retryFuture(callable, retryPolicy)
  3. In your case retryPolicy is:
RetryPolicy<T> retryPolicy = new RetryPolicy<>(maxRetries, DelayPolicy.fixedInterval(2000L));

Is it what you need?

from tascalate-concurrent.

jeffreyschultz avatar jeffreyschultz commented on May 27, 2024

It looks right. I don't know how I didn't see the retryFuture method before...

How are exceptions handled?

from tascalate-concurrent.

vsilaev avatar vsilaev commented on May 27, 2024

Jeffrey,

Unfortunately, RetryPolicy class is not documented yet, but you can specify what exceptions are re-tryable and what should stop processing. Please check sources of the class -- but, in general, everything is configurable.

And, btw, in my example above you should use:

RetryPolicy<T> retryPolicy = new RetryPolicy<>(
  maxRetries, 
  DelayPolicy.fixedInterval(2000L).withFirstRetryNoDelay() 
);

To avoid waiting before the first execution

from tascalate-concurrent.

jeffreyschultz avatar jeffreyschultz commented on May 27, 2024

This worked. I apologize for not providing a follow up!

from tascalate-concurrent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.