guava-retry使用笔记

guava-retry使用笔记

xml依赖

xml 复制代码
<dependency>
            <groupId>com.github.rholder</groupId>
            <artifactId>guava-retrying</artifactId>
            <version>2.0.0</version>    
        </dependency>

使用案例

重试3次,每次间隔3秒

java 复制代码
 /**
     * 重试3次,每次间隔3秒
     */
    @Test
    void testGuavaRetry(){
        //定义请求实现
        Callable<Boolean> callable = ()->{
            //do something...
            log.info("call task... ...");
            throw new RuntimeException();
        };

        //定义重试机制
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                //重试条件
                .retryIfResult(Objects::isNull)
                //异常重试源
                .retryIfExceptionOfType(IOException.class)
                .retryIfRuntimeException()
                .retryIfResult(res->res=false)
                //等待间隔时间
                .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS))
                //设置最大重试次数
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .build();

        try {
            retryer.call(callable);
        }catch (RetryException | ExecutionException e){
            e.printStackTrace();
        }
    }

以指数递增的间隔等待重试

java 复制代码
 @Test
    void testGuavaRetry2(){
        Callable<Boolean> callable = ()->{
            //do something...
            log.info("call task... ...");
            throw new RuntimeException();
        };
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                .retryIfExceptionOfType(IOException.class)
                .retryIfRuntimeException()
                //以指数递增的间隔等待,直到最多5分钟,5分钟后,每隔5分钟重试1次
                .withWaitStrategy(WaitStrategies
                        .exponentialWait(100,5,TimeUnit.MINUTES))
                //设置永远重试
                .withStopStrategy(StopStrategies.neverStop())
                .build();
        try {
            retryer.call(callable);
        }catch (RetryException | ExecutionException e){
            e.printStackTrace();
        }
    }

失败后重试,按斐波那契回退间隔重试

java 复制代码
 @Test
    void testGuavaRetry3(){
        Callable<Boolean> callable = ()->{
            //do something...
            log.info("call task... ...");
            throw new RuntimeException();
        };
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                .retryIfExceptionOfType(IOException.class)
                .retryIfRuntimeException()
                //失败后重试,按斐波那契回退间隔,直到2分钟,2分钟后,每隔2分钟重试1次
                .withWaitStrategy(WaitStrategies
                        .fibonacciWait(100,2,TimeUnit.MINUTES))
                //设置永远重试
                .withStopStrategy(StopStrategies.neverStop())
                .build();
        try {
            retryer.call(callable);
        }catch (RetryException | ExecutionException e){
            e.printStackTrace();
        }


    }
相关推荐
im_AMBER2 小时前
CSS 01【基础语法学习】
前端·css·笔记·学习
摇滚侠2 小时前
Spring Boot 3零基础教程,深度理解 Spring Boot 自动配置原理,笔记11
spring boot·笔记·后端
fanstering2 小时前
腾讯混元P3-SAM: Native 3D Part Segmentation
笔记·学习·3d·点云
im_AMBER3 小时前
数据结构 05 栈和队列
数据结构·笔记·学习
报错小能手3 小时前
linux学习笔记(31)网络编程——TCP time_wait机制
linux·笔记·学习
思成不止于此4 小时前
软考中级软件设计师备考指南(四):I/O 技术、安全与可靠性 —— 综合应用篇
网络·笔记·学习·信息安全·总线系统·i/o 技术·可靠性计算
聪明的笨猪猪5 小时前
Java Redis “核心应用” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
聪明的笨猪猪5 小时前
Java Redis “底层结构” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
岑梓铭6 小时前
考研408《计算机组成原理》复习笔记,第七章(1)——I/O接口
笔记·考研·408·计算机组成原理·计组
摇滚侠9 小时前
Spring Boot 3零基础教程,IOC容器中组件的注册,笔记08
spring boot·笔记·后端