views:

254

answers:

2

I wanna 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?

+1  A: 

Not yet implemented in Spring Framework 3.x. See https://jira.springframework.org/browse/SPR-5587

Additionally, bear in mind that you are talking about Servlet 3.0 specification and your average webcontainer (tomcat and jetty in their stable versions) won't support it. I think the latest version of Tomcat (7) already supports it, but it's still beta. I've tried it and I get all sort of exceptions.

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.

monzonj
A: 

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.

Krish Bheeman