Quarkus Mutiny:异步请求响应的正确处理方式

本文档旨在解决在使用 Quarkus Mutiny 进行异步编程时,如何正确处理请求响应延迟的问题。我们将探讨如何避免因异步操作未完成而导致数据不完整的情况,并提供使用 Uni.combine() 组合多个异步操作的示例,确保所有依赖的异步请求完成后再返回结果。同时,强调避免使用 await().indefinitely 阻塞线程,充分发挥响应式编程的优势。

在使用 Quarkus Mutiny 进行异步编程时,一个常见的问题是如何确保所有的异步操作都完成后再返回结果。特别是在需要调用多个外部服务,并且这些服务响应时间不确定的情况下,很容易出现返回数据不完整的情况。以下将介绍如何使用 Uni.combine() 来解决这个问题,并避免阻塞线程。

使用 Uni.combine() 组合异步操作

Uni.combine() 提供了一种优雅的方式来组合多个 Uni 对象,并在所有 Uni 对象都发出结果后执行后续操作。这可以确保在返回最终结果之前,所有依赖的异步请求都已经完成。

以下是一个示例,展示了如何使用 Uni.combine() 来组合

多个 Uni 对象:

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("/example")
public class ExampleResource {

    @GET
    @Path("/testingAsync")
    public Uni> testingMutiny() {
        List> 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) list);
    }
}

在这个例子中,我们首先创建了一个 Uni 的列表,每个 Uni 对象都模拟了一个延迟 10 秒的异步操作。然后,我们使用 Uni.combine().all().unis(unis) 将这些 Uni 对象组合在一起。combinedWith 方法接收一个函数,该函数在所有 Uni 对象都发出结果后被调用。

处理更复杂的场景:多个外部服务调用

在更复杂的场景中,例如需要调用多个外部服务来获取数据,然后将这些数据组合成一个对象,Uni.combine() 同样适用。

以下是一个示例,展示了如何使用 Uni.combine() 来调用多个外部服务,并将结果组合成一个 Car 对象:

import io.smallrye.mutiny.Uni;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.util.List;

@Path("/example")
public class ExampleResource {

    @GET
    @Path("/testingAsync")
    public Uni testingMutiny() {
        Uni> carDoorsUni = getDoors(variable1, variable2, variable3);
        Uni> carWheelsUni = getWheels(variable1,variable2, variable3);
        Uni> carWindowsUni = getWindows(variable1,variable2, variable3);

        return Uni.combine()
                .all()
                .unis(carDoorsUni, carWheelsUni, carWindowsUni)
                .combinedWith(list -> {
                    // Result of carDoorsUni
                    List carDoors = (List) list.get(0);

                    // Result of carWheelsUni
                    List carWheels = (List) list.get(1);

                    // Result of carWindowsUni
                    List carWindows = (List) list.get(2);

                    // Create a car instance with the previous results
                    Car car = createCar(...);

                    // You can also return a list of cars, but you need to change the return type of testingMutiny to Uni>
                    return car;
                })
                .invoke( () -> System.out.println("Okay it worked"));
    }

    private Uni> getDoors(Object variable1, Object variable2, Object variable3) {
        // Implement the logic to retrieve car doors
        return Uni.createFrom().item(List.of(new JsonObjectCar())); // Replace with actual implementation
    }

    private Uni> getWheels(Object variable1, Object variable2, Object variable3) {
        // Implement the logic to retrieve car wheels
        return Uni.createFrom().item(List.of(new JsonObjectCar())); // Replace with actual implementation
    }

    private Uni> getWindows(Object variable1, Object variable2, Object variable3) {
        // Implement the logic to retrieve car windows
        return Uni.createFrom().item(List.of(new JsonObjectCar())); // Replace with actual implementation
    }

    private Car createCar(Object... args) {
        // Implement the logic to create a Car object
        return new Car(); // Replace with actual implementation
    }
}

class Car {
    // Car class definition
}

class JsonObjectCar {
    // JsonObjectCar class definition
}

在这个例子中,我们定义了三个方法 getDoors(), getWheels(), 和 getWindows(),它们分别负责调用不同的外部服务来获取汽车的门、轮子和窗户信息。然后,我们使用 Uni.combine() 将这三个 Uni 对象组合在一起,并在所有服务都返回结果后,创建一个 Car 对象。

避免使用 await().indefinitely

在使用响应式编程时,应该尽量避免使用 await().indefinitely 阻塞线程。Quarkus 能够识别异步 API,并自动处理结果,因此通常不需要手动等待异步操作完成。

总结

通过使用 Uni.combine(),我们可以轻松地组合多个异步操作,并确保在返回最终结果之前,所有依赖的异步请求都已经完成。这可以避免因异步操作未完成而导致数据不完整的情况。同时,应该尽量避免使用 await().indefinitely 阻塞线程,充分发挥响应式编程的优势。记住,Quarkus 能够识别异步 API,并自动处理结果。