How to Use Spring Retry Template
I am going to talk about one of the solutions in Java with spring-context on how to use the Spring Retry Template to handle the failure operations.
Join the DZone community and get the full member experience.
Join For FreeIn the cloud computing world, one of the pivotal parts is networking, the fact is that we cannot avoid network glitches and there will be temporary disconnection for a couple of seconds which makes the operations break while doing network-related configuration from the cloud-native applications. I am going to talk about one of the solutions in Java with spring-context on how to use the Spring Retry Template to handle the failure operations. We will build a small application and see how Spring Retry Template works.
Pre-Requisites
- Spring Boot 2.3.x
- Maven
- IDE STS/Eclipse
Maven Dependency
xxxxxxxxxx
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
Create Spring Retry Template Bean
xxxxxxxxxx
public class BeanSeederServices {
public RetryTemplate retryTemplate() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(4);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(3000);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
}
Create a bean configuration class to manage required beans, use @EnableRetry annotation to enable spring retry and create a RetryTemplate bean, so that this bean can be used throughout the spring boot application to retry the failed operations. We have configured maximum re-try attempts to 4 with SimpleRetryPolicy and having back to back retries can cause locking of the resources, so we should add a BackOff policy to create a gap between retries.
By default, each operation is retried for a maximum of three attempts with no back off in between.
Put Retry-able Logic in the Service
x
public class ConfigureNetworkService
{
private RetryTemplate retryTemplate;
int counter =0;
private void configureNetworkSystem(){
retryTemplate.execute(
context -> {
verifyNwConfiguration();
return true;
});
}
private void verifyNwConfiguration(){
counter++;
LOGGER.info("N/W configuration Service Failed "+ counter);
throw new RuntimeException();
}
}
Execute block keeps executing the callback until it either succeeds or the policy dictates that we stop, in which case the most recent exception thrown by the callback will be rethrown.
Create a REST Endpoint to Test
This client is created just to hit the ConfigureNetworkService configureNetworkSystem() method.
x
value="/networksrv") (
public class NetworkClientService {
private ConfigureNetworkService configureNetworkService;
public String callRetryService() throws SQLException {
return configureNetworkService.configureNetworkSystem();
}
}
Launch the URL and See the Logs
http://localhost:8080/networksrv
x
2020-06-16 09:59:51.399 INFO 17288 --- [nio-8080-exec-1] c.e.springretrydemo.NetworkClientService : N/W configuration Service Failed 1
2020-06-16 09:59:52.401 INFO 17288 --- [nio-8080-exec-1] c.e.springretrydemo.NetworkClientService : N/W configuration Service Failed 2
2020-06-16 09:59:53.401 INFO 17288 --- [nio-8080-exec-1] c.e.springretrydemo.NetworkClientService : N/W configuration Service Failed 3
2020-06-16 09:59:53.402 INFO 17288 --- [nio-8080-exec-1] c.e.springretrydemo.NetworkClientService : N/W configuration Service Failed 4
Exception in thread "NetworkClientService" java.lang.RuntimeException
The logs show it has tried the simpleRetry method 4 times and then throw the Runtime exception.
That's the learning from this blog. Let me know other frameworks or other technical feasibility to retry the failed operations.
Opinions expressed by DZone contributors are their own.
Comments