简体   繁体   English

异步JAX-RS

[英]Asynchronous JAX-RS

My doubt is regarding the working of the Asynchronous JAX-RS which is kind of new to me and I'm trying to grasp its advantage. 我对Asynchronous JAX-RS的工作感到怀疑,这对我来说是新的,我正在尝试掌握其优势。 What I understand is that the client sends a request, the request is delegated from the requestor thread to the worker thread and once the processing is completed the response is sent back to the client using the AsyncResponse. 我了解的是,客户端发送请求,将请求从请求者线程委派给工作线程,一旦处理完成,则使用AsyncResponse将响应发送回客户端。 What I've also understood is that throughout the process the client waits for a response from the server. 我还了解到,在整个过程中,客户端都在等待服务器的响应。 (So as far as the client is concerned its the same as a normal Synchronous request) (就客户端而言,它与普通的同步请求相同)

It also states that as the request thread is sent to a worker thread for further processing and therefore by having this approach the I/O threads are free to accept new connections. 它还指出,当请求线程被发送到工作线程进行进一步处理时,因此,通过采用这种方法,I / O线程可以自由接受新连接。

What I did not understand is, the client is still waiting for a response and therefore an active connection is still maintained between the client and server. 我不了解的是,客户端仍在等待响应,因此客户端与服务器之间仍保持活动连接。 Is this not maintained in an I/O thread? 这不是在I / O线程中维护的吗? What does suspended mean here? 暂停在这里意味着什么?

Also, even if the case is that the I/O thread is released because of the delegation of the process to a worker connection with the client is still up how can the server accept more and more connections then? 此外,即使是由于将进程委派给与客户端的工作者连接而释放I / O线程的情况仍然存在,服务器又如何才能接受越来越多的连接呢?

And my next question is about the thread pool used here. 我的下一个问题是关于此处使用的线程池。 The I/O threads and worker threads are from different pools? I / O线程和辅助线程来自不同的池? are the worker/processor threads not coming from a pool managed by the server? 工作线程/处理器线程不是来自服务器管理的池吗?

Because of my failure to understand this, my next pondering is, just having a separate pool for the I/O and the processing with the client connection still up is the same as having the I/O blocked with the processing inside right? 由于无法理解这一点,因此我的下一个思考是,仅为I / O设置一个单独的池,并且在客户端连接仍处于启动状态下进行处理与在内部进行处理时阻止I / O进行处理一样吗?

I haven't grasped this concept very well. 我还没有很好地理解这个概念。

The thread pools in use in this scenario are: 在这种情况下使用的线程池是:

  1. The request processing pool, managed by the JAX-RS container (eg Jersey), and 由JAX-RS容器(例如Jersey)管理的请求处理池,以及
  2. The worker thread pool, typically managed by your own code. 工作线程池,通常由您自己的代码管理。

There is possibly an IO thread associated with the connection, but that's an implementation detail that doesn't affect this. 该连接可能有一个IO线程,但这是一个实现细节,不会影响此连接。

When you use AsyncResponse , as soon as you return from your handle method, the request processing thread (from pool #1) is freed and can be used by the container to handle another request. 当您使用AsyncResponse ,从句柄方法返回后,请求处理线程(来自池#1)将被释放,容器可以使用它来处理另一个请求。

On to your questions: 关于您的问题:

  1. "... how can the server accept more and more connections?" “ ...服务器如何接受越来越多的连接?” - it can accept more connections than if you do not use AsyncResponse for long-running requests, because you are freeing up one of your limited resources (threads in thread pool #1). -与不对长时间运行的请求不使用AsyncResponse相比,它可以接受更多的连接,因为您正在释放有限的资源之一(线程池#1中的线程)。 The connection itself is not freed, and connections are also limited resources, so you can run out of those still (as well as possibly being limited by CPU or memory). 连接本身不会被释放,连接也是有限的资源,因此您仍然可以用尽这些资源(以及可能受到CPU或内存的限制)。
  2. "are the worker/processor threads not coming from a pool managed by the server?" “工作线程/处理器线程不是来自服务器管理的池吗?” - not normally. -不正常。 See the example in the link from Paul Samsotha here - the application code creates a new thread itself. 此处查看 Paul Samsotha的链接中的示例-应用程序代码本身会创建一个新线程。 This is probably not how you would do it, you would most likely want to use an ExecutorService or similar, but the point is that you manage the "worker thread" yourself. 这可能不是您将要执行的方式,您很可能希望使用ExecutorService或类似服务,但是重点是您自己管理“工人线程”。 The exception here is if you use Jersey's @ManagedAsync annotation. 如果使用Jersey的@ManagedAsync批注,则是一个例外。
  3. Your last question should be answered by my answer to #1 - at a lower level there is still a resource associated with a connection, even if you are using AsyncResponse , but AsyncResponse does free up the container's request processing threads, which can be more limited in number than the maximum number of connections. 您的最后一个问题应该由我对#1的回答来回答-在较低级别上,即使您使用的是AsyncResponse ,仍然有与连接关联的资源,但是AsyncResponse确实释放了容器的请求处理线程,这可能会受到更多限制在数量上超过最大连接数。 You may choose to handle this problem by changing the server configuration instead of using AsyncResponse , but AsyncResponse has two advantages - it is under the application's control, and it is per-request instead of per-server. 您可以选择通过更改服务器配置而不是使用AsyncResponse来解决此问题,但是AsyncResponse具有两个优点-它在应用程序的控制下,并且是按请求而不是按服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM