将多个流量收集到一个

jobtbby3  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(536)

我想在我的spring引导服务中收集多个通量结果。我的方法:

  1. private Flux<VMachineResourceResponse> getDataForPhysicalMachineProtection(
  2. ResourcesWrapper resources, UUID groupId) {
  3. Flux<VMachineResourceResponse> result = Flux.empty();
  4. resources
  5. .getHypervResources()
  6. .forEach(
  7. resource -> {
  8. Flux<VMachineResourceResponse> protectedResourcesForAgentAndId =
  9. hypervAgentService.getProtectedResourcesForAgentAndId(
  10. groupId, resource.getAgentId());
  11. result.mergeWith(protectedResourcesForAgentAndId); //maybe that way???
  12. });
  13. return result;
  14. }

怎么做?

cs7cruho

cs7cruho1#

你应该把你的单子放在一张纸上 Flux ,那么 flatMap 再把每一个新的 Flux . 这个 flatMap 会自动将所有的东西“扁平化”为一个 Flux 下面的例子应该说明这个概念:

  1. public Flux<String> getData() {
  2. final List<String> strings = new ArrayList<>();
  3. strings.add("Foo");
  4. strings.add("Bar");
  5. return Flux.fromIterable(strings)
  6. .flatMap(this::get);
  7. }
  8. private Flux<String> get(String s) {
  9. return Flux.just(s + "Bar", s + "Foo");
  10. }

相关问题