Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Guava-Retryer?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is Guava-Retryer? in view of this question, this article introduces in detail the corresponding analysis and answers, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

In daily development, we often encounter scenarios where external services and interfaces need to be invoked. External services are generally unreliable for callers, especially when the network environment is poor, and network jitter can easily lead to abnormal situations such as request timeout. In this case, you need to use the failed retry policy to call the API API to obtain it. The retry strategy is also widely used in service governance, through regular checks to see if the service is Active.

Guava Retrying is a flexible and convenient retry component that contains a variety of retry strategies and is easy to extend.

In the words of the author:

This is a small extension to Google's Guava library to allow for the creation of configurable retrying strategies for an arbitrary function call, such as something that talks to a remote service with flaky uptime.

With Guava-retrying you can customize to perform retries and monitor the results and behavior of each retry. The most important retry method based on Guava style is really convenient.

Code example

Introduction of Guava-retry

2.0.0 com.github.rholder guava-retrying ${guava-retry.version}

Define a method that implements the Callable interface so that Guava retryer can call

Package org.java.base.retry;import java.io.IOException;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.TimeUnit;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.github.rholder.retry.RetryException;import com.github.rholder.retry.Retryer;import com.github.rholder.retry.RetryerBuilder;import com.github.rholder.retry.StopStrategies;import com.github.rholder.retry.WaitStrategies;import com.google.common.base.Predicates Public class RetryTest {private static final Logger logger = LoggerFactory.getLogger (RetryTest.class) Public static void main (String [] args) {Retryer retryer = RetryerBuilder.newBuilder () .retryIfResult (Predicates.isNull ()) / set custom paragraph retry source .retryIfExceptionOfType (Exception.class) / set exception retry source .retryIfRunt imeException () / / set exception retry source .withStopStrategy (StopStrategies.stopAfterAttempt (5)) / / set retry 5 times You can also set the retry timeout. WithWaitStrategy (WaitStrategies.fixedWait (5L, TimeUnit.SECONDS)) / / set the interval of each retry, 5 seconds. Build () Try {retryer.call (new Callable () {int I = 0; @ Override public Boolean call () throws Exception {ionization; logger.info ("the first {} execution!" , I) / / do something if (I)

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: 292

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report