Tinkering with RxJava retryWhen()
Prologue : We have been working on a project that heavily rely on synchronization services, since all of the transaction MUST happen locally and will be synced to server after some period of time.
Problem : With lots of services, comes great responsibilities. We need to ensure that some operation must happened before the others, or some operations must happened simultaneously, and other conditions that we must handle.
I'm going to share one specific problem : retry condition. Since there are several operations happened, the retry condition must be as robust as possible. We don't want one faulty retry condition causing the app to go wrong
Solution : RxJava retryWhen()
Needless to say, we have to user Reactive Programming concept to handle all the concurrent events and convert it into manageable streams. But one thing that is quite hard in particular is setting up the retryWhen() function. There are lots of example using Retrolambda, but I decided not to use that because I'm still new in this paradigm and I want to understand what I'm going to do.
According to the documentation
http://reactivex.io/documentation/operators/retry.html
http://reactivex.io/RxJava/javadoc/rx/Observable.html#retryWhen(rx.functions.Func1,%20rx.Scheduler)
the method should return the source Observable modified with retry logic. Sounds easy but implementing those without Retrolambda proved to be hard.
But fortunately, I found an awesome tool that fits my needs after some searching here and there.
https://github.com/davidmoten/rxjava-extras
If you look at the source, you will find a lot of helper tools that you can use and modify to understand deeper about Rx operations and functionalities.
Especially on retryWhen() , I used some helper methods :
RetryWhen.retryIf(conditions) RetryWhen.maxRetries(maxRetriesNumber)
There are also some other helpful methods
RetryWhen.retryWhenInstanceOf(exception condition) RetryWhen.failWhenInstanceOf(exception condition) RetryWhen.delays(observable delays) RetryWhen.delayInt(obsersable delay in integer) RetryWhen.scheduler(scheduler) RetryWhen.exponentialBackoff(delay)
The repository is a good source for understanding RxJava without using Retrolambda. Of course I will use Retrolambda in the nearby future. After all, the its benefit is clear enough. But for now, I'll use (and try to understand) RxJava without Retrolambda.
Cheerios