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
1 回答

ITMISS
TA贡献1871条经验 获得超8个赞
使用concatMap
而不是flatMap
.
默认情况下,flatMap 将从源请求 256 个首选项并一次处理它们。这种“一次性”行为因您checkForMatch()
似乎正在阻塞而减少,但仍然:源本身的请求比您想要的要多。
concatMap
另一方面,将一个一个地从源请求首选项,等到当前UserPreference
处理完毕后再请求下一个。
添加回答
举报
0/150
提交
取消