Vipin Menon
Vipin Menon

Reputation: 3302

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.

Upvotes: 2

Views: 4033

Answers (2)

Gary Russell
Gary Russell

Reputation: 174554

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.

Upvotes: 2

yuen26
yuen26

Reputation: 1038

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.

Upvotes: 2

Related Questions