【问题标题】:RxJava2 - Emit onError on parallel rail will get UndeliverableExceptionRxJava2 - 在平行轨道上发出 onError 将得到 UndeliverableException
【发布时间】:2018-04-24 17:18:32
【问题描述】:

虽然其中一个轨道发出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);
            }

        });

【问题讨论】:

    标签: java rx-java rx-java2


    【解决方案1】:

    我找到了解决方案。我需要添加一个全局错误消费者来解决问题。 https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling

    【讨论】:

      【解决方案2】:

      在您调用onError() 的那一刻,整个下游观察者链将结束。之后你不能做onNext()onError()onComplete(),因为没有东西可以交付。这就是所谓的“反应式合约”。

      【讨论】:

      • 感谢您的回复。我知道规则,在onError() 用于一些测试之后,我发出onNext()onComplete()。我编辑了代码以符合规则,但问题仍然存在
      猜你喜欢
      • 1970-01-01
      • 2017-09-17
      • 2018-02-27
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      相关资源
      最近更新 更多