【发布时间】:2020-02-27 14:21:32
【问题描述】:
我有一个场景,我将使用从DB 获取实体列表
repository.getAllByIds(ids)
这将返回Flux<Entity>
如果 Flux 为空,则我需要致电 handleAllEntitiesNotFound() 否则我需要致电 handleNotFoundEntities()
repository.getAllByIds(ids)
.buffer()
.switchIfEmpty(__ -> handleAllEntitiesNotFound(ids, erroneousEntities))
.flatMap(list -> handleNotFoundEntities(list))
private Flux<Entity> handleAllEntitiesNotFound(List<String> ids, List<ResponseError> erroneousEntities) {
Flux.fromIterable(ids).subscribe(id -> erroneousEntities.add(new ResponseError("Not Found", "Not Found", id)));
return Flux.empty();
}
我正在使用buffer() 将列表收集到Flux<List<Entity>>
问题是,当我调用服务时,它会停止,没有响应,没有日志,没有任何内容,如果我删除了.switchIfEmpty(__ -> handleAllEntitiesNotFound(ids, erroneousEntities)) 行,它可以工作并返回响应,但不处理handleAllEntitiesNotFound
将buffer() 与switchIfEmpty() 一起使用可能会出现什么问题
【问题讨论】:
标签: java spring rest project-reactor flux