如何使用Servlet 3 @WebServlet &与 Spring MVC 3 异步?

发布于 2024-09-11 06:45:28 字数 221 浏览 9 评论 0原文

我想将 servlet 3.0 异步支持与 spring MVC 集成。比如:

@RequestMapping("/chat")
@WebServlet(name="myServlet", asyncSupported=true)
public String getMessage(String userName) {
      ......
}

这可能吗?

I would like to integrate the servlet 3.0 async support with spring MVC. Something like:

@RequestMapping("/chat")
@WebServlet(name="myServlet", asyncSupported=true)
public String getMessage(String userName) {
      ......
}

is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

∞觅青森が 2024-09-18 06:45:28

没有那么快,实现好的长轮询也不是那么容易。
你提到的方法效果很好,但是存在严重的“线程饥饿”问题

,每个长轮询都会耗尽一个线程,如果你有1000个并发用户,你将需要1000个线程来服务长轮询请求(大多数情况下) Jetty 6 有一个连续模式,

它巧妙地释放长轮询请求的线程以供实际应用程序逻辑使用。

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)

Jetty 6 has a continue pattern whcih cleverly releases the thread of long polling request to be used by rhe real application logic.

冷了相思 2024-09-18 06:45:28

Spring Framework 3.x 中尚未实现。请参阅 https://jira.springframework.org/browse/SPR-5587 和 < a href="https://jira.springsource.org/browse/SPR-8517" rel="nofollow noreferrer">https://jira.springsource.org/browse/SPR-8517

如果你想要什么comet 支持(长轮询 ajax) 你“可能”尝试 CometD ( http://cometd.org/documentation/cometd-java/server/services/integration-spring)。但我警告你,我已经把它扔掉了,它只是为了臃肿! (像长轮询这样简单的事情需要几天的配置?!)

我会在 Spring3 中自己实现一些 RESTful 控制器,并为自己编写长轮询。向服务器发出 Ajax 风格的请求,控制器将其保持打开状态,直到服务器有新数据发送到浏览器。浏览器发起新的长轮询请求以获取后续事件。为了避免连接超时,只需返回使客户端重复请求的虚拟值。

大多数时候,简单简单的方法是最好的解决方案。

Not yet implemented in Spring Framework 3.x. See https://jira.springframework.org/browse/SPR-5587 and 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??!)

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.

Plain easy way is most of the time the best solutions.

少女净妖师 2024-09-18 06:45:28

这个问题很老了,但仍然没有答案。作者希望在Spring MVC中支持异步,但仍然没有给出解决方案。

正如之前的回答所述,异步支持请求已提交给 Spring 社区 bugtracker,并将在 Spring 3.1.0 中实现。这是最近发布的,但根据发行说明,计划在 3.2.0 版本中提供“Servlet 3.0 支持”: https://jira.springsource.org/browse/SEC-1685

我的应用程序需要高效的 COMET。我当前的实现基于以下示例:http://code.google.com /p/jquery-stream/wiki/EchoExample,但我有兴趣将其移至 Spring MVC 控制器。就目前而言,我刚刚改进了示例并手动注入了 spring bean,以允许与应用程序的其余部分进行通信。我遇到了一些问题,如我的问题所述: Tomcat 7 异步处理失败 - 仅同时处理一个请求。就目前而言,它运行良好。

我找到了使用jboss解决方案的示例: http://docs.jboss.org/resteasy/docs/1.0.0.GA/userguide/html/Asynchronous_HTTP_Request_Processing.html,但对我来说使用jboss并不是解决方案。 JBoss 太大、太慢并且太难开发。

The question is quite old, but still unanswered. The author wanted async support in Spring MVC, and the solution is still not given.

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

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.

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.

梦初启 2024-09-18 06:45:28

您现在可以使用出色的 Atmosphere 库来完成此操作:

这是一个 Spring MVC 示例:
https://github.com/ghillert/atmosphere-spring-web-mvc

You can sort of do this now with the fantastic Atmosphere library:

Here is a Spring MVC example:
https://github.com/ghillert/atmosphere-spring-web-mvc

千纸鹤带着心事 2024-09-18 06:45:28

目前正在考虑 Servlet 3.0 异步支持。如果您希望看到具体的场景,请在 SPR-8517 下发表评论或观看票证并关注讨论。

Servlet 3.0 async support is currently being considered. If you have specific scenarios you'd like to see reflected, please comment under SPR-8517 or watch the ticket and follow the discussion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文