@Test public void testRxRetry() throws Exception { // see https://github.com/Netflix/Hystrix/issues/1100 // Since each command instance is single-use, the expectation is that applying the .retry() operator // results in only a single execution and propagation out of that error HystrixCommand<Integer> cmd = getLatentCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.FAILURE, 300, AbstractTestHystrixCommand.FallbackResult.UNIMPLEMENTED, 100); final CountDownLatch latch = new CountDownLatch(1); System.out.println(System.currentTimeMillis() + " : Starting"); Observable<Integer> o = cmd.toObservable().retry(2); System.out.println(System.currentTimeMillis() + " Created retried command : " + o); o.subscribe(new Subscriber<Integer>() { @Override public void onCompleted() { System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : OnCompleted"); latch.countDown(); } @Override public void onError(Throwable e) { System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : OnError : " + e); latch.countDown(); } @Override public void onNext(Integer integer) { System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : OnNext : " + integer); } }); latch.await(1000, TimeUnit.MILLISECONDS); System.out.println(System.currentTimeMillis() + " ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()); }
public static void main(String... args) { /* * retry(n) can be used to immediately retry n times */ Observable.create(s -> { System.out.println("1) subscribing"); s.onError(new RuntimeException("1) always fails")); }).retry(3).subscribe(System.out::println, t -> System.out.println("1) Error: " + t)); System.out.println(""); /* * retryWhen allows custom behavior on when and if a retry should be done */ Observable.create(s -> { System.out.println("2) subscribing"); s.onError(new RuntimeException("2) always fails")); }).retryWhen(attempts -> { return attempts.zipWith(Observable.range(1, 3), (n, i) -> i).flatMap(i -> { System.out.println("2) delay retry by " + i + " second(s)"); return Observable.timer(i, TimeUnit.SECONDS); }).concatWith(Observable.error(new RuntimeException("Failed after 3 retries"))); }).toBlocking().forEach(System.out::println); } }
/** * The data stream fails intermittently so return the stream * with retry capability. */ public Observable<String> retry(Observable<String> data) { return data.retry(); }
.retry() // Retry when there is an error in timer. .concatMap(new IdleConnectionCleanupTask()) .onErrorResumeNext(new Func1<Throwable, Observable<Void>>() {
public static void subscribe(Observable<String> o) { o = o.materialize().flatMap(n -> { if (n.isOnError()) { if (n.getThrowable() instanceof IllegalStateException) { return Observable.just(n); } else { return Observable.error(n.getThrowable()); } } else { return Observable.just(n); } }).retry().dematerialize(); o.subscribe(System.out::println, t -> t.printStackTrace()); } }
private void performCheckOutLatestEncounter() { Timber.i("Checking out..."); String checkInId = getLatestEncounterCheckInId(); if (checkInId == null) { Timber.e("Cannot check-out because latest encounter check-in ID is null"); return; } if (mCheckInSubscription != null) mCheckInSubscription.unsubscribe(); mCheckInSubscription = mDataManager.checkOut(checkInId) .retry(3) .subscribeOn(Schedulers.io()) .subscribe(new Subscriber<CheckIn>() { @Override public void onCompleted() { Timber.i("Checked out successfully!"); } @Override public void onError(Throwable e) { Timber.e(e, "There was an error checking out"); } @Override public void onNext(CheckIn checkIn) { } }); }
@Override public Observable<GithubAuthorization> observable() { return super.observable().retry(0); }
private Observable<String> getServicesFromDiscoveryClient() { logger.info("Discovering services"); return Observable.from(discoveryClient.getServices()).subscribeOn(Schedulers.io()).publish().autoConnect() .map(String::toLowerCase) .doOnNext(s -> logger.debug("Service discovered: " + s)) .doOnError(e -> { String error = "Error retrieving services: " + e.getMessage(); logger.error(error); publisher.publishEvent(new SystemEvent(error, e)); }) .retry(); }
protected Observable<String> getServiceIdsFromDiscoveryClient() { logger.info("Discovering services for mappings"); return Observable.from(discoveryClient.getServices()).subscribeOn(Schedulers.io()).publish().autoConnect() .map(id -> id.toLowerCase()) .filter(id -> !id.equals(ZUUL)) .doOnNext(s -> logger.debug("Service discovered: " + s)) .doOnError(e -> errorHandler.handleSystemError("Error filtering services: " + e.getMessage(), e)) .retry(); }
protected Observable<String> getServiceIdsFromDiscoveryClient() { logger.info("Discovering services for health"); return Observable.from(discoveryClient.getServices()).subscribeOn(Schedulers.io()).publish().autoConnect() .map(id -> id.toLowerCase()) .filter(id -> !id.equals(ZUUL)) .doOnNext(s -> logger.debug("Service discovered: " + s)) .doOnError(e -> errorHandler.handleSystemError("Error filtering services: " + e.getMessage(), e)) .retry(); }
private Observable<Document> getWebPage(String url) { return Observable.<Document>create(subscriber -> { try { Document document = Jsoup.connect(url).get(); subscriber.onNext(document); subscriber.onCompleted(); } catch (IOException e) { subscriber.onError(e); } }).retry(2); }
@PostConstruct public void start() { if (subscription == null || subscription.isUnsubscribed()) { subscription = Observable.interval(pollingIntervalSeconds, SECONDS, scheduler) .doOnNext(this::onFront50Request) .flatMap(tick -> front50.getPipelines()) .doOnError(this::onFront50Error) .retry() .subscribe(this::cachePipelines); } }
public void setup() { subscriptions.add( Observable.interval(initialDelay, sendInterval, TimeUnit.MILLISECONDS, timerScheduler) .flatMap(time -> persistence.getAll() .first()) .filter(events -> events.size() > 0) .flatMapCompletable(events -> sendEvents(new ArrayList<>(events))) .doOnError(throwable -> crashReport.log(throwable)) .retry() .subscribe()); }
@Override public void connectTo(final Example_7a_Model model) { model.getLogs() .doOnError(throwable -> vm2v_log.onNext(new LogRow("!", "Unexpected error -> will retry here in ViewModel", "Error: " + throwable.getMessage()))) .retry(5) .onErrorReturn(throwable -> new LogRow("!!!", "Too many unexpected errors -> stop", "Error: " + throwable.getMessage())) .subscribe(vm2v_log); }
public Observable<K> observable() { return getApiObservable(getRestAdapter()).retry(this::retry).debounce(100, TimeUnit.MILLISECONDS); }
@Test public void sample_281() throws Exception { risky() .timeout(1, SECONDS) .doOnError(th -> log.warn("Will retry", th)) .retry() .subscribe(log::info); }
@Test public void sample_296() throws Exception { Observable .defer(() -> risky()) .retry(); }
@Test public void sample_303() throws Exception { risky() .timeout(1, SECONDS) .retry(10); }
@Test public void sample_291() throws Exception { risky().cache().retry(); //BROKEN }
@Test public void sample_310() throws Exception { risky() .timeout(1, SECONDS) .retry((attempt, e) -> attempt <= 10 && !(e instanceof TimeoutException)); }