java8 function 實現 retry

通過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 , 如果覺得還不錯,請點贊哦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章