Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What does the Retry retry mechanism mean?

Shulou Source: shulou.com Published: 2022-06-02 09:14:00 09月26日 Update

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)

Tags: Mechanism policy time service interface meaning message problem learning configuration method time annotation third party project test practical maximum success code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Shulou Technology vpn Shulou Tech Info