Skip to content

Instantly share code, notes, and snippets.

@pyadav
Forked from djensen47/rxjava-retrywith.java
Created August 8, 2016 03:58
Show Gist options
  • Select an option

  • Save pyadav/1d48db59bcaa38068d2e87bdd2bafa10 to your computer and use it in GitHub Desktop.

Select an option

Save pyadav/1d48db59bcaa38068d2e87bdd2bafa10 to your computer and use it in GitHub Desktop.
RxJava retryWith example
// slide 75 of this: http://www.slideshare.net/Couchbase/reactive-programmingrxjavaefficientdata-benchristensenmichaelnitschinger
Observable.
.defer(() -> bucket.get("id")) //create new
.retryWhen(attempts -> attempts
.zipWith(Observable.range(1,3), (n, i) -> i) // retry maximum of 3 times
.flatMap(i -> {
System.out.println("Delaying retry by " + i + " second(s)");
return Observable.timer(i, TimeUnit.SECONDS); //delay the resubscribe
})
)
.toBlocking()
.forEach(System.out::println);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment