3

spring中遵循类似的问题。我希望能够在路由器中获得响应。并进行响应刷新,这样我就可以在不扩展 RTT 的情况下继续在服务器上工作

意思是,在春天做一些类似答案的事情:

    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()
    }
}
4

1 回答 1

1

您可以使用 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();
    }
}
于 2021-12-02T09:40:29.620 回答