Spring code example with a large number of characters?

I can find a lot of information about how Long Polling works (for example, this and this), but there are no simple examples of how to implement this in code.

Basically, how can I use Apache Tomcat to serve requests and how to write a simple (spring) application that will be a "long poll" of the server for new messages?

An example doesn't have to be scalable, secure or complete, it just needs to work! I would appreciate it if someone could give me such a textbook or turn to another.

+5
source share
2 answers

Here is the simplest example I can come up with ...

In the controller:

@RequestMapping("/longPolling")
public String longPolling(Model model) {
  while(true) {
    // .. Do something, break when done...
    if( somethingIsDone ) {
       break;
    }
  }
  return "someResponse";
}

"" ajax, - . , , - .

, Spring 3.2, - DeferredResult   Callable . Spring 3.2, , , , , Atmosphere, Spring. JavaScript, .

+3

anwser " " , API Servlet 3 (Tomcat 7) Spring 3.2

@RequestMapping("/longPolling")
public Future<String> longPolling(Model model) {
  return callSomethingWhereAFutureReturns();
}

: - "" ( ) . , / , , . , /a ( ). "Spring", POJO, JSON.

, . - ( ) ().

0

All Articles