如何使用 Quarkus/Mutiny 从我的 Reactive REST 调用阻塞服务

How to call a blocking service from my Reactive REST with Quarkus/Mutiny

我需要在服务 A 完成后调用服务 B,并且需要服务 A 的 return 值用于服务 B。 我该怎么做呢?你们能帮帮我吗?

我下面的代码无法正常工作,服务 B/productService.checkout 没有执行。

public Uni<List<Shop>> checkout(Person person) {
     Uni<List<Shop>> shopsUni = shopService.find("person_id", person.getId()).list();
        return shopsUni.map(shops -> {
            for (Shop shop : shops) {
                productService.checkout(shop);
            }
            return shops;
      });
}

您将需要使用 chain 运算符,因为您希望将对服务 A 的调用与对服务 B 的调用 链接 。因此,您将需要类似于:

public Uni<List<Shop>> checkout(Person person) {
   // Represent the call to the Service A
   Uni<List<Shop>> shopsUni = shopService.find("person_id", person.getId()).list();

   return shopsUni.chain(shops -> {
       // Here is a small subtility. 
       // You want to call the Service B multiple times (once per shop), 
       // so we will use "join" which will call the Service B multiple times
       // and then we _join_ the results.
       List<Uni<Void>> callsToServiceB = new ArrayList<>();
       for (Shop shop: shops) {
           // I'm assuming productService.checkout returns a Uni<Void>
           callsToServiceB.add(productService.checkout(shop)); 
        }
        // Now join the result. The service B will be called concurrently
        return Uni.join(callsToServiceB).andFailFast()
            // fail fast stops after the first failure. 
            // replace the result with the list of shops as in the question
            .replaceWith(shops);    
    });    

}