反应堆中的嵌套 flatMap。

huangapple 未分类评论46阅读模式
英文:

Reactor flatmap inside a flatmap

问题

在传统的非响应式编程中,我的代码将如下所示:

public OrderEstimationsResult getEstimations(ObjectId productId, int quantity, float longitude, float latitude, ShipmentMethod shipmentMethod) {
    Product product = productService.getById(productId);
    ShippingEstimationResult estimation = shippingService.getEstimations(product.getWeight(), product.getLocation(), new Location(longitude, latitude), new Date(), ShipmentMethod.FAST);
    float price = productService.calculatePrice(product, quantity);
    return new OrderEstimationsResult(price, estimation.getEstimatedCost(), new Date());
}

在响应式编程中,我使用了 flatMap 内部的 flatMap 来实现这一点。

public Mono<OrderEstimationsResult> getEstimations(ObjectId productId, int quantity, float longitude, float latitude, ShipmentMethod shipmentMethod) {
    return productService
            .getById(productId)
            .flatMap(product -> shippingService
                    .getEstimations(product.getWeight(), product.getLocation(), new Location(longitude, latitude), new Date(), ShipmentMethod.FAST)
                    .flatMap(estimation -> {
                        float price = productService.calculatePrice(product, quantity);
                        return Mono.just(new OrderEstimationsResult(price, estimation.getEstimatedCost(), new Date()));
                    })
            );
}

请问是否有人可以告诉我我是否在使用正确/最佳的方法?

英文:

I have faced a problem while transforming two streams to single-stream while the second one is depended on the first stream.

In traditional non-reactive programming my code would be written as following.

public OrderEstimationsResult getEstimations(ObjectId productId, int quantity, float longitude, float latitude, ShipmentMethod shipmentMethod) {
    Product product = productService.getById(productId);
    ShippingEstimationResult estimation = shippingService.getEstimations(product.getWeight(), product.getLocation(), new Location(longitude, latitude), new Date(), ShipmentMethod.FAST);
    float price = productService.calculatePrice(product, quantity);
    return new OrderEstimationsResult(price, estimation.getEstimatedCost(), new Date());
}

I managed to do this using flatmap inside a flatmap in reactive programming.

public Mono&lt;OrderEstimationsResult&gt; getEstimations(ObjectId productId, int quantity, float longitude, float latitude, ShipmentMethod shipmentMethod) {
    return productService
            .getById(productId)
            .flatMap(product -&gt; shippingService
                    .getEstimations(product.getWeight(), product.getLocation(), new Location(longitude, latitude), new Date(), ShipmentMethod.FAST)
                    .flatMap(estimation -&gt; {
                        float price = productService.calculatePrice(product, quantity);
                        return Mono.just(new OrderEstimationsResult(price, estimation.getEstimatedCost(), new Date()));
                    })
            );
}

Could anyone please tell whether I'm using the correct/best approach?

huangapple
  • 本文由 发表于 2020年4月6日 10:42:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61052214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定