Bhaskar
Bhaskar

Reputation: 143

spring deferredresult

I am new to spring and want to implement long polling for a website to display admin message immediately when it becomes available to all clients,i searched google for hours and could only find out deferredresult(spring 3.2) can be used to implement it.my question is how i can achieve long polling with deferredresult, I would appreciate it if anyone could refer me to such a tutorial.

Upvotes: 0

Views: 1043

Answers (3)

elirandav
elirandav

Reputation: 2063

Short background about DeferredResult: Your controller is eventually a function executed by the servlet container (for that matter, let's assume that the server container is Tomcat) worker thread. Your service flow start with Tomcat and ends with Tomcat. Tomcat gets the request from the client, holds the connection, and eventually returns a response to the client. Your code (controller or servlet) is somewhere in the middle.

Consider this flow:

  1. Tomcat get client request.
  2. Tomcat executes your controller.
  3. Release Tomcat thread but keep the client connection (don't return response) and run heavy processing on different thread.
  4. When your heavy processing complete, update Tomcat with its response and return it to the client (by Tomcat).

Because the servlet (your code) and the servlet container (Tomcat) are different entities, then to allow this flow (releasing tomcat thread but keep the client connection) we need to have this support in their contract, the package javax.servlet, which introduced in Servlet 3.0 . Spring MVC use this new Servlet 3.0 capability when the return value of the controller is DeferredResult or Callable, although they are two different things. Callable is an interface that is part of java.util, and it is an improvement for the Runnable interface. DeferredResult is a class designed by Spring to allow more options (that I will describe) for asynchronous request processing in Spring MVC, and this class just holds the result (as implied by its name) while your Callable implementation holds the async code. So it means you can use both in your controller, run your async code with Callable and set the result in DeferredResult, which will be the controller return value. So what do you get by using DeferredResult as the return value instead of Callable? DeferredResult has built-in callbacks like onError, onTimeout, and onCompletion. It makes error handling very easy. In addition, as it is just the result container, you can choose any thread (or thread pool) to run on your async code. With Callable, you don't have this choice.

Here you can find a simple working examples I created with both options, Callable and DeferredResult.

Upvotes: 0

2020
2020

Reputation: 2841

Another option is to use AsyncContext. This will keep the initial GET request "open" and enable you to send multiple messages as part of the response, unlike DeferredResult which allows to send only ONE response message. Here is a good-link that explains how !

Upvotes: 3

CodeChimp
CodeChimp

Reputation: 8154

Straight from the horses mouth.

You have two basic options: Option 1 is a Callable , where the Callable returns the String view name (you may also be able to use @ResponseBody or some of the other normal Spring return types like ModelAndView, but I have never investigated that).

Option two is to return DeferredResult, which is like Callable. except you can pass that off to a separate thread and fill in the results there. Again, not sure if you can return a ModelAndView or use @ResponseBody to return XML/JSON, but I am sure you can.

Upvotes: 1

Related Questions