gpt4 book ai didi

java - 仅当第一个发出的事件不满足条件时才从 Flux 中发布下一个元素

转载 作者:行者123 更新时间:2023-12-02 10:09:50 25 4
gpt4 key购买 nike

UserPreferenceFlux 按优先级顺序保存用户偏好的信息,我们必须考虑第二偏好,仅与第一偏好不匹配。偏好匹配需要阻塞 I/O 调用。我尝试使用下面的代码,即使与用户第一首选项匹配,我也可以看到为第二首选项进行了 WebClient 调用,这是不必要的(因为第一匹配首选项已经在进行中)。

Flux<UserPreference> userPreferenceFlux = getUserPreferences();
UserPreferenceFlux
.flatMap(preference -> checkForMatch()) // Blocking IO call for match check
.filter(preference -> preference.isMatchFound())
.next(); // The Idea is to consider next preference if current preference is
// not found

最佳答案

使用concatMap而不是flatMap

默认情况下,flatMap 将从源请求 256 个首选项并一次性处理它们。这种“一次性”行为因您的 checkForMatch() 似乎被阻塞而减弱,但仍然:源本身的请求比您想要的更多。

另一方面,

concatMap 将从源逐一请求首选项,等到当前 UserPreference 处理完毕后再请求下一个。

关于java - 仅当第一个发出的事件不满足条件时才从 Flux 中发布下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com