【发布时间】:2019-11-21 16:33:14
【问题描述】:
背景
我正在使用 Spring Boot 2.2.1、project-reactor 3.3.0 和 spring-data-mongodb 2.2.1,并且我正在尝试从多个查询中加载数据。我的代码大致是这样的:
Flux.just("type1", "type2", "type3", "type4")
.concatMap { type ->
reactiveMongoOperations.find<Map<String, Any>>(BasicQuery("{'type': '$type'}"), "collectionName")
.doOnError { e ->
log.error("Caught exception when reading from mongodb: ${e::class.simpleName} - ${e.message}", e)
}.switchIfEmpty {
log.warn("Failed to find any documents of type $type")
Mono.empty<Map<String, Any>>()
}
}
.. // More operations here
.subscribe()
问题是如果reactiveMongoOperations.find(..) 没有找到给定类型的任何文档(因此"Failed to find any documents of type $type" 被记录),整个操作将无限期地挂起。如果我删除 switchIfEmpty 子句,操作完成,一切正常。
问题
- 如果我添加
switchIfEmpty操作,为什么整个操作会挂起?如果我使用flatMap而不是concatMap没关系,它最终还是会挂起。 - 我应该如何记录没有找到特定查询的文档? IE。我想记录当
reactiveMongoOperations.find(..)返回一个空的Flux时没有找到任何文档。
【问题讨论】:
-
您的代码在所有括号中看起来很奇怪,请使用正确的可运行代码更新您的问题
-
感谢您的建议,但我认为它是正确的,不过它是 Kotlin。我认为 Kotlin 无处不在,但我可能应该改用纯 Java 以使 Java 开发人员更容易。
标签: spring-boot reactive-programming spring-data-mongodb project-reactor