尽管其中一条导轨发出onError()
,但另一条导轨中的isCancelled()
仍返回false,从而导致UndeliverableException
。如何检查下游是否取消了平行导轨?
Disposable disposable = Flowable.create(new FlowableOnSubscribe<Integer>() {
@Override
public void subscribe(FlowableEmitter<Integer> emitter) throws Exception {
System.out.println("Flowable.create-emitter.isCancelled:" + emitter.isCancelled());
for (int i = 1; i < 10; i++) {
emitter.onNext(i);
}
emitter.onComplete();
}
}, BackpressureStrategy.BUFFER).parallel(6).runOn(Schedulers.io())
.flatMap(new Function<Integer, Publisher<String>>() {
@Override
public Publisher<String> apply(Integer t) throws Exception {
// TODO Auto-generated method stub
return Flowable.create(new FlowableOnSubscribe<String>() {
@Override
public void subscribe(FlowableEmitter<String> emitter) throws Exception {
System.out.println("flatMap-before onError-isCancelled:" + emitter.isCancelled());
try {
if (true) { // assume trigger the error
throw new Exception("Test");
}
if (!emitter.isCancelled()) {
emitter.onNext(String.valueOf((t + 1)));
emitter.onComplete();
}
} catch (Exception ex) {
if (!emitter.isCancelled()) {
emitter.onError(ex);
}
}
System.out.println("flatMap-after onError-isCancelled:" + emitter.isCancelled());
}
}, BackpressureStrategy.BUFFER);
}
}).sequential().subscribeOn(scheduler).observeOn(Schedulers.single())
.subscribeWith(new ResourceSubscriber<String>() {
public void onComplete() {
System.out.println("onComplete");
}
public void onError(Throwable arg0) {
System.out.println("onError:" + arg0.toString());
}
public void onNext(String arg0) {
System.out.println("onNext:" + arg0);
}
});
最佳答案
我找到了解决方案。我需要添加一个全局错误使用者来解决问题。
https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
关于java - RxJava2-在并行轨道上发出onError将获得UndeliverableException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50007501/