官术网_书友最值得收藏!

Retry

The last error handling combinator we'll examine is retry. This combinator is useful when we know that an error or exception is only transient, so we should probably give it another shot by resubscribing to the observable.

First, we'll create an observable that fails when it is subscribed to for the first time. However, the next time it is subscribed to, it succeeds and emits a new item:

(defn retry-obs [] 
  (let [errored (atom false)] 
    (rx/observable* 
     (fn [observer] 
       (if @errored 
         (rx/on-next observer 20) 
         (do (reset! errored true) 
             (throw (Exception. "Oops. Something went wrong")))))))) 

Let's see what happens if we simply subscribe to it:

(rx/subscribe (retry-obs) 
              (fn [v] (prn-to-repl "result is " v))) 
 
;; Exception Oops. Something went wrong  rx-playground.core/retry-obs/fn--1476 

As expected, the exception simply bubbles up, as in our first example. However, we know—for the purposes of this example—that this is a transient failure. Let's see what changes if we use retry:

(rx/subscribe (->> (retry-obs) 
                   (.retry)) 
              (fn [v] (prn-to-repl "result is " v))) 
 
;; "result is " 20 

Now, our code is responsible for retrying the observable and, as expected, we get the correct output.

It's important to note that retry will attempt to resubscribe indefinitely until it succeeds. This might not be what you want, so Rx provides a variation, called retryWith, which allows us to specify a predicate function that controls when and if retrying should stop.

All of these operators give us the tools we need to build reliable reactive applications, and we should always keep them in mind since they are, without a doubt, a great addition to our toolbox. The RxJava wiki on the subject should be referred to for more information: https://github.com/ReactiveX/RxJava/wiki/Error-Handling-Operators.

主站蜘蛛池模板: 辽宁省| 临洮县| 台东市| 台南市| 周口市| 乐至县| 千阳县| 颍上县| 大埔区| 商丘市| 静海县| 黄大仙区| 泸州市| 吉隆县| 青铜峡市| 鲁山县| 南和县| 舞阳县| 闵行区| 同德县| 内乡县| 崇州市| 吉木乃县| 曲麻莱县| 和静县| 聂荣县| 平邑县| 英山县| 苏尼特左旗| 吐鲁番市| 隆子县| 龙山县| 昌都县| 蒲城县| 万源市| 庄浪县| 五华县| 许昌市| 双鸭山市| 蒲城县| 汉阴县|