Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is Guava-Retryer?

Shulou Source: shulou.com Published: 2022-06-02 02:33:57 09月12日 Update

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)

Tags: Policy duration task time times time interface method result service maximum longest situation problem support simultaneously multiple object more network Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Technology Shulou Information Apple