In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Spring's RetryTemplate". In the operation of practical cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In cloud computing scenarios, the network is an important part. In fact, the network environment is not as stable as the network of the development environment, so in cloud computing, the network is unreliable has become a default hidden rule. In the process of system development, under the necessary premise of meeting the normal business requirements, the robustness and fault tolerance of the system has also become an important technical requirement.
In the unreliable environment of the network, in order to ensure the business process, it is necessary to retry the process exception when the network is abnormal.
The Spring framework provides us with a retry mechanism, so let's try the retry of Spring.
Maven Dependency
Assuming that the project is managed by Maven, you need to increase the dependency on the spring-retry package in the home pom.
Org.springframework.retry
Spring-retry
Create Spring Retry Template
Create a Bean configuration class to manage bean, use @ EnableRetry to enable Spring retry, and create a RetryTemplate to join Spring Container with the @ Bean annotation. Configure a maximum number of retries of 4.
@ Configuration
@ EnableRetry
Public class BeanSeederServices {
@ Bean
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
}
}
Build retry logic
Create a Service, use the RetryTemplate we built, and hand over the business logic that needs to be retried to RetryTemplate.
@ Service
Public class ConfigureNetworkService
{
@ Autowired
Private RetryTemplate retryTemplate
Int counter = 0
Private void configureNetworkSystem () {
RetryTemplate.execute (
Context-> {
VerifyNwConfiguration ()
Return true
});
}
Private void verifyNwConfiguration () {
Counter++
LOGGER.info ("N configuration Service Failed W configuration Service Failed" + counter)
Throw new RuntimeException ()
}
}
Create a Controller to provide the execution entry and trigger our retry function through url.
@ RestController
@ RequestMapping (value= "/ networksrv")
Public class NetworkClientService {
@ Autowired
Private ConfigureNetworkService configureNetworkService
@ GetMapping
Public String callRetryService () throws SQLException {
Return configureNetworkService.configureNetworkSystem ()
}
}
We will see the retry log from the Spring output on the console
2020-06-1609 c.e.springretrydemo.NetworkClientService 51.399 INFO 17288-[nio-8080-exec-1] c.e.springretrydemo.NetworkClientService: NMague W configuration Service Failed 1
2020-06-16 09 c.e.springretrydemo.NetworkClientService 59 configuration Service Failed 52.401 INFO 17288-[nio-8080-exec-1] c.e.springretrydemo.NetworkClientService: Namp W configuration Service Failed 2
2020-06-1609 c.e.springretrydemo.NetworkClientService 53.401 INFO 17288-[nio-8080-exec-1] c.e.springretrydemo.NetworkClientService: n Accord W configuration Service Failed 3
2020-06-1609 c.e.springretrydemo.NetworkClientService 53.402 INFO 17288-[nio-8080-exec-1] c.e.springretrydemo.NetworkClientService: Namp W configuration Service Failed 4
This is the end of Exception in thread "NetworkClientService" java.lang.RuntimeException "how to use Spring's RetryTemplate". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.