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

To view or add a comment, sign in

More articles by Bukhori Aqid

  • Anti-Pattern: How obvious are they?

    For the past year, I've been involved in the development of a large java server-side monolith that slowly morphed into…

    7 Comments
  • Flipbox's contributions to Web Technology

    We love to use open source libraries, so we thought it's a must to contribute whatever we can for the communities, and…

  • [Android] Handling screen state using Sosoito

    Prologue : How many times that you, as an Android developer initiate a progress dialog for asynchronous operation (…

  • Using Katalon Studio for Automated Testing Creation

    Prologue : Since last year, we've been using several tools for automated testing ( Postman Test Runner, Selenium IDE…

  • Progressive Web App with VueJS

    Prologue : We use VueJS as our main frontend framework ( starting from late 2016 ). Previously we use AngularJS or good…

  • Kickstart your Android Development!

    At my early days as Android Developer, I love finding different methods to speed up my Android development process. Due…

  • [Android] Implement DeepLinking using HOKO

    First of all, what's mobile deep link and when do I need it? From wikipedia : In the context of mobile apps, deep…

  • [Android] DataBinding Library + Espresso : IllegalAccessError

    Problem : When creating espresso test for our new project using Data Binding Library, we keep on encountering…

    1 Comment
  • [Android] Missing ViewPager after resuming Fragment

    Problem : When returning back from other activity / fragment, a viewpager inside the current fragment is not resumed…

    7 Comments
  • Test your API using Postman Test Script

    Prologue : Having a different set of web developers in Flipbox sometimes resulting in different style and structure of…

Others also viewed

Explore content categories