BVB Source Codes

spring-mvc-showcase Show DeferredResultController.java Source code

Return Download spring-mvc-showcase: download DeferredResultController.java Source code - Download spring-mvc-showcase Source code - Type:.java
  1. package org.springframework.samples.mvc.async;
  2.  
  3. import java.util.Queue;
  4. import java.util.concurrent.ConcurrentLinkedQueue;
  5.  
  6. import org.springframework.scheduling.annotation.Scheduled;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.ExceptionHandler;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.context.request.async.DeferredResult;
  12. import org.springframework.web.servlet.ModelAndView;
  13.  
  14. @Controller
  15. @RequestMapping("/async")
  16. public class DeferredResultController {
  17.  
  18.         private final Queue<DeferredResult<String>> responseBodyQueue = new ConcurrentLinkedQueue<DeferredResult<String>>();
  19.  
  20.         private final Queue<DeferredResult<ModelAndView>> mavQueue = new ConcurrentLinkedQueue<DeferredResult<ModelAndView>>();
  21.  
  22.         private final Queue<DeferredResult<String>> exceptionQueue = new ConcurrentLinkedQueue<DeferredResult<String>>();
  23.  
  24.  
  25.         @RequestMapping("/deferred-result/response-body")
  26.         public @ResponseBody DeferredResult<String> deferredResult() {
  27.                 DeferredResult<String> result = new DeferredResult<String>();
  28.                 this.responseBodyQueue.add(result);
  29.                 return result;
  30.         }
  31.  
  32.         @RequestMapping("/deferred-result/model-and-view")
  33.         public DeferredResult<ModelAndView> deferredResultWithView() {
  34.                 DeferredResult<ModelAndView> result = new DeferredResult<ModelAndView>();
  35.                 this.mavQueue.add(result);
  36.                 return result;
  37.         }
  38.  
  39.         @RequestMapping("/deferred-result/exception")
  40.         public @ResponseBody DeferredResult<String> deferredResultWithException() {
  41.                 DeferredResult<String> result = new DeferredResult<String>();
  42.                 this.exceptionQueue.add(result);
  43.                 return result;
  44.         }
  45.  
  46.         @RequestMapping("/deferred-result/timeout-value")
  47.         public @ResponseBody DeferredResult<String> deferredResultWithTimeoutValue() {
  48.  
  49.                 // Provide a default result in case of timeout and override the timeout value
  50.                 // set in src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
  51.  
  52.                 return new DeferredResult<String>(1000L, "Deferred result after timeout");
  53.         }
  54.  
  55.         @Scheduled(fixedRate=2000)
  56.         public void processQueues() {
  57.                 for (DeferredResult<String> result : this.responseBodyQueue) {
  58.                         result.setResult("Deferred result");
  59.                         this.responseBodyQueue.remove(result);
  60.                 }
  61.                 for (DeferredResult<String> result : this.exceptionQueue) {
  62.                         result.setErrorResult(new IllegalStateException("DeferredResult error"));
  63.                         this.exceptionQueue.remove(result);
  64.                 }
  65.                 for (DeferredResult<ModelAndView> result : this.mavQueue) {
  66.                         result.setResult(new ModelAndView("views/html", "javaBean", new JavaBean("bar", "apple")));
  67.                         this.mavQueue.remove(result);
  68.                 }
  69.         }
  70.  
  71.         @ExceptionHandler
  72.         @ResponseBody
  73.         public String handleException(IllegalStateException ex) {
  74.                 return "Handled exception: " + ex.getMessage();
  75.         }
  76.  
  77. }
  78.  
downloadDeferredResultController.java Source code - Download spring-mvc-showcase Source code
Related Source Codes/Software:
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
micro - Async HTTP microservices made easy ... 2017-01-12
gobot - Golang framework for robotics, drones, and the Int... 2017-01-11
mas - 2017-01-13
Zappa - Serverless Python Web Services ... 2017-01-12
FrameworkBenchmarks - Source code for the framework benchmarking project... 2017-02-22
buildbot - Python-based continuous integration testing framew... 2017-02-22
rufus - The Reliable USB Formatting Utility ... 2017-02-22
uiGradients - Beautiful colour gradients for design and code ... 2017-02-22
Haxl - A Haskell library that simplifies access to remote... 2017-02-21
duktape - Duktape - embeddable Javascript engine with a focu... 2017-02-21
scala-best-practices - A collection of Scala best practice 2017-02-21
hammerspoon - Staggeringly powerful OS X desktop automation with... 2017-02-21
DGElasticPullToRefresh - Elastic pull to refresh for iOS developed in Swift 2017-02-21
evercookie - evercookie is a javascript API that produces extre... 2017-02-21

 Back to top