REST端点:无返回值的异步执行 [英] REST-Endpoint: Async execution without return value

查看:105
本文介绍了REST端点:无返回值的异步执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem might be very easy to solve, but I don't get it at the moment. In my Quarkus-App I have a REST-Endpoint which should call a method, don't wait for the result and immediately return a 202-HTTP-Statuscode.

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

我已经阅读了有关Vert.x和异步处理的Quarkus文档.但关键是:处理是异步完成的,但客户端会等待结果.我的客户不需要等待,因为没有返回值.就像调用批处理一样.

I've read the Quarkus-Documentation about Vert.x and asynchronous processing. But the point there is: the processing is done asynchronously, but the client waits for the result. My clients don't need to wait, because there is no return value. It's something like the invocation of a batch-processing.

所以我需要类似new Thread的东西,但要包含所有的Quarkus上下文.

So I need something like a new Thread, but with all the Quarkus-Context.

推荐答案

我们找到了一个解决方案:

We've found a solution:

@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();
}

这篇关于REST端点:无返回值的异步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆