Java 改型onFailure()方法调用数
我正在使用改型,并为远程API调用失败的场景实现了重试逻辑。我从以下线程创建了以下类: onFailure方法是否通过改造执行/调用了5次?为什么呢Java 改型onFailure()方法调用数,java,retrofit,okhttp,Java,Retrofit,Okhttp,我正在使用改型,并为远程API调用失败的场景实现了重试逻辑。我从以下线程创建了以下类: onFailure方法是否通过改造执行/调用了5次?为什么呢 public abstract class CallbackWithRetry<T> implements Callback<T> { private static final int TOTAL_RETRIES = 3; private int retryCount = 0; @Overrid
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)