简体   繁体   中英

Spring Webflux - how to get value from Flux without block() operations

I wonder how to write non-blocking code with Webflux.

Here is what I want to do:

  1. Get all Products by ProductProperties field (returned as Flux)
  2. Get a list of values from Flux<Product>.availabilityCalendar Use the data retrieved in step 2 and fetch some other data (returned as Flux<>) - everything should be a non-blocking operations.

How to do that? How to get values from Flux<Object> and then fetch some other data returned as Flux<> avoiding blocking operations like Flux.block() to retrieve data that are needed in the next step to fetch final data to return?

    public Flux<Product> getAllProductsByAvailability(Flux<ProductProperties> productProperties,
                Map<String, String> searchParams) {

    productProperties
                    .flatMap(property -> productRepository.findByProductPropertiesId(property.getId())) //1. return Products
                    .flatMap(product -> Flux.just(product.getAvailabilityCalendar())) //2. how to get Product.availabilityCalendar list as non-blocking operation to work with this data afterwards?
(...)

where:

  1. productRepository.findByProductPropertiesId returns Flux

  2. Product has field: ArrayList<ProductAvailability> availabilityCalendar

Is it a good approach?

Thank you!

by using the onNext parameter

productRepository.findByProductPropertiesId(property.getId())
.onNext(product -> {
    return // Do things here
})

like this I check the tag valid

 Flux.fromIterable(vo.getTags())
        .flatMap((tag) -> tagService.findByCode(tag.getCode()).map(TagBo::createByVo)).filter(Objects::nonNull).collectList().doOnNext(l->vo.setTags(l));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM