Adapting RxJava 1.1.5 to Reactor Core 3.1.0.M3

给你一囗甜甜゛ 提交于 2019-12-11 06:39:19

问题


I am trying to use a library that uses RxJava 1.1.5 with Spring WebFlux (i.e. Reactor Core 3.1.0.M3) but I am having trouble adapting Observable to Flux.

I thought this would be relatively straightforward, but my adapter isn't working:

import reactor.core.publisher.Flux;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;

public static <T> Flux<T> toFlux(Observable<T> observable) {
    return Flux.create(emitter -> {
        final Subscription subscription = observable.subscribe(new Subscriber<T>() {
            @Override
            public void onNext(T value) {
                emitter.next(value);
            }
            @Override
            public void onCompleted() {
                emitter.complete();
            }
            @Override
            public void onError(Throwable throwable) {
                emitter.error(throwable);
            }
        });
        emitter.onDispose(subscription::unsubscribe);
    });
}

I have verified that onNext and onCompleted are both getting called in the correct order but my Flux is always empty. Does anyone see what I am doing wrong?

On a related note, why isn't there an adapter for RxJava 1 in reactor-addons?


回答1:


Use the RxJavaReactiveStreams adapter to turn your Observable into a Publisher, then have Flux.fromPublisher() consume it.

compile 'io.reactivex:rxjava-reactive-streams:1.2.1'

Observable<T> o = ...

Flux.from(RxReactiveStreams.toPublisher(o));

On a related note, why isn't there an adapter for RxJava 1 in reactor-addons?

They don't want to support or encourage using that old technology and I completely agree.



来源:https://stackoverflow.com/questions/45405837/adapting-rxjava-1-1-5-to-reactor-core-3-1-0-m3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!