重试机制
要重试的话,一般采用循环的方式。循环中满足条件则退出循环,同样也可以设置最大重试次数。
Java中的循环体包括,for,while,do...while等。
do…while循环
public Object getValueWithRetry(int maxRetry) {
int time = 0;
Object result = null;
do {
time++;
try {
// 业务逻辑代码
result = new Object();
} catch (Exception e) {
e.printStackTrace();
}
} while (Objects.isNull(result) && time < maxRetry);
return result;
}