spring-retry Retryble annotation versus retryTemplate

I am trying to understand spring-retry and I saw 2 approaches where the first one was the simple

    @Retryable(value = {SomeException1.class,SomeException2.class}, maxAttempts = Constants.RETRY_VAL, backoff = @Backoff(value = Constants.RETRY_BACK))
    public String getData(){...}

and the second-way RetryTemplate (Execute with RetryCallback/RecoveryCallback).

My understanding is that the second one gives more fine-grained control. When would we go for the 2’nd approach? Would it be when we have our own Implementations for Backoff and retryPolicies?

Also, I couldn’t find examples or samples of setting up custom policies up with the @Retryble annotation.

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

In my opinion, the second approach RetryTemplate is preferred choice. Because this approach help us to write configuration at one place and use at many place.

You can also create one or many RetryTemplate beans. Each RetryTemplate bean is used for a map of exceptions.

Jason
Guest

You have essentially equal control either way.

To use a customized RetryTemplate via Retryable, simply wire up a retry interceptor as a bean (with the custom template) and provide it in the interceptor property.

/**
 * Retry interceptor bean name to be applied for retryable method. Is mutually
 * exclusive with other attributes.
 * @return the retry interceptor bean name
 */
String interceptor() default "";

It’s simply a matter of preference which way to go.