java Reformation onFailure()方法调用次数
我正在使用改型,并为远程API调用失败的场景实现了重试逻辑。我创建了以下类,我从以下线程中获取: Retrying the request using Retrofit 2
public abstract class CallbackWithRetry<T> implements Callback<T> {
private static final int TOTAL_RETRIES = 3;
private int retryCount = 0;
@Override
public void onFailure(Call<T> call, Throwable t) {
if (retryCount++ < TOTAL_RETRIES) {
log.info("Retrying... (" + retryCount + " out of " + TOTAL_RETRIES + ")");
retry(call);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void retry(Call<T> call) {
call.clone().enqueue(this);
}
}
我是这样用的:
call.enqueue(new CallbackWithRetry<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
log.info("Response received from remote API: {}", response.body());
}
});
它按预期工作,但我在日志中看到以下内容,我试图理解原因:
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (1 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (1 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (1 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (1 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (1 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (2 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (2 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (2 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (2 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (2 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (3 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (3 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (3 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (3 out of 3)
INFO [OkHttp http://localhost:8081/...] (CallbackWithRetry.java:16) - Retrying... (3 out of 3)
onFailure方法是否通过改造执行/调用了5次?为什么
共 (0) 个答案