Search code examples
springmultithreadingthreadpoolspring-retry

Does spring @Retryable create a new connection or use the same connection with server


I have a Spring project wherein I am using @Retryable annotation on a method. The method calls an API and posts some json payload to it.

I have set maxAttempts as 4 and a retry delay of 20 ms using Backoff.

I am debugging an issue and hence trying to understand if @Retryable creates a new connection(a new thread) with the server where the API is hosted after the specified delay(20 ms in my case) or uses the same connection(same thread)


Solution

  • The @Retryable uses the RetryTemplate and that one has the logic like this:

            while (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
    
                try {
                    ...
                    T result = retryCallback.doWithRetry(context);
                    doOnSuccessInterceptors(retryCallback, context, result);
                    return result;
                }
                catch (Throwable e) {
    
                    ...
    
                    if (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
                        ...
                    }
    
                    ...
            }
    

    So, all the retry attempts are happening on the same thread - the calling one.