我是RxJava的新手,但我正在将它整合到一个项目中,我正在努力帮助我学习它。我遇到了一个关于最佳实践的问题。
我有一个关于如何处理onError
以防止停止Observable
处理的问题。
以下是设置:
我有一个userIds列表,我想要做两个或更多的网络请求。如果用户标识的任何网络请求失败,则该用户标识不会被更新并且可以被跳过。这不应该阻止处理其他用户标识。我确实有一个解决方案,但它涉及嵌套订阅(参见第二个代码块)。我看到的一个问题是,如果每次呼叫失败,即使在检测到某个阈值数量失败后,也无法短路并阻止其余部分点击网络资源。
有更好的方法吗?
在传统代码中:
List<String> results = new ArrayList<String>();
for (String userId : userIds) {
try {
String info = getInfo(userId); // can throw an GetInfoException
String otherInfo = getOtherInfo(userId); // can throw an GetOtherInfoException
results.add(info + ", " + otherInfo);
} catch (GetInfoException e) {
log.error(e);
} catch (GetOtherInfoException e) {
log.error(e);
}
}
问题:
伪代码:
userid -> network requests -> result
1 -> a, b -> onNext(1[a ,b])
2 -> a, onError -> onError
3 -> a, b -> onNext(3[a, b])
4 -> a, b -> onNext(4[a, b])
以下是userIds列表和每2个info信息请求的工作示例。如果你运行它,你会发现它会失败(见源代码下面)
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Func1;
public class TestMergeDelayError {
public static Observable<String> getUserIds() {
return Observable.from(new String[]{"1", "2", "3", "4", "5", "6"});
}
public static Observable<String> getInfo(final String prefix, final String integer, final String errorNumber) {
Observable<String> observable = Observable.create(new OnSubscribeFunc<String>() {
public Subscription onSubscribe(Observer<? super String> t1) {
if (integer.contains(errorNumber)) {
t1.onError(new Exception());
} else {
t1.onNext(prefix + integer);
t1.onCompleted();
}
return Subscriptions.empty();
}
});
return observable;
}
public static void main(String[] args) {
Observable<String> userIdObservable = getUserIds();
Observable<String> t = userIdObservable.flatMap(new Func1<String, Observable<String>>() {
public Observable<String> call(final String t1) {
Observable<String> info1 = getInfo("1::: ", t1, "2");
Observable<String> info2 = getInfo("2::: ",t1, "3");
return Observable.mergeDelayError(info1, info2);
}
});
t.subscribe(new Action1<String>() {
public void call(String t1) {
System.out.println(t1);
}
}, new Action1<Throwable>() {
public void call(Throwable t1) {
t1.printStackTrace();
}
},
new Action0(){
public void call() {
System.out.println("onComplete");
}
});
}
}
输出:
1::: 1
2::: 1
2::: 2
java.lang.Exception
at TestMergeDelayError$1.onSubscribe(TestMergeDelayError.java:32)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMergeDelayError$MergeDelayErrorObservable$ParentObserver.onNext(OperationMergeDelayError.java:266)
at rx.operators.OperationMergeDelayError$MergeDelayErrorObservable$ParentObserver.onNext(OperationMergeDelayError.java:210)
at rx.operators.OperationMergeDelayError$2.onSubscribe(OperationMergeDelayError.java:77)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMergeDelayError$MergeDelayErrorObservable.onSubscribe(OperationMergeDelayError.java:171)
at rx.operators.OperationMergeDelayError$1.onSubscribe(OperationMergeDelayError.java:64)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMerge$MergeObservable$ParentObserver.onNext(OperationMerge.java:164)
at rx.operators.OperationMerge$MergeObservable$ParentObserver.onNext(OperationMerge.java:116)
at rx.operators.OperationMap$MapObservable$1.onNext(OperationMap.java:105)
at rx.operators.SafeObserver.onNext(SafeObserver.java:102)
at rx.operators.OperationToObservableIterable$ToObservableIterable.onSubscribe(OperationToObservableIterable.java:94)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMap$MapObservable.onSubscribe(OperationMap.java:102)
at rx.operators.OperationMap$2.onSubscribe(OperationMap.java:76)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMerge$MergeObservable.onSubscribe(OperationMerge.java:106)
at rx.operators.OperationMerge$1.onSubscribe(OperationMerge.java:56)
at rx.Observable.subscribe(Observable.java:241)
at rx.Observable.protectivelyWrapAndSubscribe(Observable.java:320)
at rx.Observable.subscribe(Observable.java:483)
嵌套订阅解决方案:
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Func1;
public class TestMergeDelayError {
public static Observable<String> getUserIds() {
return Observable.from(new String[]{"1", "2", "3", "4", "5", "6"});
}
public static Observable<String> getInfo(final String prefix, final String integer, final String errorNumber) {
Observable<String> observable = Observable.create(new OnSubscribeFunc<String>() {
public Subscription onSubscribe(Observer<? super String> t1) {
if (integer.contains(errorNumber)) {
t1.onError(new Exception());
} else {
t1.onNext(prefix + integer);
t1.onCompleted();
}
return Subscriptions.empty();
}
});
return observable;
}
public static void main(String[] args) {
Observable<String> userIdObservable = getUserIds();
userIdObservable.subscribe(new Action1<String>() {
public void call(String t1) {
Observable<String> info1 = getInfo("1::: ", t1, "2");
Observable<String> info2 = getInfo("2::: ", t1, "3");
Observable.merge(info1, info2).subscribe(new Action1<String>() {
public void call(String t1) {
System.out.println(t1);
}
}, new Action1<Throwable>() {
public void call(Throwable t1) {
t1.printStackTrace();
}
},
new Action0() {
public void call() {
System.out.println("onComplete");
}
});
}
});
}
}
输出:
1::: 1
2::: 1
onComplete
java.lang.Exception
at TestMergeDelayError$1.onSubscribe(TestMergeDelayError.java:28)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMerge$MergeObservable$ParentObserver.onNext(OperationMerge.java:164)
at rx.operators.OperationMerge$MergeObservable$ParentObserver.onNext(OperationMerge.java:116)
at rx.operators.OperationToObservableIterable$ToObservableIterable.onSubscribe(OperationToObservableIterable.java:94)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMerge$MergeObservable.onSubscribe(OperationMerge.java:106)
at rx.operators.OperationMerge$1.onSubscribe(OperationMerge.java:56)
at rx.Observable.subscribe(Observable.java:241)
at rx.Observable.protectivelyWrapAndSubscribe(Observable.java:320)
at rx.Observable.subscribe(Observable.java:483)
at TestMergeDelayError$2.call(TestMergeDelayError.java:47)
at TestMergeDelayError$2.call(TestMergeDelayError.java:42)
at rx.Observable$2.onNext(Observable.java:381)
at rx.operators.SafeObserver.onNext(SafeObserver.java:102)
at rx.operators.OperationToObservableIterable$ToObservableIterable.onSubscribe(OperationToObservableIterable.java:94)
at rx.Observable.subscribe(Observable.java:241)
at rx.Observable.protectivelyWrapAndSubscribe(Observable.java:320)
at rx.Observable.subscribe(Observable.java:367)
at TestMergeDelayError.main(TestMergeDelayError.java:42)
1::: 3
java.lang.Exception
at TestMergeDelayError$1.onSubscribe(TestMergeDelayError.java:28)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMerge$MergeObservable$ParentObserver.onNext(OperationMerge.java:164)
at rx.operators.OperationMerge$MergeObservable$ParentObserver.onNext(OperationMerge.java:116)
at rx.operators.OperationToObservableIterable$ToObservableIterable.onSubscribe(OperationToObservableIterable.java:94)
at rx.Observable.subscribe(Observable.java:241)
at rx.operators.OperationMerge$MergeObservable.onSubscribe(OperationMerge.java:106)
at rx.operators.OperationMerge$1.onSubscribe(OperationMerge.java:56)
at rx.Observable.subscribe(Observable.java:241)
at rx.Observable.protectivelyWrapAndSubscribe(Observable.java:320)
at rx.Observable.subscribe(Observable.java:483)
at TestMergeDelayError$2.call(TestMergeDelayError.java:47)
at TestMergeDelayError$2.call(TestMergeDelayError.java:42)
at rx.Observable$2.onNext(Observable.java:381)
at rx.operators.SafeObserver.onNext(SafeObserver.java:102)
at rx.operators.OperationToObservableIterable$ToObservableIterable.onSubscribe(OperationToObservableIterable.java:94)
at rx.Observable.subscribe(Observable.java:241)
at rx.Observable.protectivelyWrapAndSubscribe(Observable.java:320)
at rx.Observable.subscribe(Observable.java:367)
at TestMergeDelayError.main(TestMergeDelayError.java:42)
1::: 4
2::: 4
onComplete
1::: 5
2::: 5
onComplete
1::: 6
2::: 6
onComplete
正如您所看到的那样,只有失败的单个用户标识会停止其单独处理,但其余的用户标识已被处理。
只是寻求建议,看看这个解决方案是否有意义,如果没有,那么最佳实践是什么。
谢谢, 亚历
答案 0 :(得分:18)
由于您要忽略该错误,可以尝试onErrorResumeNext(Observable.<String>empty());
。例如,
Observable<String> info1 = getInfo("1::: ", t1, "2").onErrorResumeNext(Observable.<String>empty());
Observable<String> info2 = getInfo("2::: ", t1, "3").onErrorResumeNext(Observable.<String>empty());
return Observable.merge(info1, info2);
答案 1 :(得分:9)
最佳做法是使用将多个Observable组合成一个的mergeDelayError( ),允许无错误的Observable在传播错误之前继续。
mergeDelayError
的行为与merge
非常相似。例外情况是合并的其中一个Observable终止时出现onError通知。如果合并时发生这种情况,合并后的Observable将立即发出onError通知并终止。另一方面,mergeDelayError将阻止报告错误,直到它给出任何其他非产生错误的Observable,它正在合并完成发布其项目的机会,并且它将自己发出错误,并且只会终止所有其他合并的Observable都已完成时的onError通知。
答案 2 :(得分:1)
作为一个Rx新手,我也在寻找一个简单的答案,分别处理异常并继续处理下一个事件,但找不到答案 @Daniele Segato要求的是什么。这是一个您无法控制的解决方案:
上面的示例假设您可以控制observable,即,一种方法是使用mergeDelayError将错误延迟到最后,或者使用merge分别将每个事件的已知空事件Observable作为Observable返回。
如果是源事件错误,您可以使用lift创建另一个observable,它基本上可以优雅地处理当前Observable的值。 SimpleErrorEmitter类模拟有时会失败的无界流。
Observable.create(new SimpleErrorEmitter())
// transform errors to write to error stream
.lift(new SuppressError<Integer>(System.err::println))
.doOnNext(System.out::println) // and everything else to console
.subscribe();
class SimpleErrorEmitter implements OnSubscribe<Integer> {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(1);
subscriber.onNext(2);
subscriber.onError(new FooException());
subscriber.onNext(3);
subscriber.onNext(4);
subscriber.onCompleted();
}
class SuppressError<T> implements Operator<T, T> {
final Action1<Throwable> onError;
public SuppressError(Action1<Throwable> onError) {
this.onError = onError;
}
@Override
public Subscriber<? super T> call(Subscriber<? super T> t1) {
return new Subscriber<T>(t1) {
@Override
public void onNext(T t) {
t1.onNext(t);
}
@Override
public void onError(Throwable e) { // handle errors using a separate function
onError.call(e);
}
@Override
public void onCompleted() {
t1.onCompleted();
}
};
}
如果是订阅者处理错误,可以尝试/捕获并正常继续
Observable<Integer> justInts = justStrs.map((str) -> {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
return null;
}
});
我仍在尝试找到一种简单的方法来重试或延迟尝试失败的事件并从下一步继续。
Observable<String> justStrs = Observable
.just("1", "2", "three", "4", "5") // or an unbounded stream
// both these retrying from beginning
// when you delay or retry, if they are of known exception type
.retryWhen(ex -> ex.flatMap(eachex -> {
// for example, if it is a socket or timeout type of exception, try delaying it or retrying it
if (eachex instanceof RuntimeException) {
return Observable.timer(1L, TimeUnit.MICROSECONDS, Schedulers.immediate());
}
return Observable.error(eachex);
}))
// or simply retry 2 times
.retry(2) // if it is the source problem, attempt retry
.doOnError((ex) -> System.err.println("On Error:" + ex));
参考:https://groups.google.com/forum/#!topic/rxjava/trm2n6S4FSc
答案 3 :(得分:1)
查看Observable.flatMap
的来源:
return merge(map(func));
如果您希望处理所有可能的用户ID,您可以继续使用flatMap的修改版本:
Observable.mergeDelayError(userIdObservable.map(userInfoFunc))
此外,如果你说:
如果用户标识的任何网络请求失败,则该用户标识失败 将不会更新,可以跳过
然后不要使用:
return Observable.mergeDelayError(info1, info2);
因为这会导致请求info1和info2,即使其中一个失败也是如此。
相反:
return Observable.merge(info1, info2);
当info1和info2订阅同一个线程时,它们将按顺序运行,因此如果info1失败,将永远不会请求info2。 由于info1和info2是I / O限制的,我假设你想并行运行它们:
getInfo("1::: ", t1, "2").subscribeOn(Schedulers.io());
getInfo("2::: ",t1, "3").subscribeOn(Schedulers.io());
这应该可以显着加快处理速度
整个代码:
public class TestMergeDelayError {
public static Observable<String> getUserIds() {
return Observable.from(new String[]{"1", "2", "3", "4", "5", "6"});
}
public static Observable<String> getInfo(final String prefix, final String integer, final String errorNumber) {
return Observable.create(new OnSubscribeFunc<String>() {
public Subscription onSubscribe(Observer<? super String> t1) {
if (integer.contains(errorNumber)) {
t1.onError(new Exception());
} else {
t1.onNext(prefix + integer);
t1.onCompleted();
}
return Subscriptions.empty();
}
})
.subscribeOn(Schedulers.io());
}
public static void main(String[] args) {
Observable<String> userIdObservable = getUserIds();
Observable<String> t = Observable.mergeDelayError(userIdObservable.map(new Func1<String, Observable<String>>() {
public Observable<String> call(final String t1) {
Observable<String> info1 = getInfo("1::: ", t1, "2");
Observable<String> info2 = getInfo("2::: ",t1, "3");
return Observable.merge(info1, info2);
}
}));
//rest is the same
}
}