Apply replays in RXjava

I want to run a method with repetitions 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 onErrorResumeNext it as many times as needed. (if I don't want to surround it with try / catch)

Is it possible to implement it using RXJava methods?

+4
source share
1 answer

RxJava offers standard repeat operators that allow you to repeat several times, repeat if the exception matches the predicate or has complex repeat logic. The first two uses are the simplest:

 source.retry(5).subscribe(...) source.retry(e -> e instanceof IOException).subscribe(...); 

The latter requires the assembly of a secondary observable, which can now have delays, counters, etc .:

 source.retryWhen(o -> o.delay(100, TimeUnit.MILLISECONDS)).subscribe(...) 
+7
source

All Articles