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



我需要在服务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;
});
}

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

最新更新