gpt4 book ai didi

java - 响应后立即请求?

转载 作者:行者123 更新时间:2023-11-30 11:12:45 27 4
gpt4 key购买 nike

我对以下场景有点迷茫:

我的网络服务使用带有表单数据的 POST 请求。我必须回复 200 OK 否则发件人认为请求失败。

在回答 200 后,我想立即使用我刚刚收到的一些数据调用远程主机上的另一个网络服务。

我的网络服务使用带有 @GET 注释的 POST 请求。那行得通,我可以读取所有表单数据。为了调用其他网络服务,我使用了 Jersey Client API。这也很好用。

我只是不知道如何从一个调用切换到另一个调用。一切都用 Jersey 2 编程并部署在 Tomcat 中,因此没有真正的应用程序服务器。没有可用的完整 Java EE 堆栈。

我是否缺少一些中间件?我是否需要实现自定义事件循环或某些消息代理?

最佳答案

不确定是否有任何“标准” 方法来处理这个问题,但是有一个 CompletionCallback我们可以注册 AyncResponse .

CompletionCallback:

A request processing callback that receives request processing completion events.

A completion callback is invoked when the whole request processing is over, i.e. once a response for the request has been processed and sent back to the client or in when an unmapped exception or error is being propagated to the container.

AsyncResponse 旨在异步处理请求,但我们可以立即调用 resume 将其视为同步处理。一个简单的例子就像

@Path("/callback")
public class AsyncCallback {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void postWithAsync(@Suspended AsyncResponse asyncResponse,
SomeObject object) {

asyncResponse.register(new CompletionCallback() {
@Override
public void onComplete(Throwable error) {
if (error == null) {
System.out.println("Processing new Request");
} else {
System.out.println("Exception in Request handling");
}
}
});
Response response = Response.ok("Success").build();
// Carry on like nothing happened
asyncResponse.resume(response);
}
}

您可以在 Asynchronous Server-side Callbacks 查看更多解释

关于java - 响应后立即请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738564/

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