简单的重试工具方法

87 阅读1分钟
public abstract class AbstractRetry {
    public boolean execute(int num,Long timeoutTime,Long interval) throws InterruptedException {
        Long start=System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            if(System.currentTimeMillis()>start+timeoutTime ){
               return false;
            }
            if(handler()){
                return true;
            }
            System.out.println("失败了:"+(i+1));
            Thread.sleep(interval);
        }
        return false;
    }
    public abstract boolean handler() throws InterruptedException;
}