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

PublishNext element from the flux only if the first emitted event doesn't met criteria

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 个首选项并一次处理它们。这种 "all-at-once" 行为因您的 checkForMatch() 似乎被阻塞这一事实而减弱,但仍然:请求源本身比您想要的更多。

另一方面,

concatMap会一个一个地向源请求偏好,等到当前UserPreference被处理后再请求下一个。