BVB Source Codes

spring-mvc-showcase Show CallableController.java Source code

Return Download spring-mvc-showcase: download CallableController.java Source code - Download spring-mvc-showcase Source code - Type:.java
  1. package org.springframework.samples.mvc.async;
  2.  
  3. import java.util.concurrent.Callable;
  4.  
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.context.request.async.WebAsyncTask;
  12.  
  13. @Controller
  14. @RequestMapping("/async/callable")
  15. public class CallableController {
  16.  
  17.  
  18.         @RequestMapping("/response-body")
  19.         public @ResponseBody Callable<String> callable() {
  20.  
  21.                 return new Callable<String>() {
  22.                         @Override
  23.                         public String call() throws Exception {
  24.                                 Thread.sleep(2000);
  25.                                 return "Callable result";
  26.                         }
  27.                 };
  28.         }
  29.  
  30.         @RequestMapping("/view")
  31.         public Callable<String> callableWithView(final Model model) {
  32.  
  33.                 return new Callable<String>() {
  34.                         @Override
  35.                         public String call() throws Exception {
  36.                                 Thread.sleep(2000);
  37.                                 model.addAttribute("foo", "bar");
  38.                                 model.addAttribute("fruit", "apple");
  39.                                 return "views/html";
  40.                         }
  41.                 };
  42.         }
  43.  
  44.         @RequestMapping("/exception")
  45.         public @ResponseBody Callable<String> callableWithException(
  46.                         final @RequestParam(required=false, defaultValue="true") boolean handled) {
  47.  
  48.                 return new Callable<String>() {
  49.                         @Override
  50.                         public String call() throws Exception {
  51.                                 Thread.sleep(2000);
  52.                                 if (handled) {
  53.                                         // see handleException method further below
  54.                                         throw new IllegalStateException("Callable error");
  55.                                 }
  56.                                 else {
  57.                                         throw new IllegalArgumentException("Callable error");
  58.                                 }
  59.                         }
  60.                 };
  61.         }
  62.  
  63.         @RequestMapping("/custom-timeout-handling")
  64.         public @ResponseBody WebAsyncTask<String> callableWithCustomTimeoutHandling() {
  65.  
  66.                 Callable<String> callable = new Callable<String>() {
  67.                         @Override
  68.                         public String call() throws Exception {
  69.                                 Thread.sleep(2000);
  70.                                 return "Callable result";
  71.                         }
  72.                 };
  73.  
  74.                 return new WebAsyncTask<String>(1000, callable);
  75.         }
  76.  
  77.         @ExceptionHandler
  78.         @ResponseBody
  79.         public String handleException(IllegalStateException ex) {
  80.                 return "Handled exception: " + ex.getMessage();
  81.         }
  82.  
  83. }
  84.  
downloadCallableController.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
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
Waves - Click effect inspired by Google's Material Design ... 2017-02-21
PlainTasks - An opinionated todo-list plugin for Sublime Text e... 2017-02-21
Torque3D - MIT Licensed Open Source version of Torque 3D from... 2017-02-21
should.js - BDD style assertions for node.js -- test framework... 2017-02-21

 Back to top