# Spring Retry Tutorial

In this tutorial I will talk about some retry examples in Spring and how to do it. Retry logic or in other words retry strategies are being used when an operation is failed and we want to basically retry that operation again by a logic. Most of the software systems are doing external calls, operations etc. which are dependent on other systems or resources. For example, charging a client by sending a charge request to Stripe, sending a message to Amazon SQS queue which might failed for a short time due to a transitive issue. Instead of, failing the process at the very first try we can basically have a retry strategy to try the current operation multiple times to continue current process without any issue. In Spring, there are multiple ways to define how to retry on some operation.

## Enabling Retry

First, we have to enable retry globally from Spring context to our @Configuration class by adding @EnableRetry annotation.

## 1. Retry with Retryable Annotation

To define a retry strategy for a method we have to add @Retryable annotation on it. Additionally, we can define the behaviour of retry by providing some set of inputs. We will take a closer look for the most general ones which are;

• maxAttemps - Maximum number of attemps to retry.
• value - Exception types to enable retry.
• backoff - Backoff property to define backoff logic.
• delay - The duration of delay given milliseconds
• multiplier - Multiplier value for delay

A retry with fixed delay example;

In this case we have a retry strategy having maximum attemps by 5 where each of them will attempt run with 1 second of delay. Later on, if all attempts failed there is no any error handling logic for the last thrown exception. To cover this, we will define a recovery action by implementing a method annotated with @Recover. This recovery method should have a method signature including the exception thrown from retried method along with other method parameters from method to recover. With this way spring will resolve the correct recover method. So, our recovery method will be like;

An example output will be like;

Now, if we want to have an exponential retry strategy then we have to set multiplier in backoff property. Here is an example;

This means, retry each attempt by having with delay of 1000 * $$2^{n}$$ where n is the current number of retry. An example output from a test would be like;

## 2. Retry with RetryTemplate

Another solution to provide a retry strategy is by using RetryTemplate. Here I will use FixedBackOffPolicy implementation as an example. First we implement a bean method in a configuration class for RetryTemplate;

Now we can use this RestTemplate anywhere in spring to apply a retry logic by its execute method. There are 3 implementations of execute where I will show the common one by giving RetryCallback and RecoveryCallback functions.

In this example we defined a function which will be retried by number of attemps defined for RestTemplate and a recovery method which will be called when all retries exhausted. In other words, we can implement these callback classes and pass it in execute method. Something like;

And the output would be like;

You can find the whole project from my github