Skip to content

Instantly share code, notes, and snippets.

@benjchristensen
Created August 20, 2014 17:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjchristensen/3363d420607f03307dd0 to your computer and use it in GitHub Desktop.
Save benjchristensen/3363d420607f03307dd0 to your computer and use it in GitHub Desktop.
RetryWhen Example
delay retry by 1 second(s)
delay retry by 2 second(s)
delay retry by 3 second(s)
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
public class RetryWhenTests {
public static void main(String[] args) {
Observable.create((Subscriber<? super String> s) -> {
s.onError(new RuntimeException("always fails"));
}).retryWhen(attempts -> {
return attempts.zipWith(Observable.range(1, 3), (n, i) -> i).flatMap(i -> {
System.out.println("delay retry by " + i + " second(s)");
return Observable.timer(i, TimeUnit.SECONDS);
});
}).toBlocking().forEach(System.out::println);
}
}
@jhalterman
Copy link

@benjchristensen When running this over an Observable.range(1, 10), I noticed the rx thread pool starts with 2 threads and grows up to 8, which is unexpected since this is the only callback/timer being used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment