Spring Reactive编程中如何处理两个助焊剂? [英] How to deal with two Fluxes in Spring Reactive Programing?
问题描述
我有两个助焊剂
Flux<Foo> foo;
Flux<Bar> bar;
class Foo
String id
String prop1
String barId
class Bar
String barId
String color
boolean isInFoo
foo和bar都具有barId属性. foo的大小始终等于或小于bar的大小,但通常小于bar的大小(Flux中Bar的数量.例如Foo可以代表一篮子Bar项目中的一些选定项目,尽管两者是不同的对象.
foo and bar both have barId property. The size of foo is always equal to or less than size of bar but usually it is much less than bar size (number of Bars in Flux. For example Foo could represent a few selected items out of a basket of Bar items although the two are different objects.
Bar具有一个布尔值标志isInFoo,默认值为= false,如果Foo具有该barId,则将其设置为true.
Bar has a boolean flag isInFoo with default value = false, which is set to true if Foo has that barId.
如何遍历bar并查找每个bar在foo中是否具有barId以及是否将isInFoo设置为true.
How to iterate through bar and find if each bar has a barId in foo and if so set isInFoo to true.
我也有空
Mono<Foo> findByBarId(String barId) {}
除了上述Flux<Foo> findAll() {}
请注意,findByBarId和findAll是昂贵的数据库操作.
Note that findByBarId and findAll are expensive database operations.
推荐答案
假设您有以下两个变化:
Suppose you have following two flux:
Flux<Foo> foos;
Flux<Bar> bars;
您可以如下修改条形对象的通量:
You can modify the Flux of Bar objects as follows:
bars.flatMap(bar -> findByBarId(bar.getBarId())
.flatMap(foo -> {
bar.setIsInFoo(true);
return Mono.just(bar);
}).switchIfEmpty(Mono.just(bar)));
这篇关于Spring Reactive编程中如何处理两个助焊剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!