gpt4 book ai didi

java - jersey ws 2.0 @suspended AsyncResponse,它有什么作用?

转载 作者:搜寻专家 更新时间:2023-10-31 20:33:42 27 4
gpt4 key购买 nike

我正在分析一些 jersey 2.0 代码,我对以下方法的工作原理有疑问:

@Stateless
@Path("/mycoolstuff")
public class MyEjbResource {

@GET
@Asynchronous //does this mean the method executes on child thread ?
public void longRunningOperation(@Suspended AsyncResponse ar) {
final String result = executeLongRunningOperation();
ar.resume(result);
}

private String executeLongRunningOperation() { … }
}

假设我在网络浏览器中输入 www.mysite/mycoolstuff这将执行该方法,但我不理解 asyncResponse 用于 @Asynchronous 注释的内容。从浏览器我怎么会注意到它的异步?删除注释有什么区别?也是阅读 documentation 后暂停的注释我不清楚它的目的。

@Asynchronous 注释只是告诉程序在新线程上执行此方法吗?它是执行“new Thread(.....)”的便捷方法吗?

更新:这个注释解除了卡在请求处理线程上的服务器。吞吐量可以更好。反正来自官方docs :

Request processing on the server works by default in a synchronous processing mode, which means that a client connection of a request is processed in a single I/O container thread. Once the thread processing the request returns to the I/O container, the container can safely assume that the request processing is finished and that the client connection can be safely released including all the resources associated with the connection. This model is typically sufficient for processing of requests for which the processing resource method execution takes a relatively short time. However, in cases where a resource method execution is known to take a long time to compute the result, server-side asynchronous processing model should be used. In this model, the association between a request processing thread and client connection is broken. I/O container that handles incoming request may no longer assume that a client connection can be safely closed when a request processing thread returns. Instead a facility for explicitly suspending, resuming and closing client connections needs to be exposed. Note that the use of server-side asynchronous processing model will not improve the request processing time perceived by the client. It will however increase the throughput of the server, by releasing the initial request processing thread back to the I/O container while the request may still be waiting in a queue for processing or the processing may still be running on another dedicated thread. The released I/O container thread can be used to accept and process new incoming request connections.

最佳答案

@Suspended 有没有用过就更确定了,不然跟用起来没什么区别。

让我们谈谈它的好处:

  • @Suspended 将暂停/挂起当前线程,直到它获得响应,默认情况下 #NO_TIMEOUT 没有设置挂起超时。因此,这并不意味着您的请求响应 (I/O) 线程将获得空闲并可用于其他请求。
  • 现在假设您希望您的服务在某个特定时间做出响应,但是您从资源调用的方法不能保证响应时间,那么您将如何管理您的服务响应时间?届时,您可以使用 @Suspended 为您的服务设置暂停超时,甚至可以在超时时提供回退响应。

下面是一些设置挂起/暂停超时的代码示例

public void longRunningOperation(@Suspended AsyncResponse ar) {
ar.setTimeoutHandler(customHandler);
ar.setTimeout(10, TimeUnit.SECONDS);
final String result = executeLongRunningOperation();
ar.resume(result);
}

for more details refer this

关于java - jersey ws 2.0 @suspended AsyncResponse,它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002246/

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