Thursday, September 23, 2021

Retryable Programming

 Spring retry is one of the implementation in Retryable Programming.
Code snippet:
Foo foo = template.execute(new RetryCallback<Foo>() {
    public Foo doWithRetry(RetryContext context) {
        // business logic here
    },
  new RecoveryCallback<Foo>() {
    Foo recover(RetryContext context) throws Exception {
          // recover logic here
    }
});
When the retry attempt is exhausted, the RetryOperations kicks off and it passes the control to a different callback, called the RecoveryCallback.

@Retryable(value = { SomeServerException.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000, maxDelay=5000))
public void doSomething( arg1){
}

  @Recover
    public void recover(SomeServerException e, arg1){

}

Note: I. @Retryable should be invoked from some other bean.
          II. The method doSomething and recover should be a public method.



No comments:

Post a Comment