球衣ws 2.0 @suspended AsyncResponse,它有什么作用? [英] jersey ws 2.0 @suspended AsyncResponse, what does it do?

查看:117
本文介绍了球衣ws 2.0 @suspended AsyncResponse,它有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分析某些jersey 2.0代码,并且对以下方法的工作方式有疑问:

I am analyzing some jersey 2.0 code and i have a question on how the following method works:

@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 这将执行该方法,但无法理解@Asynchronous注释均未使用asyncResponse.从浏览器中,我如何注意到它的异步性?删除注释有什么区别?阅读文档我不清楚其目的.

Lets say im at a web browser and i type in www.mysite/mycoolstuff this will execute the method but im not understanding what the asyncResponse is used for neither the @Asynchronous annotation. From the browser how would i notice its asychnronous ? what would be the difference in removing the annotation ? Also the suspended annotation after reading the documentation i'm not clear its purpose.

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

is the @Asynchronous annotation simply telling the program to execute this method on a new thread ? is it a convenience method for doing "new Thread(.....)" ?

更新:此批注减轻了服务器挂在请求处理线程上的麻烦.吞吐量可能会更好.无论如何,从官方文档:

Update: this annotation relieves the server of hanging onto the request processing thread. Throughput can be better. Anyway from the official docs:

默认情况下,服务器上的请求处理在同步处理模式下工作,这意味着请求的客户端连接在单个I/O容器线程中处理.一旦处理请求的线程返回到I/O容器,该容器就可以安全地假定请求处理已完成,并且可以安全地释放客户端连接,包括与该连接关联的所有资源.该模型通常足以处理处理资源方法执行花费相对较短时间的请求.但是,在已知执行资源方法要花费很长时间来计算结果的情况下,应使用服务器端异步处理模型.在此模型中,请求处理线程和客户端连接之间的关联被破坏.处理请求的I/O容器可能不再假定在请求处理线程返回时可以安全地关闭客户端连接.相反,需要公开显式挂起,恢复和关闭客户端连接的功能.请注意,使用服务器端异步处理模型不会改善客户端感知的请求处理时间.但是,通过将初始请求处理线程释放回I/O容器,可以增加服务器的吞吐量,而请求可能仍在队列中等待处理,或者处理仍在另一个专用线程上运行.释放的I/O容器线程可用于接受和处理新的传入请求连接.

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 have more definite if you used it, else it not makes any difference of using it.let's talk about benefits of its.

  • @Suspended将暂停/暂停当前线程,直到获得响应为止,默认情况下#NO_TIMEOUT没有设置暂停超时.因此,这并不意味着您的请求响应线程是免费的,并且可供其他人使用.
  • 现在假设您希望服务在特定时间响应,但是从资源中调用的方法不能保证响应时间,那么您将如何管理服务响应时间.在那时,您可以设置暂停超时使用@Suspended为您的服务提供服务,甚至在时间超过时提供回退响应.

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

Below is some sample of code for setting suspend/pause timeout

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

有关详情,请参阅

这篇关于球衣ws 2.0 @suspended AsyncResponse,它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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