gpt4 book ai didi

vert.x - 如何在 quarkus/Vert.X 中的操作之前发送响应

转载 作者:行者123 更新时间:2023-12-04 17:08:10 24 4
gpt4 key购买 nike

正在关注 similar question Spring 。我希望能够在路由器中获得响应。并进行响应刷新,这样我就可以在不延长 RTT 的情况下继续在服务器上工作

意思是,做一些类似 Spring 的答案:

    public void doSomething(@RequestBody List<Message> messages, HttpServletResponse response) {
int code = (messages!=null && !messages.isEmpty()) ? HttpServletResponse.SC_OK
: HttpServletResponse.SC_NOT_FOUND;
if (code != HttpServletResponse.SC_OK) {
response.sendError(code, res);
return;
}
java.io.PrintWriter wr = response.getWriter();
response.setStatus(code);
wr.print(res);
wr.flush();
wr.close();

// Now it it time to do the long processing
...
}

这是我今天的 quarkus 代码:

@Path("/events")
class EventsRouter {
val logger: Logger = Logger.getLogger(EventsRouter::class.java)

@POST
@Consumes(MediaType.APPLICATION_JSON)
fun handleEvent(
@HeaderParam("User-Agent") userAgent: String?,
eventPayload: EventPayload,
): Response {
val time = LocalDateTime.now()
...
return Response.ok().build()
}
}

最佳答案

您可以使用 Vert.x executeBlocking异步运行阻塞代码。

下面是一个例子(未经测试,所以请将其视为伪):

@Path("/events")
class EventsRouter {
val logger: Logger = Logger.getLogger(EventsRouter::class.java);
private final Vertx vertx;

@Inject
public EventsRouter(Vertx vertx) {
this.vertx = vertx;
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
fun handleEvent(
@HeaderParam("User-Agent") userAgent: String?,
eventPayload: EventPayload,
): Response {
val time = LocalDateTime.now()
vertx.executeBlocking(promise -> {
//Do something...
promise.complete();
}, res -> {
System.out.println("Something was done");
});
return Response.ok().build();
}
}

关于vert.x - 如何在 quarkus/Vert.X 中的操作之前发送响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70066738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com