I want to run a method with retries using RXJava
return Observable
.just(myObj)
.flatMap(doc ->
myFunc(myObj, ....)
)
.doOnError(e -> log.Error())
.onErrorResumeNext(myObj2 ->
methodIWantToRunWithRetries(...)
.onErrorResumeNext(myObj3 ->
methodIWantToRunWithRetries(...)
)
);
}
If I use onErrorResumeNext
I need to nest it as many times I want my retries.
(unless I want to surround it with try/catch)
Is there an option to implement it with RXJava methods?
RxJava offers standard retry operators that let you retry a number of times, retry if the exception matches a predicate or have some complicated retry logic. The first two use are the simplest:
source.retry(5).subscribe(...)
source.retry(e -> e instanceof IOException).subscribe(...);
The latter one requires assembling a secondary observable which now can have delays, counters, etc. attached:
source.retryWhen(o -> o.delay(100, TimeUnit.MILLISECONDS)).subscribe(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With