Filtering Flux elements based on result of webservice call

Viewed 40

In below code I am filtering elements ina flux based on some logic and then further filtering based on the result of webservice call. Now the webservice returns a Mono so I can use it in the filter method. calling block() doesn't work. Can someone how to go about this. Thanks in advance. The code is not fully compilable as I removed company specific code.

protected Flux<> getFilteredResults(Flux<> resultsToFilter, boolean someBoolean) {
        return resultsToFilter.filter(cr -> {
            //Filtering logic
        })
                .filter( a-> {
                    Mono<Boolean> serviceCallResult = makeServiceCall(a);
                    return serviceCallResult.block(); //HOW TO DO THIS CORRECTLY
                });

    }
1 Answers

You should avoid block while building reactive flows. filter could be used to apply synchronous logic. In case you need to apply asynchronous function that returns Mono - use filterWhen.

protected Flux<> getFilteredResults(Flux<> resultsToFilter, boolean someBoolean) {
    return resultsToFilter
            .filterWhen(a ->  makeServiceCall(a));

}
Related