0

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

我下面的代码不能正常工作,服务 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;
      });
}
4

1 回答 1

1

您将需要使用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);    
    });    

}
于 2022-01-19T08:48:25.477 回答