In this Spring boot tutorial, learn to use Spring retry module to invoke remote APIs and retry the request if it fails for some reason such as a network outage, server down, network glitch, deadlock etc. In such cases, we usually try to retry the operation a few times before sending an error to the client to make processing more robust and less prone to failure.
The spring-retry module provides a declarative way to configure the retries using annotations. We can also define the fallback method if all retries fail.
1. Maven
Import the latest version of spring-retry dependency from the maven repository. Spring retry is AOP based so include the latest version of spring-aspects as well.
The @EneableRetry annotation enables the spring retry feature in the application. We can apply it to any @Confguration class. The @EnableRetry scan for all @Retryable and @Recover annotated methods and proxies them using AOP. It also configures RetryListener interfaces used for intercepting methods used during retries.
In spring-retry, we can retry the operations using the following annotations for the declarative approach.
3.3. @Retryable
It indicates a method to be a candidate for retry. We specify the exception type for which the retry should be done, the maximum number of retries and the delay between two retries using the backoff policy.
Please note that maxAttampts includes the first failure also. In other words, the first invocation of this method is always the first retry attempt. The default retry count is 3.
It specifies the fallback method if all retries fail for a @Retryable method. The method accepts the exception that occurred in the Retryable method and its arguments in the same order.
In this example, we created a Spring boot project to expose one sample Rest API which will call one backend operation. The backed operation is prone to failure, and we will fail it randomly to simulate the service failures.
The BackendAdapterImpl class implements both @Retryable and @Recover methods as declared in BackendAdapter interface. We should always use the retry annotations on interfaces, so the implementations are automatically retry enabled.
@Slf4j@ServicepublicclassBackendAdapterImplimplementsBackendAdapter{@OverridepublicStringgetBackendResponse(String param1,String param2){int random =newRandom().nextInt(4);if(random %2==0){thrownewRemoteServiceNotAvailableException("Remote API failed");}return"Hello from remote backend!!!";}@OverridepublicStringgetBackendResponseFallback(RemoteServiceNotAvailableException e,String param1,String param2){return"Hello from fallback method!!!";}}
In the above code, the backend service will be retried 3 times. In those 3 attempts, if we get success response then that successful response will be returned else the fallback method will be called.
Create one simple RestController that will call the BackendAdapter.getBackendResponse() method where we have simulated the exception.
This tutorial taught us to use the spring retry module for implementing retry-based remote API invocations that can handle infrequent runtime exceptions or network failures.
A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Comments