简体   繁体   English

Spring Webflux - 如何在不使用 block() 操作的情况下从 Flux 中获取价值

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

I wonder how to write non-blocking code with Webflux.我想知道如何使用 Webflux 编写非阻塞代码。

Here is what I want to do:这是我想做的事情:

  1. Get all Products by ProductProperties field (returned as Flux)ProductProperties字段获取所有产品(以 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.Flux<Product>.availabilityCalendar获取值列表 使用在步骤 2 中检索到的数据并获取一些其他数据(以 Flux<> 形式返回) - 一切都应该是非阻塞操作。

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?如何从Flux<Object>获取值,然后获取作为 Flux<> 返回的其他一些数据,避免像 Flux.block() 这样的阻塞操作来检索下一步获取最终数据返回所需的数据?

    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 productRepository.findByProductPropertiesId返回 Flux

  2. Product has field: ArrayList<ProductAvailability> availabilityCalendar产品具有字段: ArrayList<ProductAvailability> availabilityCalendar

Is it a good approach?这是一个好方法吗?

Thank you!谢谢!

by using the onNext parameter通过使用 onNext 参数

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Webflux - 如何从 Mono/Flux 中多次检索值,而无需多次调用以获取这些 Mono/Flux - Spring Webflux - how to retrieve value from Mono/Flux multiple times without making multiple calls to get those Mono/Flux 如何从Spring Webflux将专有流转换为Flux - How to turn a proprietary stream into a Flux from spring webflux Spring WebFlux。 从数据库(直接和通量运算符)获取数据有两种方法吗? 有什么区别? - Spring WebFlux. There are two ways to get data from database (direct and flux-operators)? What difference? 如何使用 mono 中的 DTO 和 spring 反应性 webflux 中的焊剂制作新的 mono - How to make new mono with DTO from mono and flux in spring reactive webflux Spring webflux / Project reactor - 如何将数据从 flux 写入文本文件 - Spring webflux / Project reactor - how to write data from flux to text file 如何在不阻塞的情况下从 Flux 获取列表? - How to get a List from Flux without blocking? Spring WebFlux - 如何从数据库中获取值并设置此值以创建 object - Spring WebFlux - how to get value from database and set this value to create object 如何在不阻塞的情况下从 Spring Webflux 中的 Mono 对象中提取数据? - How to extract data from a Mono object in Spring Webflux without blocking? 如何从Mono获取用户名 <user> 在春季启动webflux? - How to get username from mono<user> on spring boot webflux? 如何从 Spring WebFlux 反应式中的 ServerRequest 对象获取请求正文? - How to get request body from ServerRequest object in Spring WebFlux reactive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM