开发者

How to use Servlet 3 @WebServlet & async with Spring MVC 3?

I would like to integrate the servlet 3.0 async support with spring MVC. So开发者_Python百科mething like:

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

is it possible?


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.


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜