Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable to Flux Conversion

I have my code in Rxjava and I want to change it reactor project. My rxjava code is

public Observable<Map<Integer, Data>> getObject(List<Integer> ids, List<String> filters) {
        if (ids == null || ids.isEmpty()) {
            return Observable.just(new HashMap<>());
        }
        return Observable.fromCallable(() -> {
            Map<Integer, Data> data=  client.getObject(ids, filters).getData();
            return data;
        });
    }

How do I write this code using Flux / Mono? What should I use(Flux / Mono)?

Basically I want to know how to store Map to Flux like Observable or should I use Mono

like image 265
Ankit Bansal Avatar asked Jun 16 '18 11:06

Ankit Bansal


1 Answers

Observable to Flux Conversion can be achieved using

Flux<User> convFlux = Flux.from(observable.toFlowable(BackpressureStrategy.BUFFER));

Different BackpressureStrategy that can be applied are as below:

package io.reactivex;

public enum BackpressureStrategy {
   MISSING,
   ERROR,
   BUFFER,
   DROP,
   LATEST;

   private BackpressureStrategy() {
   }
}

Conversion between RxJava 2 types and Mono/Flux back and forth and compose backpressure, cancellation and fusion where applicable please refer https://projectreactor.io/docs/adapter/release/api/reactor/adapter/rxjava/RxJava2Adapter.html

You may refer to https://github.com/reactor/lite-rx-api-hands-on/blob/complete/src/main/java/io/pivotal/literx/Part09Adapt.java which has all the conversion implemented as an exercise, thanks to Simon Baslé.

like image 63
Santosh Sindham Avatar answered Oct 20 '22 08:10

Santosh Sindham