失敗重試工具類

本工具類主要有兩種使用方式,一種是讓方法變爲在任何地方調用失敗都會重試,另一種是在調用的地方設置調用方法或一段代碼失敗重試。第一種方式:在需要發生異常重試的方法第一行插入像 if (Retry.simpleRunCurrentMethod(target, arg1, arg2 /*...*/, argn).isSuccess()) return;的代碼就可以讓當前方法發生異常重試,不需要手動try catch,target是調用當前方法的對象,arg1到argn是當前方法傳入的參數,第二種方式:Retry.run(() -> getString("test6"), 5); 把要重試的方法通過Supplier包裹或lambda或方法引用傳入進行異常重試,run有四個重載反方法,可以傳入重試次數、異常處理器、需要重試的代碼。

使用示例:

// not return value
void test1(String arg) {
    if (Retry.simpleRunCurrentMethod(this, arg).isSuccess()) return;

    if (new Random().nextBoolean()) {
        throw new RuntimeException("run test1 throw");
    }
    System.out.println("test:" + arg);
}


// return value
String test2(String arg) {
    Retry retry = Retry.simpleRunCurrentMethod(this, arg)
    if (retry .isSuccess()) return (String)service.getResult();

    if (new Random().nextBoolean()) {
        throw new RuntimeException("run test2 throw");
    }
    return "test:" + arg;
}

// other way
void test3() {
        String result = Retry.run(() -> getString("test6"), 5);
        System.out.println(result);
}

String getString(String str) {
        if (Math.random() < 0.5) {
            throw new RuntimeException("run getString throw");
        }
        return str;
}

更多用法看下面Tester.java所示,代碼github:https://github.com/RuiHeHubGit/throwrerun

運行Tester結果如下圖,日誌輸出可以關閉:

Tester.java

import java.util.Objects;

public class Tester {
    private static int num;

    public static void main(String[] args) {
        if (Retry.getInstance(null, new Object[]{args}).runCurrentMethod()) return;

        System.out.println("main");
        test1();
        test2("test2");
        test3("test3", null);
        Tester tester = new Tester();
        System.out.println(tester.test4() + tester.test4());
        test5();
        test6();
        test7();
    }

    static int c = 0;

    private static void test1() {
        if (Retry.simpleRunCurrentMethod(null).isSuccess()) return;

        if (c++ < 2) {
            test1();
        }
        if (num < 1) {
            num = 1;
            throw new RuntimeException("run test1 throw");
        }
        System.out.println("test1" + c);
        num = 0;

    }

    private static void test2(String arg) {
        if (Retry.simpleRunCurrentMethod(null, arg).isSuccess()) return;
        if (num < 2) {
            num = 2;
            throw new RuntimeException("run test2 throw");
        }
        System.out.println("test2:" + arg);
    }

    private static void test3(String arg1, String arg2) {
        if (Retry.getInstance(null, arg1, arg2)
                .setThrowHandler((service, t) -> service.updateArguments(String.valueOf(num = 3), "3"))
                .setRetryTotal(1)
                .runCurrentMethod()) return;

        if (!Objects.equals(arg1, arg2)) {
            throw new RuntimeException("run test3 throw");
        }
        System.out.println("test3:" + arg1 + "," + arg2);
    }

    public String test4() {
        Retry service = Retry.simpleRunCurrentMethod(this);
        if (service.isSuccess()) {
            return (String) service.getResult();
        }

        if (num < 4) {
            num = 4;
            throw new RuntimeException("run test4 throw");
        }
        return "test4";
    }

    public static void test5() {
        String result = Retry.run(() -> getString("test5"));
        System.out.println(result);
    }

    public static void test6() {
        String result = Retry.run(() -> getString("test6"), 5);
        System.out.println(result);
    }

    public static void test7() {
        String result = Retry.run(() -> getString("test7"), (service, t) -> System.out.println(t.getMessage()));
        System.out.println(result);
    }

    private static String getString(String str) {
        if (Math.random() < 0.5) {
            throw new RuntimeException("run getString throw");
        }
        return str;
    }

}

 

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