博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJava^Android]项目经验分享 --- 失败重试
阅读量:4665 次
发布时间:2019-06-09

本文共 6033 字,大约阅读时间需要 20 分钟。

简单介绍一下业务逻辑:获取字符串,如果获取失败进行10次重试,超出10次未成功视为失败。

模拟获取字符串场景

代码块

class MsgTool {        int count;        String getMsg() throws Exception {            count++;            LogUtils.d("execute getMsg count : " + count);            if (count == 15) {                return "getMsg";            } else {                throw new Exception("exception getMsg");            }        }    }

Java代码实现逻辑(实现方式很多种,这里不是重点)

代码块

public void testMain() {        LogUtils.d("result : " + getSyncMsg());    }    private String getSyncMsg() {        MsgTool msgTool = new MsgTool();        String result = null;        boolean isSuccess = false;        int count = 0;        while ((count < 10) && !isSuccess) {            try {                result = msgTool.getMsg();                isSuccess = true;            } catch (Exception e) {                count++;            }        }        return result;    }

输出结果

23:33:14.908 32364-32377/? D/LogUtils: execute getMsg count : 123:33:14.908 32364-32377/? D/LogUtils: execute getMsg count : 223:33:14.908 32364-32377/? D/LogUtils: execute getMsg count : 323:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 423:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 523:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 623:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 723:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 823:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 923:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 1023:33:14.909 32364-32377/? D/LogUtils: result : null

针对上述业务逻辑改为RxJava实现,使用操作符retry可实现

代码块

public void testMain() {        getSyncMsg().subscribe(getSubscriber());    }    private Observable
getSyncMsg() { MsgTool msgTool = new MsgTool(); Observable
o = Observable.create(subscriber -> { try { subscriber.onNext(msgTool.getMsg()); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } }); return o.retry(10); } private Subscriber
getSubscriber() { return new Subscriber() { @Override public void onCompleted() { LogUtils.d("onCompleted"); } @Override public void onError(Throwable e) { LogUtils.d("onError : " + e.toString()); } @Override public void onNext(Object o) { LogUtils.d("onNext : " + o); } }; }

输出结果

23:45:43.761 3285-3307/? D/LogUtils: execute getMsg count : 123:45:43.762 3285-3307/? D/LogUtils: execute getMsg count : 223:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 323:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 423:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 523:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 623:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 723:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 823:45:43.764 3285-3307/? D/LogUtils: execute getMsg count : 923:45:43.764 3285-3307/? D/LogUtils: execute getMsg count : 1023:45:43.764 3285-3307/? D/LogUtils: execute getMsg count : 1123:45:43.765 3285-3307/? D/LogUtils: onError : java.lang.Exception: exception getMsg

下面我们增加一个业务逻辑,每次重试延迟一秒种。此功能不做Java代码实现(使用定时器、Android系统下使用Handler等),而用RxJava代码实现,虽然看着很迷糊,但是慢慢品味就会发觉它的魅力所在。

public void testMain() {        getSyncMsg().subscribe(getSubscriber());    }    private Observable
getSyncMsg() { MsgTool msg = new MsgTool(); Observable
o = Observable.create(subscriber -> { try { subscriber.onNext(msg.getMsg()); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } }); return o.retryWhen(this::delayRetry); } //此方法就是魅力的所在 private Observable
delayRetry(Observable
o) { return o.zipWith(Observable.range(1, 10), //控制10次以内 (throwable, integer) -> { if (integer == 10) { //如果是最后一次,结合的结果是异常。 return throwable; } else { return integer; } }) .flatMap(object -> Observable.create(subscriber -> { //转换retryWhey发射的数据 if (object instanceof Throwable) { subscriber.onError((Throwable) object); } else { subscriber.onNext(o); subscriber.onCompleted(); } }).delay(1, TimeUnit.SECONDS)); //延迟一秒发射 } private Subscriber getSubscriber() { return new Subscriber() { @Override public void onCompleted() { LogUtils.d("onCompleted"); } @Override public void onError(Throwable e) { LogUtils.d("onError : " + e.toString()); } @Override public void onNext(Object o) { LogUtils.d("onNext : " + o); } }; }

输出结果

00:36:57.271 19355-19372/? D/LogUtils: onStart00:36:57.297 19355-19372/? D/LogUtils: execute getMsg count : 100:36:58.305 19355-19377/? D/LogUtils: execute getMsg count : 200:36:59.306 19355-19404/? D/LogUtils: execute getMsg count : 300:37:00.307 19355-19375/? D/LogUtils: execute getMsg count : 400:37:01.308 19355-19376/? D/LogUtils: execute getMsg count : 500:37:02.308 19355-19377/? D/LogUtils: execute getMsg count : 600:37:03.309 19355-19404/? D/LogUtils: execute getMsg count : 700:37:04.309 19355-19375/? D/LogUtils: execute getMsg count : 800:37:05.310 19355-19376/? D/LogUtils: execute getMsg count : 900:37:06.311 19355-19377/? D/LogUtils: execute getMsg count : 1000:37:06.320 19355-19377/? D/LogUtils: onError : java.lang.Exception: exception getMsg

转载于:https://www.cnblogs.com/assassin-l/p/5088963.html

你可能感兴趣的文章
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
shop--7.店铺编辑和列表--更新店铺的信息,包括对店铺照片的处理,根据shopId获取shop信息...
查看>>
数据结构化与保存
查看>>
C# .net 获取程序运行的路径的几种方法
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
Python入门学习笔记4:他人的博客及他人的学习思路
查看>>
webstorm里直接调用命令行
查看>>
关联规则算法之FP growth算法
查看>>
对数组序列进行洗牌
查看>>
决策树
查看>>
团队作业
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
win7,Ubuntu 12.04 双系统修改启动项顺序三方法
查看>>
python--列表推导式和生成表达式
查看>>
P - Psychos in a Line 单调队列
查看>>
POJ 2653 Pick-up sticks(计算几何)
查看>>