我从响应中获得Mono<User>
,用户有一个订阅列表,我必须(通过另一个api)检查其中至少有一个符合条件,然后根据这个条件,我必须发送进一步的事件。
Mono<User> user = userFacade.getUser();
user.flatMap(this::hasSub)
.flatMap(this::sendEvent(Pair.getLeft, Pair.getRight))
Mono<Pair<User,Boolean>> hasSub(User user) {
Stream<Mono<Boolean>> listOfBooleans= user.getSubscriptions.stream
.map(sub -> SubFacade.isPrioSub(sub.getId))//SubFacade returns Mono<Boolean>
//At this point I am stuck, I need to check if at least one of Stream<Mono<Boolean>> is true, but anyMatch() is arguing about passing Mono<Boolean> into it
//also to optimize requests it would be nice to have analogue of break to exit iteration once we get Mono<true>, but dropWith() also is not accepting Mono<Boolean>
return Mono.just(Pair.of(user, 'get boolean from listOfBooleans'))
}
知道怎么解决这个问题吗?
发布于 2021-05-08 12:06:30
Mono<Pair<User, Boolean>> hasSub(User user) {
return Flux.fromIterable(user.getSubscriptions())
.filterWhen(sub -> SubFacade.isPrioSub(sub.getId()))
.next() // takes the first element passing the filter
.map(x -> Pair.of(user, true))
.defaultIfEmpty(Pair.of(user, false));
}
注意:这将使依次检查给定用户的订阅,并在第一个满意项处停止。如果需要并发性,则应该将filterWhen
替换为flatMap
和filter
。
https://stackoverflow.com/questions/67441879
复制相似问题