In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the meaning of Retry retry mechanism". In daily operation, I believe many people have doubts about what Retry retry mechanism means. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the meaning of Retry retry mechanism?" Next, please follow the editor to study!
1. Preface
In project development, sometimes we can't avoid calling third-party service interfaces, and sometimes we need to retry several times to make the call successful because of network and other factors. so a retry mechanism is needed to ensure normal access to the interface.
2.guava-retrying
This is the toolkit provided by Google guava, and we can introduce the corresponding package into the project to easily achieve retry access. The policy pattern that is widely used in guava-retrying allows you to customize various execution retry policies. Here are the simple and practical steps.
1. Pom introduced com.google.guava guava 19.0 com.github.rholder guava-retrying 2.0.0 ②. Basic business code
I implement a simple access to the interface here to simulate access to the third-party interface.
GetMapping ("/ retrytest") public Map retrytest () {Map resultMap = Maps.newHashMap (); resultMap.put ("id", 1001L); resultMap.put ("msg", "Test"); Map tokenMap = Maps.newHashMap (); tokenMap.put ("token", UUID.randomUUID (). ToString ()); resultMap.put ("data", tokenMap); return resultMap;} ③ .Retryer Bean
The various retry policies are defined in Retryer and will be used one by one when the call method is executed.
RetryListener is a retry listener that listens to the process of each retry.
BlockStrategy is a custom blocking policy.
@ Beanpublic Retryer retry () {RetryListener retryListener = new RetryListener () {@ Override public void onRetry (Attempt attempt) {try {if (attempt.hasException ()) {log.error ("- -" + Arrays.toString (attempt.getExceptionCause (). GetStackTrace () } else {log.info ("- -" + attempt.get () .toString ());}} catch (Exception e) {e.printStackTrace ();} BlockStrategy blockStrategy = new BlockStrategy () {@ Override public void block (long sleepTime) throws InterruptedException {LocalDateTime startTime = LocalDateTime.now (); long start = System.currentTimeMillis (); long end = start; log.info ("[SpinBlockStrategy]... begin wait."); while (end-start)
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.