REST-Endpoint:异步执行,无返回值

Sim*_*ick 5 asynchronous jax-rs quarkus

我的问题可能很容易解决,但目前我不明白。在我的 Quarkus-App 中,我有一个 REST-Endpoint,它应该调用一个方法,不等待结果并立即返回 202-HTTP-Statuscode。

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response calculateAsync(String input) {
    process();
    return Response.accepted().build();
}
Run Code Online (Sandbox Code Playgroud)

我已阅读有关 Vert.x 和异步处理的 Quarkus 文档。但要点是:处理是异步完成的,但客户端等待结果。我的客户不需要等待,因为没有返回值。这类似于批处理的调用。

所以我需要类似 a 的东西new Thread,但具有所有 Quarkus-Context。

Sim*_*ick 5

我们找到了解决方案:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response calculateAsync(String input) {
    Uni.createFrom().item(input).emitOn(Infrastructure.getDefaultWorkerPool()).subscribe().with(
            item -> process(input), Throwable::printStackTrace
    );

    return Response.accepted().build();
}
Run Code Online (Sandbox Code Playgroud)