简体   繁体   English

Servlet中的异步功能

[英]Async feature in Servlets

I was just going back to Servlet-3.x features and exploring it. 我只是回到Servlet-3.x功能并进行了探索。 If I am not wrong, before Servlet-3.x it was thread per request model and it would run out of threads in the pool, for heavy incoming traffic. 如果我没看错,则在Servlet-3.x之前,它是每个请求模型的线程,它将耗尽池中的线程,从而导致大量传入流量。

So, with Servlet-3.x it says it is Asynchronous and doesn't keep the threads blocked , rather releases them immediately but just the task is delegated. 因此,使用Servlet-3.x表示它是异步的,不会使线程保持阻塞状态,而是立即释放它们,而只是委派任务。

Here is my interpretation, 这是我的解释,

consider there are 2 threads in Server thread-pool 考虑服务器线程池中有2个线程

For a new Async Servlet request R1 there is a thread T1 , this T1 would delegate the task to T2 and T1 responds back to client immediately. 对于新的异步servlet请求R1有一个线程T1 ,该T1将委派任务, T2T1立即响应回客户端。

Question: Is T2 created from Server thread-pool? 问题: T2是从服务器线程池创建的吗? If so, I don't get the point. 如果是这样,我不明白这一点。

  • Case 1: If it was old Synchronous Servlet request T1 would have been busy running I/O task, 情况1:如果旧的同步Servlet请求T1一直在忙于运行I / O任务,

  • Case 2: If it was Asynchronous Servlet call T2 is busy running I/O task. 情况2:如果是异步Servlet调用,则T2正在忙于运行I / O任务。

  • In both cases, one of them is busy. 在这两种情况下,其中一个都很忙。

I tried to check the same with a sample Async servlet in openliberty app server, below is the sample log captured from my sample demo Servlet. 我尝试在openliberty应用服务器中使用示例异步Servlet进行openliberty ,以下是从示例演示Servlet捕获的示例日志。

Entering doGet() == thread name is = Default Executor-thread-116
Exiting doGet() == thread name is = Default Executor-thread-116
=== Long running task started ===
Thread executing @start of long running task = Default Executor-thread-54
Thread executing @end of long running task = Default Executor-thread-54
=== Long running task ended ===

As shown above, the Default Executor-thread-116 is released immediately and delegated long running task to the Default Executor-thread-54 , but I am not sure if they are from the App Server thread pool. 如上所示, Default Executor-thread-116立即释放,并将长期运行的任务委托给Default Executor-thread-54 ,但是我不确定它们是否来自App Server线程池。 If so, why can't just Default Executor-thread-116 do the task instead of delegation? 如果是这样,为什么不能仅由Default Executor-thread-116执行任务而不是委派?

Can someone throw some light on this async behavior of Servlets in JavaEE 有人可以阐明JavaEE中Servlet的这种异步行为吗

In your example, where the work is synchronous and there's no separate executor/threadpool, there is nearly no point to use async servlets. 在您的示例中,工作是同步的,没有单独的执行程序/线程池,几乎没有意义使用异步servlet。 Lots of samples/examples out there are just block on a 2nd thread because they're trying to illustrate just the syntax. 许多示例/示例在第二个线程上只是块,因为它们试图仅说明语法。

But there's no reason why you can't spin off a thread to do a little work, add your async context to some list, and then after some event (inbound JMS, websocket, whatever) provides the data needed to complete the async response. 但是没有理由不让您无法剥离线程来做一些工作,将异步上下文添加到某个列表,然后在某个事件(入站JMS,websocket等)提供完成异步响应所需的数据之后。 For example, a 2-player game server wouldn't wait for player 2 in a second thread, it would just have their async context floating around in memory waiting for a 2nd player to find it. 例如,一个2人游戏服务器不会在第二个线程中等待玩家2,而只是将他们的异步上下文在内存中浮动,以等待第二个玩家找到它。

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

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