美文网首页多线程我爱编程
java8 function 实现 retry

java8 function 实现 retry

作者: 良人与我 | 来源:发表于2019-07-20 11:51 被阅读35次

    通过java8 的 function 实现 retry
    可以通过参数配置 retry的次数、时间间隙、和所有尝试都失败后的抛出异常。

    package com.river.reytry;
    
    import java.util.function.Supplier;
    
    /**
     * @author riverfan
     * @date 2019-07-19
     */
    public class RetryHelp {
        private int num = 1;
        private Supplier<Boolean> consumer;
        private long intervalTime = 0;
        private Supplier<RuntimeException> exceptionSupplier;
    
        public static RetryHelp build() {
            return new RetryHelp();
        }
    
        public RetryHelp tryWith(Supplier<Boolean> t) {
            this.consumer = t;
            return this;
        }
    
        public RetryHelp tryInterval(long time) {
            intervalTime = time;
            return this;
        }
    
        public RetryHelp tryNum(int num) {
            this.num = num;
            return this;
        }
    
        public RetryHelp elseThrow(Supplier<RuntimeException> exceptionSupplier) {
            this.exceptionSupplier = exceptionSupplier;
            return this;
        }
    
        public void start() throws RuntimeException {
            for (int i = 0; i < num; i++) {
    
                if (consumer.get()) {
                    return;
                }
                //最后一次不用sleep
                if(i == num-1){
                    continue;
                }
                try {
                    Thread.sleep(intervalTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("no success throw exception");
            if(exceptionSupplier == null){
                return;
            }
            throw exceptionSupplier.get();
        }
    
    
    }
    
    

    是否需要 retry 是通过 tryWith 的表达式所返回的结果决定。
    如果是true 就不用再尝试
    如果是false 就继续尝试
    直到 尝试的次数num 已经超过,抛出异常

    测试

    package com.river.reytry;
    
    public class RetryHelpTest {
    
        public static void main(String[] args) {
            RetryHelp.build()
                    .tryWith(() -> {
                        System.out.println("hello world");
                        return false;
                    })
                    .tryInterval(1000L)
                    .tryNum(3)
                    .elseThrow(() -> new RuntimeException("some thing wrong"))
                    .start();
        }
    }
    

    结果如下

    hello world
    hello world
    hello world
    Exception in thread "main" java.lang.RuntimeException: some thing wrong
        at com.river.reytry.RetryHelpTest.lambda$main$1(RetryHelpTest.java:11)
        at com.river.reytry.RetryHelp.start(RetryHelp.java:46)
        at com.river.reytry.RetryHelpTest.main(RetryHelpTest.java:12)
    no success throw exception
    
    Process finished with exit code 1
    

    写个100次的循环,看随机出现3次都是false 的情况会不会

    public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                System.out.println("--------------");
                RetryHelp.build()
                        .tryWith(() -> {
                            System.out.println("hello world");
                            return RandomUtils.nextBoolean();
                        })
                        .tryInterval(1L)
                        .tryNum(3)
                        .elseThrow(() -> new RuntimeException("some thing wrong"))
                        .start();
            }
        }
    

    结果如下

    --------------
    hello world
    hello world
    --------------
    hello world
    --------------
    hello world
    --------------
    hello world
    --------------
    hello world
    hello world
    --------------
    hello world
    hello world
    hello world
    no success throw exception
    Exception in thread "main" java.lang.RuntimeException: some thing wrong
        at com.river.reytry.RetryHelpTest.lambda$main$1(RetryHelpTest.java:17)
        at com.river.reytry.RetryHelp.start(RetryHelp.java:59)
        at com.river.reytry.RetryHelpTest.main(RetryHelpTest.java:18)
    
    Process finished with exit code 1
    

    ok , 如果觉得还不错,请点赞哦。

    java8 function 实现 retry

    相关文章

      网友评论

        本文标题:java8 function 实现 retry

        本文链接:https://www.haomeiwen.com/subject/qdrelctx.html