[RxJava^Android]项目经验分享 --- 失败重试
简单介绍一下业务逻辑:获取字符串,如果获取失败进行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 : 1
23:33:14.908 32364-32377/? D/LogUtils: execute getMsg count : 2
23:33:14.908 32364-32377/? D/LogUtils: execute getMsg count : 3
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 4
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 5
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 6
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 7
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 8
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 9
23:33:14.909 32364-32377/? D/LogUtils: execute getMsg count : 10
23:33:14.909 32364-32377/? D/LogUtils: result : null
针对上述业务逻辑改为RxJava实现,使用操作符retry
可实现
代码块
public void testMain() {
getSyncMsg().subscribe(getSubscriber());
}
private Observable<String> getSyncMsg() {
MsgTool msgTool = new MsgTool();
Observable<String> o = Observable.create(subscriber -> {
try {
subscriber.onNext(msgTool.getMsg());
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
});
return o.retry(10);
}
private Subscriber<Object> getSubscriber() {
return new Subscriber<Object>() {
@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 : 1
23:45:43.762 3285-3307/? D/LogUtils: execute getMsg count : 2
23:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 3
23:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 4
23:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 5
23:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 6
23:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 7
23:45:43.763 3285-3307/? D/LogUtils: execute getMsg count : 8
23:45:43.764 3285-3307/? D/LogUtils: execute getMsg count : 9
23:45:43.764 3285-3307/? D/LogUtils: execute getMsg count : 10
23:45:43.764 3285-3307/? D/LogUtils: execute getMsg count : 11
23: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<String> getSyncMsg() {
MsgTool msg = new MsgTool();
Observable<String> o = Observable.create(subscriber -> {
try {
subscriber.onNext(msg.getMsg());
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
}
});
return o.retryWhen(this::delayRetry);
}
//此方法就是魅力的所在
private Observable<Object> delayRetry(Observable<? extends Throwable> 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<Object> getSubscriber() {
return new Subscriber<Object>() {
@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: onStart
00:36:57.297 19355-19372/? D/LogUtils: execute getMsg count : 1
00:36:58.305 19355-19377/? D/LogUtils: execute getMsg count : 2
00:36:59.306 19355-19404/? D/LogUtils: execute getMsg count : 3
00:37:00.307 19355-19375/? D/LogUtils: execute getMsg count : 4
00:37:01.308 19355-19376/? D/LogUtils: execute getMsg count : 5
00:37:02.308 19355-19377/? D/LogUtils: execute getMsg count : 6
00:37:03.309 19355-19404/? D/LogUtils: execute getMsg count : 7
00:37:04.309 19355-19375/? D/LogUtils: execute getMsg count : 8
00:37:05.310 19355-19376/? D/LogUtils: execute getMsg count : 9
00:37:06.311 19355-19377/? D/LogUtils: execute getMsg count : 10
00:37:06.320 19355-19377/? D/LogUtils: onError : java.lang.Exception: exception getMsg
[RxJava^Android]项目经验分享 --- 失败重试的更多相关文章
- [RxJava^Android]项目经验分享 --- 异常方法处理
简单介绍一下背景,最近RxJava很火,我也看来学习一下,计划在项目的独立模块中使用它.使用过程中遇到很多问题,在这里记录分享一下.可能有使用不当的地方,大家多多包涵.对于RxJava的基本概念和功能 ...
- [RxJava^Android]项目经验分享 --- RxLifecycle功能实现分析(一)
最近在研究RxJava自定义操作符的实现原理,发现成型的项目案例较少.突然想起在项目中应用的RxLifecycle是使用自定义操作符,便拿来研究了一下.分析之前,跟大家了解一些相关操作符和RxLi ...
- [RxJava^Android]项目经验分享 --- RxLifecycle功能实现分析(二)
接着上一篇文章的内容,这篇文章一边分析RxLifecycle的实现原理,一边学习RxJava操作符. 首先RxLifecycle在基础类里定义BehaviorSubject并绑定Activity或 ...
- [RxJava^Android]项目经验分享 --- 递归实现
介绍一下业务逻辑:获取接口数据,根据接口内容判断是否需要继续获取数据. 本文使用递归思路,通过RxJava来实现此功能,获取数据的Observable直接用模拟的Observable.just()替代 ...
- 项目经验分享[转自min.jiang]
最近三个月,我非常荣幸的做为TeamLeader带领几个小组成员做了一个国外项目,这里想为大家分享一些小经验,尽管我佣有六年多的项目经验,但我一直的方向是架构师.大家知道架构师一般情况是偏向技 ...
- Georgia Tech Online Master of Science in Computer Science 项目经验分享
Georgia Tech Online Master of Science in Computer Science 项目经验分享 Posted on 2014/04/22 项目关键词:工科名校,计算机 ...
- IdentityServer4系列之中文文档及实际项目经验分享
0.前言 原文:http://docs.identityserver.io/en/release/声明: 1.目录一至五章节根据IdentityServer英文文档翻译而来,有些内容会根据自己的理解来 ...
- Unity多媒体展示项目经验分享-ImageEffect+动态绑定
Unity多媒体展示项目经验分享-ImageEffect+动态绑定+网络通信 <ignore_js_op> “海尔科技展墙”是去年年初我们为上海家电博览会制作的一个多媒体展项,有限的工期以 ...
- 使用Webpack+Gulp开发运行于Dcloud平台HTML5+引擎的混合APP项目经验分享
什么是5+Runtime? 首先简单介绍一下5+Runtime: HTML5 Plus Runtime(5+Rumtime)是由Dcloud开发的一套"增强版的手机浏览器引擎",与 ...
随机推荐
- instanceof操作符
instanceof是Java.php的一个二元操作符(运算符),和==,>,<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实 ...
- C#中的LINQ
从自己的印象笔记里面整理出来,排版欠佳.见谅! 1.LINQ: 语言集成查询(Language Integrated Query) 实例: var q= from c in catego ...
- bzoj violet系列 (2708~2725)
cbh大爷说:写博客不能弃坑. orz cbh 那我就来更新博客了. violet这个系列的题好神啊……出题人好劲啊…… ……怎么最近都在理性愉悦啊…… 另外bzoj400题纪念~ 2708: [Vi ...
- Debian8升级4.5内核
本文讲述如何升级Debian8的内核到4.5版本 0x01:去linux kernel官网https://www.kernel.org/下载4.5的内核,选择tar.xz格式 0x02:想办法把下载好 ...
- 《jQuery知识点总结》(一)
write less do more写更少的代码实现更多的功能DOM:document object model (文件对象模型)选择器(选择元素的对象或者节点)id 选择器 $("#id& ...
- python scipy学习-曲线拟合
根据某地每月的平均温度[17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18]拟合温度函数. import numpy as np import matplot ...
- 【Tomcat】解决Eclipse无法添加Tomcat Service问题
直接上图:今天因为弄Maven的时候,不小心把Tomcat7 Service 给弄没了,没法直接添加. 可以参照上图的结构进行 Download and Install...点击之后等待一会儿. 其实 ...
- hosts 屏蔽百度
127.0.0.1 localhost cpro.baidu.com vie.baidu.com cpro.baidu.com ubmcmm.baidustatic.com uumcmm.ba ...
- c++ 陷阱
.c89 c90 gnu90 c99 c11 c++98( default ) c++03 c++11 gnu++11 boolC 标准 does not support the boolean da ...
- 提取postgresql数据库中jsonb列的数据
; SELECT t.errmsg,sms_records.* FROM sms_records, jsonb_to_record(result_json) AS t(errmsg text,sid ...