Design pattern for “retrying” logic that failed?

后端 未结 11 1310
挽巷
挽巷 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: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

提交回复
热议问题