Created
August 20, 2014 17:24
-
-
Save benjchristensen/3363d420607f03307dd0 to your computer and use it in GitHub Desktop.
RetryWhen Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delay retry by 1 second(s) | |
delay retry by 2 second(s) | |
delay retry by 3 second(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.