背景
我正在使用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
子句,则操作完成,一切正常。
问题