0

Trying to understand how the retries are implemented, I'd expect to see something like threads in the source of Spring Retry - but don't. I'd like to know how retrying is implemented, if not as per threads.

Additionally I didn't find an @Aspect that would wrap the method to be retried. Since AOP is a dependency of Spring Retry - I would also expect to see some AOP-stuff. Where is that 'hidden'?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Mr. Gung
  • 117
  • 1
  • 9

2 Answers2

2

There is no threading; retries are invoked on the calling thread.

Spring Retry can be used in two modes:

In Imperative Mode you programmatically invoke the target code using a RetryTemplate.

In Declarative Mode you annotate public methods with @Retryable.

In this mode, @EnableRetry registers 2 beans RetryConfiguration (extends AbstractPointcutAdvisor) and AnnotationAwareAspectJAutoProxyCreator.

RetryConfiguration creates an Advice (AnnotationAwareRetryOperationsInterceptor) and AnnotationClassOrMethodPointcut.

Using these beans, Spring AOP uses the auto proxy creator to wrap beans with @Retryable annotations in a proxy with the interceptor advice.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
1

Disclaimer: I am an AOP expert, not a Spring user.

Spring-Retry does not have a direct dependency on either of Spring-AOP, AspectJ or Spring-Aspects, only an indirect one via Spring-Core:

https://mvnrepository.com/artifact/org.springframework.retry/spring-retry/1.3.4

Have you seen class StatefulRetryOperationsInterceptor implements MethodInterceptor? There you have a dependency on classes from the org.aopalliance.intercept package. That mini library from AOP Alliance is like the smallest common denominator for Spring AOP, AspectJ and other interceptor-based approaches. It looks as if in this case we have manually implemented interceptors rather than full-fledged Spring AOP or AspectJ aspects.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • 1
    You can use it with or without AOP. Use `RetryTemplate` programmatically (https://github.com/spring-projects/spring-retry#-using-retrytemplate) or, you can annotate public methods with `@Retryable` (https://github.com/spring-projects/spring-retry#declarative-retry) ; in the latter case, it does require aspectJ. https://github.com/spring-projects/spring-retry#-additional-dependencies - it gets spring-aop as a transitive dependency via spring-context. – Gary Russell Oct 31 '22 at 15:53
  • 1
    Thanks for the clarification. But the question was more like "where are the aspects?", and the answer seems to be "there are not any", because the library manually implements interceptors and does not explicitly implement any `@Aspect`s with pointcuts, before/after/around advice or the like. – kriegaex Oct 31 '22 at 15:55
  • 2
    Right, but the `MethodInterceptor` is applied as an advice to the target bean via Spring AOP's `AspectJAwareAdvisorAutoProxyCreator`. – Gary Russell Oct 31 '22 at 16:28
  • I think you should write a comprehensive answer. I will vote for it and suggest it to be the accepted one. – kriegaex Oct 31 '22 at 18:35