java 如何在 Spring MVC 3 中使用 Servlet 3 @WebServlet 和异步?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3345302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Servlet 3 @WebServlet & async with Spring MVC 3?
提问by Ben
I would like to integrate the servlet 3.0 async support with spring MVC. Something like:
我想将 servlet 3.0 异步支持与 spring MVC 集成。就像是:
@RequestMapping("/chat")
@WebServlet(name="myServlet", asyncSupported=true)
public String getMessage(String userName) {
......
}
is it possible?
是否可以?
回答by Krish Bheeman
Not so fast, it is not that easy to implement good long polling. The method you mentioned works well, but there is a serious issue of "thread starvation"
没那么快,实现好的长轮询也不是那么容易。你提到的方法效果很好,但是存在“线程饥饿”的严重问题
Each Long polling will use up one thread, if you have 1000 concurrent user you would need 1000 thread to service the long polling request ( which most of the time does update of the server side status on the client browser)
每个长轮询将使用一个线程,如果您有 1000 个并发用户,则需要 1000 个线程来为长轮询请求提供服务(大部分时间都会在客户端浏览器上更新服务器端状态)
Jetty 6 has a continue pattern whcih cleverly releases the thread of long polling request to be used by rhe real application logic.
Jetty 6 有一个继续模式,它巧妙地释放了长轮询请求的线程以供实际应用程序逻辑使用。
回答by monzonj
Not yet implemented in Spring Framework 3.x. See https://jira.springframework.org/browse/SPR-5587and https://jira.springsource.org/browse/SPR-8517
尚未在 Spring Framework 3.x 中实现。见https://jira.springframework.org/browse/SPR-5587和https://jira.springsource.org/browse/SPR-8517
If what you want is comet support (long-polling ajax) You "might" try CometD (http://cometd.org/documentation/cometd-java/server/services/integration-spring). But I warn you that I have dropped it, it's just to bloated! (Such a simple thing like long polling requires days of configuration??!)
如果您想要的是彗星支持(长轮询 ajax),您“可能”尝试 CometD(http://cometd.org/documentation/cometd-java/server/services/integration-spring)。但我警告你,我已经放弃了它,只是为了臃肿!(像长轮询这样简单的事情需要几天的配置??!)
I would just implement myself some RESTful controllers in Spring3 and program myself the long polling . Make your Ajax-style request to the server, your controller keeps it open until the server has new data to send to the browser. The browser initiates a new long polling request in order to obtain subsequent events. To avoid connection timeouts just return dummy values that make the client repeat the request.
我只会在 Spring3 中为自己实现一些 RESTful 控制器,并为自己编写长轮询。向服务器发出 Ajax 样式的请求,您的控制器将保持打开状态,直到服务器有新数据要发送到浏览器为止。浏览器发起新的长轮询请求,以获取后续事件。为了避免连接超时,只需返回使客户端重复请求的虚拟值。
Plain easy way is most of the time the best solutions.
大多数时候,简单的方法是最好的解决方案。
回答by Danubian Sailor
The question is quite old, but still unanswered. The author wanted async support in Spring MVC, and the solution is still not given.
这个问题很老了,但仍然没有答案。作者想在Spring MVC中实现异步支持,至今没有给出解决方案。
As previous answer stated, async support request was submitted to spring community bugtracker, and was to be implemented in Spring 3.1.0. This got released recently, but according to release notes "Servlet 3.0 support" is planned to be made in version 3.2.0: https://jira.springsource.org/browse/SEC-1685
如前所述,异步支持请求已提交给 spring 社区 bugtracker,并将在 Spring 3.1.0 中实现。这是最近发布的,但根据发行说明“Servlet 3.0 支持”计划在 3.2.0 版中进行:https: //jira.springsource.org/browse/SEC-1685
I need highly efficient COMET for my application. My current implementation is based on this example: http://code.google.com/p/jquery-stream/wiki/EchoExample, but I'm interested in moving it to Spring MVC controller. As for now, I've just improved the example and manually injected there spring beans to allow communication with the rest of the application. I had some problems with it, described in my question: Tomcat 7 Async Processing failing - only one request processed simultanously. As for now it is working fine.
我的应用程序需要高效的 COMET。我当前的实现基于此示例:http: //code.google.com/p/jquery-stream/wiki/EchoExample,但我有兴趣将其移至 Spring MVC 控制器。至于现在,我刚刚改进了示例并手动注入了 spring bean,以允许与应用程序的其余部分进行通信。我遇到了一些问题,在我的问题中有所描述:Tomcat 7 Async Processing failed - only one request simultanously。至于现在它工作正常。
I've found example which uses jboss solutions: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Asynchronous_HTTP_Request_Processing.html, but using jboss as for me is no solution. JBoss is too big, too slow and too hard to develop on.
我找到了使用 jboss 解决方案的示例:http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Asynchronous_HTTP_Request_Processing.html,但对我来说使用 jboss 不是解决方案。JBoss 太大、太慢且难以开发。
回答by Adam Gent
You can sort of do this now with the fantastic Atmospherelibrary:
您现在可以使用出色的Atmosphere库来执行此操作:
Here is a Spring MVC example: https://github.com/ghillert/atmosphere-spring-web-mvc
这是一个 Spring MVC 示例:https: //github.com/ghillert/atmosphere-spring-web-mvc

