In this section, you will learn about Asynchronous Request Processing in Spring MVC 3.2.
Before understanding asynchronous request processing, you must be familiar with Servlet 3 async processing feature.
Given below the sequence of events of Servlet 3 async processing :
Spring 3.2 asynchronous request processing is based on Servlet 3 based asynchronous request processing. Spring 3.2 asynchronous request processing returns java.util.concurrent.Callable and DeferredResult instead of returning a value and bring out value from a separate thread.
When a application requires to return value asynchronously within a thread handled by Spring MVC, a java.util.concurrent.Callable is returned. Given below the example of a controller method returning Callable :
@RequestMapping(method=RequestMethod.POST)
public Callable<String> UploadFile(final MultipartFile file) {
return new Callable<String>() {
public Object call() throws Exception {
// ...
return "anyView";
}
};
}
When a application requires to return value asynchronously from a thread of
its own selection, a DeferredResult can be returned.
Given below the example of a controller method returning DeferredResult
:
@RequestMapping("/statements")
@ResponseBody
public DeferredResult<String> statements() {
DeferredResult<String> defResult = new DeferredResult<String>();
// Save the defResult in memory
return defResult;
}
// In some other thread...
defResult.setResult(data);
Given below the sequence for Asynchronous Request Processing with Callable :
Given below the sequence for Asynchronous Request Processing with a DeferredResult :
|
Recommend the tutorial |
Ask Questions? Discuss: Spring 3.2 Asynchronous Request Processing
Post your Comment