boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

使用 Quarkus Mutiny 构建响应式应用:等待请求响应完成


avatar
作者 2025年9月11日 8

使用 Quarkus Mutiny 构建响应式应用:等待请求响应完成

使用 quarkus Mutiny 构建响应式应用:等待请求响应完成

本文档旨在解决在使用 Quarkus Mutiny 构建响应式应用时,如何正确处理异步请求并等待其完成的问题。通过示例代码,我们将演示如何使用 Uni.combine() 来并发执行多个请求,并在所有请求完成后将结果合并,避免因异步延迟导致数据不完整的情况。同时,强调避免在响应式编程中使用阻塞操作 await(),充分利用 Quarkus 提供的异步特性。

在使用 Quarkus 构建响应式应用程序时,Mutiny 提供了一种方便的方式来处理异步操作。然而,在某些情况下,我们需要确保所有异步请求都已完成,然后再返回结果。本教程将介绍如何使用 Uni.combine() 方法来实现这一点,避免因异步延迟导致数据不完整。

理解问题

在传统的同步编程中,我们可以简单地按顺序执行请求,并等待每个请求完成后再进行下一步操作。但在响应式编程中,我们通常希望并发地执行多个请求,以提高应用程序的性能。问题在于,如何确保在所有请求都完成后,再将结果返回给客户端?

解决方案:使用 Uni.combine()

Mutiny 提供了 Uni.combine() 方法,可以用于并发地执行多个 Uni,并在所有 Uni 都发出结果后,将结果合并。这正是我们需要的解决方案。

以下是一个示例,展示了如何使用 Uni.combine() 来并发地执行多个请求,并将结果合并为一个列表:

import io.smallrye.mutiny.Uni;  import Javax.ws.rs.GET; import javax.ws.rs.Path; import java.time.Duration; import java.util.ArrayList; import java.util.List;  @Path("/testingAsync") public class MyResource {      @GET     public Uni<List<String>> testingMutiny() {         List<Uni<String>> unis = new ArrayList<>();         List.of("hello", "RestEasy").forEach(e -> {             unis.add(Uni.createFrom().item(e)                     .onItem().delayIt().by(Duration.ofMillis(10000)));         });          return Uni.combine().all().unis(unis)                 .combinedWith(list -> (List<String>) list);     } }

在这个例子中,我们首先创建了一个 Uni 列表,每个 Uni 都代表一个异步请求。然后,我们使用 Uni.combine().all().unis(unis) 将这些 Uni 组合在一起。combinedWith() 方法接收一个函数,该函数将在所有 Uni 都发出结果后被调用。在这个函数中,我们将结果转换为一个 List<String> 并返回。

使用 Quarkus Mutiny 构建响应式应用:等待请求响应完成

Tweeze

Tweeze.app是一个ai驱动的个性化新闻简报服务,定位为个人互联网AI阅读助手

使用 Quarkus Mutiny 构建响应式应用:等待请求响应完成37

查看详情 使用 Quarkus Mutiny 构建响应式应用:等待请求响应完成

更复杂的用例:处理多个 endpoint 调用

假设我们需要调用多个 endpoint,并将它们的结果合并到一个 Car 对象中。以下是一个示例:

import io.smallrye.mutiny.Uni; import java.util.List;  @Path("/testingAsync") public class MyResource {      @GET     public Uni<Car> testingMutiny() {         Uni<List<JSonObjectCar>> carDoorsUni = getDoors(variable1, variable2, variable3);         Uni<List<jsonObjectCar>> carWheelsUni = getWheels(variable1, variable2, variable3);         Uni<List<JsonObjectCar>> carwindowsUni = getwindows(variable1, variable2, variable3);          return Uni.combine()                 .all()                 .unis(carDoorsUni, carWheelsUni, carWindowsUni)                 .combinedWith(list -> {                     // Result of carDoorsUni                     List<JsonObjectCar> carDoors = (List<JsonObjectCar>) list.get(0);                      // Result of carWheelsUni                     List<JsonObjectCar> carWheels = (List<JsonObjectCar>) list.get(1);                      // Result of carWindowsUni                     List<JsonObjectCar> carWindows = (List<JsonObjectCar>) list.get(2);                      // Create a car instance with the previous results                     Car car = createCar(carDoors, carWheels, carWindows);                      // You can also return a list of cars, but you need to change the return type of testingMutiny to Uni<List<Car>>                     return car;                 })                 .invoke(() -> System.out.println("Okay it worked"));     }      private Uni<List<JsonObjectCar>> getDoors(String variable1, String variable2, String variable3) {         // Implement the logic to fetch doors         return Uni.createFrom().item(List.of(new JsonObjectCar())); // Replace with actual implementation     }      private Uni<List<JsonObjectCar>> getWheels(String variable1, String variable2, String variable3) {         // Implement the logic to fetch wheels         return Uni.createFrom().item(List.of(new JsonObjectCar())); // Replace with actual implementation     }      private Uni<List<JsonObjectCar>> getWindows(String variable1, String variable2, String variable3) {         // Implement the logic to fetch windows         return Uni.createFrom().item(List.of(new JsonObjectCar())); // Replace with actual implementation     }      private Car createCar(List<JsonObjectCar> carDoors, List<JsonObjectCar> carWheels, List<JsonObjectCar> carWindows) {         // Implement the logic to create a Car object         Car car = new Car();         //Set values to Car object based on doors, wheels, and windows         return car;     } }

在这个例子中,我们首先创建了三个 Uni,分别代表获取车门、车轮和车窗的请求。然后,我们使用 Uni.combine().all().unis(carDoorsUni, carWheelsUni, carWindowsUni) 将这些 Uni 组合在一起。combinedWith() 方法接收一个函数,该函数将在所有 Uni 都发出结果后被调用。在这个函数中,我们从结果中提取车门、车轮和车窗的信息,并创建一个 Car 对象。

重要注意事项

  • 避免使用 await(): 在响应式编程中,应该避免使用 await() 方法,因为它会阻塞当前线程,从而降低应用程序的性能。Quarkus 能够识别异步 API 并相应地解释结果,因此通常不需要使用 await()。
  • 不需要 subscribe(): 在 Quarkus 中,通常不需要手动调用 subscribe() 方法来订阅 Uni 或 Multi。Quarkus 会自动处理订阅和结果的处理。
  • 错误处理: 在实际应用中,需要添加适当的错误处理机制,以处理请求失败的情况。可以使用 Uni.onFailure() 方法来处理错误。

总结

Uni.combine() 是一个强大的工具,可以用于并发地执行多个异步请求,并在所有请求都完成后将结果合并。通过使用 Uni.combine(),我们可以避免因异步延迟导致数据不完整的问题,并提高应用程序的性能。记住,在响应式编程中,应该避免使用 await() 方法,并充分利用 Quarkus 提供的异步特性。



评论(已关闭)

评论已关闭