如何遍历Mono <list <string >>调用Mono返回方法

时间:2019-02-22 22:57:31

标签: java reactive-programming spring-webflux project-reactor

我正在尝试通过以下方法实现以下目的

  
      
  1. 从Dealer X获取所有汽车
  2.   
  3. 创建包装对象,该对象存储一组所有汽车和另一组所有制造商2a。用在以下位置获得的汽车填充汽车   步骤1
  4.   
  5. 每辆车都获得各自独立的制造商
  6.   
  7. 将所有获得的制成品存储到包装对象制成品Set
  8. 中   
  9. 返回汽车和制造商的单声道
  10.   
Mono<CarAndManufactures> requestCarAndManufactures(Request req) {
final String dealerId = buildDealerId(req.getDealerRegion(), req.getDealerId());
final CarAndManufactures CarAndManufactures = new CarAndManufactures();
return webSocketClient.getCars(dealerId) //note #getCars returns a Mono
    .map(getCarsResponse -> getCarsResponse
      .getResult()
      .stream()
      .map(Car::getId)
      .collect(toSet()))
    .map(carIds -> {
      CarAndManufactures.setCars(carIds);
      return CarAndManufactures;
    })
    .flatMapMany(CarAndManufactures1 -> Flux.fromIterable(CarAndManufactures.getCars().keySet()))
    .collectList()
    .log("Existing cars")
    .flatMap(carIds -> { //This is the problem area
      carIds
          .stream()
          .map(carId -> {
            webSocketClient.getManufactures(carId) //Note getManufactures returns a Mono... This method does look like its ever called
                .map(getManufactureResponse -> getManufactureResponse
                    .getResult()
                    .stream()
                    .map(Manufacture::getId)
                    .collect(toSet()))
                .map(ManufactureIds -> {
                  CarAndManufactures.SetManufactures(ManufactureIds); //since the line commented on above is not called the Manufacture Set is empty
                  return CarAndManufactures;
                });
            return CarAndManufactures;
          });
          return just(CarAndManufactures);
        }
    )
    .log("Car And Manufactures"); 

}

制造品集始终为空,看起来好像从未调用过webSocketClient.getManufactures(carId)。以为我可能在某个地方缺少.subscribe,但是由于webflux控制器正在使用它,因此我认为在任何地方都不需要#subscribes

1 个答案:

答案 0 :(得分:0)

仍然不明白为什么下面的方法有效,但是OP中的代码无效。在最后一个flatMap内添加了Flux.fromIterable,而不是以前,现在使用addAll添加制造集,因为它们的集合现在一次进入一个集合,而不是以前都可用。

Mono<CarAndManufactures> requestCarAndManufactures(Request req) {
final String dealerId = EntityUtil.buildDealerId(req.getDealerRegion(), req.getDealerId());

return webSocketClient.getCars(req.getApiKey(), dealerId)
    .map(getCarsResponse -> getCarsResponse
        .getResult()
        .stream()
        .collect(toMap(AppCar::getId,
            appCar -> new MutablePropertyManager(appCar.getProps().getSize()))))
    .map(stringImmutablePropertiesMap -> {
      CarAndManufactures CarAndManufactures = new CarAndManufactures();
      CarAndManufactures.setCars(stringImmutablePropertiesMap);
      return CarAndManufactures;
    })
    .flatMap(CarAndManufactures ->
        Flux.fromIterable(CarAndManufactures.getCars().keySet())
            .flatMap(s -> webSocketClient.getManufacturesByCar(req.getApiKey(), s)
                .map(getManufacturesResponse -> getManufacturesResponse
                    .getResult()
                    .stream()
                    .map(AppManufacture::getId)
                    .collect(toSet()))
                .map(ManufactureIds -> {
                  CarAndManufactures.addManufactures(ManufactureIds);
                  return CarAndManufactures;
                }))
            .last())
    .log("Car And Manufactures");

}