Design pattern for “retrying” logic that failed?

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

    I'm using AOP and Java annotations. There is a ready-made mechanism in jcabi-aspects (I'm a developer):

    @RetryOnFailure(attempts = 3, delay = 1, unit = TimeUnit.SECONDS)
    public void establishConnection() {
      this.connection = newConnection();
    }
    

    ps. You can also try RetryScalar from Cactoos.

提交回复
热议问题