Created
June 9, 2015 20:57
-
-
Save defHLT/59143e2ef85306bddb5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Created by ice on 5/3/15. | |
| */ | |
| public class RetryWithDelay implements Func1<Observable<? extends Throwable>, Observable<?>> { | |
| private final int maxRetries; | |
| private final int retryDelayMillis; | |
| private int retryCount; | |
| public RetryWithDelay(final int maxRetries, final int retryDelayMillis) { | |
| this.maxRetries = maxRetries; | |
| this.retryDelayMillis = retryDelayMillis; | |
| this.retryCount = 0; | |
| } | |
| @Override | |
| public Observable<?> call(final Observable<? extends Throwable> attempts) { | |
| return attempts | |
| .flatMap(new Func1<Throwable, Observable<?>>() { | |
| @Override | |
| public Observable<?> call(Throwable throwable) { | |
| if (++retryCount < maxRetries) { | |
| // When this Observable calls onNext, the original | |
| // Observable will be retried (i.e. re-subscribed). | |
| Timber.d("retrying"); | |
| return Observable.timer(retryDelayMillis, | |
| TimeUnit.MILLISECONDS); | |
| } | |
| // Max retries hit. Just pass the error along. | |
| return Observable.error(throwable); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment