Reactive Programming with RxJava-Chapter7:Test and Troubleshooting(1)
来源:互联网 发布:数据分析师 泉州 编辑:程序博客网 时间:2024/05/21 11:09
Error Handling
The Reactive Manifesto enumerates four traits that reactive systems should embrace
- responsive
- resilient
- elastic
- message driven
Where Are My Exceptions
In RxJava,failures are just another type of notification.Every Observable is a sequence of events of type T optionally followed by completion or error notification.
Subscribe() only listening for values and not errors is often a bad sign and possibly missed errors.Even if you do not except any exceptions to happen (which is rarely the case),at least place error logging that plugs into your logging framework.
It is advised to explicitly propagate exceptions via subscriber.onError() if possible.Even better,use fromCallable()
Declarative try-catch Replacement
Replacing errors with a fixed result using onErrorReturn()
onErrorReturn() is a fluent and very pleasant to read alternative to a try-catch block that returns fixed result in the catch statement know from imperative style;
Lazily computing fallback value using onErrorResumeNext()
There are two possible scenarios here:
- The primary way of generating a stream of data failed (onError() event,so we switch to a secondaty source that is just as good,but for some reason we treate it as backup)
- In the presence of a failure,we would like to replace real data with some less expensive,more stable,maybe stale information.
The onErrorResumeNext() operator basically replaces error notification with another stream.If you subscribe to an Observable guarded with onErrorResumeNext() in case of failure,RxJava transparently switches from main Observable to the fallback one,specified as an argument.
Timing out When Events Do Not Occur
simplest use case for timeout():timeout(long,TimeUnit)
This is quite common in real-life systems: the initial element of the response has relatively high latency as a result of establishing the connection,SSL handshake,query optimization,or whatever the server is doing.But subsequent responses are either readily available or easily retrievable,so latency between them is much lower;
Similar in behavior to onErrorResumeNext();
Retrying After Failures
The onError notification is terminal;no other event can ever appear in such stream.
Attention
If your Observable is cached or otherwise guaranteed to always return the same sequence of elements,retry() will not work:
risky().cached().retry() //BROKEN
To overcome this issue,you can delay the creation of Observable even futher by using defer()
Observable .defer(() -> risky()) .retry()
Retrying by using delay and limited attempts
retryWhen(Func1)
Attention
The retryWhen operator is similar to retry but decides whether or not to resubscribe to and mirror the source Observable by passing the Throwable from the onError notification to a function that generates a second Observable, and observes its result to determine what to do. If that result is an emitted item, retryWhen resubscribes to and mirrors the source and the process repeats; if that result is an onError notification, retryWhen passes this notification on to its observers and terminates.
Observable.create((Subscriber<? super String> s) -> { System.out.println("subscribing"); 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);
result:
subscribingdelay retry by 1 second(s)subscribingdelay retry by 2 second(s)subscribingdelay retry by 3 second(s)subscribing
Another example:
static final int ATTEMPTS = 11;//....retryWhen(attempts -> attempts .zipWith(Observable.range(1,ATTEMPTS) , (err,attempt) -> attempt < ATTEMPTS ? Observable.timer(1,SECONDS) : Observable.error(err)) .flatMap(x -> x))
最后,安利一款自己写的基于MVP的Android开发框架
https://github.com/sxenon/Pure
欢迎大家拍砖,如果觉得好,麻烦多多star
- Reactive Programming with RxJava-Chapter7:Test and Troubleshooting(1)
- Reactive Programming with RxJava-Chapter3:Operators and Transformations(1)
- Reactive Programming with Rxjava-Chapter6:Flow Control and Backpressure(1)
- Reactive Programming with RxJava-Chapter2:Reactive Extensions
- Reactive Programming with RxJava-Chapter1:Reactive Programming with RxJava
- Reactive Programming with RxJava-Chapter3:Operators and Transformations(2)
- Reactive Programming with RxJava-Chapter3:Operators and Transformations(3)
- Reactive Programming with RxJava-Chapter6:Flow Control and Backpressure(2)
- Reactive Programming with RxJava-Chapter4:Applying Reactive Programming to Existing Application(1)
- Functional Reactive Programming on Android With RxJava
- Reactive Programming with RxJava-Chapter5:Reactive from Top to Bottom(1)
- RxJava实现响应式编程(Reactive Programming with RxJava)
- Reactive Programming with RxJava-Chapter4:Applying Reactive Programming to Existing Application(2)
- Reactive Programming with RxJava,介绍一本书和Rx
- Reactive Programming with RxJava.pdf 英文原版 免费下载
- Reactive Programming with RxJava-Chapter5:Reactive from Top to Bottom(2)
- RxJava(一:functional reactive programming)
- Functional Reactive Programming<1>
- PHP实现菜单无限极分类
- POJ1056
- 最长上升子序列LIS
- 哈夫曼树
- android intent使用示例
- Reactive Programming with RxJava-Chapter7:Test and Troubleshooting(1)
- 一、创建项目
- C++跨平台XML解析类库-pugixml
- Android Studio解决导入项目非常慢的问题
- hadoop的mapreduce任务的执行流程
- [TravelNotes] ZJOI 2017 DAY1 滚粗记
- woowj
- 算法作业_10(2017.3.23第五周)
- Spark商业案例与性能调优实战100课》第36课:彻底解密Spark 2.1.X中Sort Shuffle中Reducer端源码内幕.pptx>>