異常發生重試機制。

public static void main(String[] args) {
    String s = tryAgain();
    System.out.println(s);

}

private static  String tryAgain() {
    int retry = 10; //重試次數
    int callCount = 0;
    while (true){
        try {
            //String result = null;
            String result = your method();  //換成你要調用的方法
            if(StringUtils.isNotBlank(result)){
                return result;
            }else{
                throw new Exception("result null");
            }
        } catch (Exception e) {
            callCount++;
            logger.error("http request error {}", e);
            if(callCount > retry){
                return null;
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException var3) {}
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章