[英]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:这是我想做的事情:
ProductProperties
field (returned as Flux)按ProductProperties
字段获取所有产品(以 Flux 形式返回)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:在哪里:
productRepository.findByProductPropertiesId
returns Flux productRepository.findByProductPropertiesId
返回 Flux
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.