如何执行并发 void 方法调用

问题描述 投票:0回答:1

我正在使用一个数据库,并试图弄清楚如何同时执行具有 void 返回类型的写入。我已经按照 docs 包装了阻塞调用。

我想要:

  1. 从数据库中获取返回 Mono 的项目
  2. 同时调用 2 个返回 Mono 的方法,我需要在返回之前完成
  3. 将 get() 的响应返回给订阅者

从功能上来说,我可以使用它:

    return repository
            .get(param)
            .flatMap(response -> Mono.zip(
                    method1(response),
                    method2(response),
                    (x, y) -> response
            ))

但是:

  • 我觉得我正在滥用 Mono.zip 来订阅响应,因为我忽略了 x 和 y

  • 我不得不更改method1/2接口,以在写入成功后任意返回一个值

这样做的正确方法是什么?

spring-webflux project-reactor
1个回答
0
投票

考虑到您描述的任务,我没有发现您的代码有任何问题。

但我认为可以通过

.thenReturn()
后备来简化:

 return repository.get(param)
                  .flatMap(response -> Mono.zip(method1(response), method2(response))
                        .thenReturn(response)
                  )

response
将会在
Mono.zip
完成后返还

© www.soinside.com 2019 - 2024. All rights reserved.