仅当第一个发出的事件不符合条件时才从通量中发布下一个元素

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


婷婷同学_
浏览 110回答 1
1回答

ITMISS

使用concatMap而不是flatMap.默认情况下,flatMap 将从源请求 256 个首选项并一次处理它们。这种“一次性”行为因您checkForMatch()似乎正在阻塞而减少,但仍然:源本身的请求比您想要的要多。concatMap另一方面,将一个一个地从源请求首选项,等到当前UserPreference处理完毕后再请求下一个。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java