Marcos J.C Kichel
Marcos J.C Kichel

Reputation: 7219

Java + Reactor: Http call for each record

I have a Stream<A> that I want to iterate over and fetch additional information about each record from another service. In the end it should become a Stream<B>.

Since I am using Jetty I can't use .block().

How do I do that using Reactor Project?

Upvotes: 2

Views: 788

Answers (1)

Eugene Utkin
Eugene Utkin

Reputation: 61

You can use flatMap:

Flux<A> fluxA = //...;
Flux<B> fluxB = sampleFlux.flatMap(elem -> fetchDataFromRemoteService(elem));

where fetchDataFromRemoteService(A a) return Publisher.

Or you can use Flux#concatMap:

Flux<A> fluxA = //...;
Flux<B> fluxB = sampleFlux.concatMap(elem -> fetchDataFromRemoteService(elem));

The difference between flatMap and concatMap will be that in the second case, all actions will follow each other.

Or:

Stream<B> streamB =  Flux.fromStream(streamA)
                         .flatMap(this::fetchDataFromRemoteService)
                         .toStream();

Upvotes: 1

Related Questions