java - 如何调用所有`Mono` 同时
问题描述
我想同时调用所有Mono
并获取所有值。但是下面的代码不足以满足这两个要求。如何实施?
@Test
fun test1() {
val m1 = Mono.delay(Duration.ofSeconds(3)).thenReturn("v1").log()
val m2 = Mono.delay(Duration.ofSeconds(3)).thenReturn("v2").log()
// waits in 3 seconds but cannot get returned values
val result = Mono.`when`(m1, m2).block()
assertNull(result)
}
@Test
fun test2() {
val m1 = Mono.delay(Duration.ofSeconds(3)).thenReturn("v1").log()
val m2 = Mono.delay(Duration.ofSeconds(3)).thenReturn("v2").log()
// can get returned value but requires 6 seconds to process
val result = Flux.concat(m1, m2).collectList().block()
assertEquals(listOf("v1", "v2"), result)
}
解决方案
您可以使用以下zip
功能:
val result = Mono.zip(m1, m2).block()
推荐阅读
- python - 导入 numpy 模块时出现 Python 错误
- javascript - 使用@testing-library/user-event 进行虚假点击
- python - 如何将数据集拆分为特定的数字?
- kubernetes - kubernetes - 将入口流量路由到某些路径的特定 pod
- java - 为 java 构建带有额外模块的 opencv 4.5.0
- docker - Docker - 在 dockerfile 中使用 curl
- sql - 在oracle sql中获取每个月的累计总和
- javascript - 如何从其他数组生成基于数组索引的交集(数组或对象类型)?
- linux - 交叉编译引用其他符号链接的库的 c 应用程序库
- python - 将熊猫数据框保存在动态创建的文件夹中