Design pattern for “retrying” logic that failed?

后端 未结 11 1298
挽巷
挽巷 2020-12-07 16:10

I\'m writing some reconnect logic to periodically attempt to establish a connection to a remote endpoint which went down. Essentially, the code looks like this:



        
相关标签:
11条回答
  • 2020-12-07 17:03

    You could try the Idempotent Retry Pattern.

    enter image description here

    0 讨论(0)
  • 2020-12-07 17:05

    If you are using java 8, this may helps.

    import java.util.function.Supplier;
    
    public class Retrier {
    public static <T> Object retry(Supplier<T> function, int retryCount) throws Exception {
         while (0<retryCount) {
            try {
                return function.get();
            } catch (Exception e) {
                retryCount--;
                if(retryCount == 0) {
                    throw e;
                }
            }
        }
        return null;
    }
    
    public static void main(String[] args) {
        try {
            retry(()-> {
                System.out.println(5/0);
                return null;
            }, 5);
        } catch (Exception e) {
            System.out.println("Exception : " + e.getMessage());
        }
    }
    }
    

    Thanks,

    Praveen R.

    0 讨论(0)
  • 2020-12-07 17:12

    Using Failsafe (author here):

    RetryPolicy retryPolicy = new RetryPolicy()
      .retryOn(IOException.class)
      .withMaxRetries(5)
      .withDelay(1, TimeUnit.SECONDS);
    
    Failsafe.with(retryPolicy).run(() -> newConnection());
    

    No annotations, no magic, doesn't need to be a Spring app, etc. Just straightforward and simple.

    0 讨论(0)
  • 2020-12-07 17:13

    You can try spring-retry, it has a clean interface and it's easy to use.

    Example:

     @Retryable(maxAttempts = 4, backoff = @Backoff(delay = 500))
     public void establishConnection() {
        this.connection = newConnection();
     } 
    

    In case of exception, it will retry (call) up to 4 times the method establishConnection() with a backoff policy of 500ms

    0 讨论(0)
  • 2020-12-07 17:14

    I'm using retry4j library. Test code example:

    public static void main(String[] args) {
        Callable<Object> callable = () -> {
            doSomething();
            return null;
        };
    
        RetryConfig config = new RetryConfigBuilder()
                .retryOnAnyException()
                .withMaxNumberOfTries(3)
                .withDelayBetweenTries(5, ChronoUnit.SECONDS)
                .withExponentialBackoff()
                .build();
    
        new CallExecutorBuilder<>().config(config).build().execute(callable);
    }
    
    public static void doSomething() {
        System.out.println("Trying to connect");
        // some logic
        throw new RuntimeException("Disconnected"); // init error
        // some logic
    }
    
    0 讨论(0)
提交回复
热议问题