简体   繁体   English

Flux 的 onComplete 执行后如何返回 Mono?

[英]How do I return a Mono after Flux's onComplete is executed?

I am trying to save a set of categories.我正在尝试保存一组类别。 When all categories are saved, set the categories of product to it.保存所有类别后,将产品类别设置为它。 Then return Product.然后返回产品。 So far I've managed to do this.到目前为止,我已经成功地做到了这一点。

public Mono<Product> save(Product product) {
    final Set<Category> categories = product.getCategories();
    Set<Category> _categorySet = new HashSet<>();
    Mono<Product> _product;
    for (Category category : categories) {
        final Mono<Category> save = categoryService.save(category);
        save.subscribe(_categorySet::add,null,()->{
            product.setCategories(_categorySet);
            repository.save(product);
        });
    }
}

How do I return product after it has been saved without relying on block() ?保存后如何在不依赖block()的情况下退回产品? I cannot seem to find a source to learn these stuffs.我似乎找不到学习这些东西的来源。 Can someone point me to good materials.谁能指点我的好材料。

don't block webflux chain manually.不要手动阻止 webflux 链。 try to use this code.尝试使用此代码。

public Mono<Product> save(Product product) {

    final Set<Category> categories = product.getCategories();
    return Flux.fromIterable(categories)
            .flatMap(categoryService::save)
            .collect(Collectors.toSet())
            .flatMap(categorySet -> { // use `flatMap` if repository.save(product); returns `Mono<Product>`. or else use `map` if repository.save(product); returns `Product`
                product.setCategories(categorySet);
                return repository.save(product);
            });
}

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

相关问题 如何使用返回 Mono 的生成包装调用创建 Flux - How do I create a Flux using generate wrapping calls that return a Mono 如何返回包含 Reactive Mono 和 Flux 的 Reactive Flux? - How to return a Reactive Flux that contains a Reactive Mono and Flux? 如何从 Flux onComplete 发射 - How to emit from Flux onComplete 如何实现具有可完成未来的具有 Mono/Flux 的 Reactive Framework 中存在的.switchIfEmpty() 类型的方法? - How do I achieve .switchIfEmpty() type of method present in Reactive Framework with Mono/Flux with completable future? 具有Flux和Mono的Reactor的Flux.combineLatest() - Reactor's Flux.combineLatest() with Flux and Mono WebFlux - 如何检查 Mono <responseentity<flux<myitem> &gt;&gt; 为空返回 404 </responseentity<flux<myitem> - WebFlux - how to check if Mono<ResponseEntity<Flux<MyItem>>> is empty to return 404 如何有条件地执行 Reactor 的 Mono/Flux 供应商操作符? - How to conditionally execute Reactor's Mono/Flux supplier operator? 如何使用带有 Flux 或 Mono 的 @KafkaListener 注释方法? - How do you use a @KafkaListener annotated method with a Flux or Mono? 如何使用返回 Mono 的数据库调用对 Flux 中的每个 object 进行连接 - How to do a join for every object in a Flux with a database call returning a Mono 如何迭代Flux并与Mono混合 - How to iterate Flux and mix with Mono
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM