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

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.

主站蜘蛛池模板: 上思县| 龙川县| 潼关县| 永顺县| 汉源县| 疏附县| 英吉沙县| 葫芦岛市| 嘉荫县| 祥云县| 攀枝花市| 九龙坡区| 马龙县| 蚌埠市| 准格尔旗| 霍林郭勒市| 榆社县| 荣昌县| 望奎县| 尼玛县| 潮安县| 花莲市| 阳春市| 台北县| 呼和浩特市| 宕昌县| 漳州市| 南宫市| 榆中县| 土默特右旗| 封开县| 洛浦县| 将乐县| 蒲城县| 达尔| 芦溪县| 南平市| 栾川县| 五指山市| 榕江县| 英吉沙县|