11package cn .com .ttblog .ssmbootstrap_table .controller ;
22
3- import java .io .IOException ;
4- import java .util .Arrays ;
5- import java .util .Map ;
6- import java .util .concurrent .Callable ;
7- import java .util .concurrent .TimeUnit ;
8- import javax .annotation .Resource ;
9- import javax .servlet .http .HttpServletRequest ;
10- import javax .servlet .http .HttpServletResponse ;
11- import javax .servlet .http .HttpSession ;
12-
13- import org .apache .commons .io .IOUtils ;
3+ import cn .com .ttblog .ssmbootstrap_table .model .User ;
4+ import cn .com .ttblog .ssmbootstrap_table .service .IUserService ;
5+ import com .alibaba .fastjson .JSON ;
6+ import com .alibaba .fastjson .JSONObject ;
7+ import com .google .common .collect .Maps ;
148import org .apache .commons .lang3 .builder .ToStringBuilder ;
159import org .slf4j .Logger ;
1610import org .slf4j .LoggerFactory ;
1711import org .springframework .stereotype .Controller ;
1812import org .springframework .ui .Model ;
19- import org .springframework .web .bind .annotation .ModelAttribute ;
20- import org .springframework .web .bind .annotation .PathVariable ;
21- import org .springframework .web .bind .annotation .RequestBody ;
22- import org .springframework .web .bind .annotation .RequestMapping ;
23- import org .springframework .web .bind .annotation .RequestMethod ;
24- import org .springframework .web .bind .annotation .RequestParam ;
25- import org .springframework .web .bind .annotation .ResponseBody ;
26- import org .springframework .web .bind .annotation .SessionAttributes ;
13+ import org .springframework .web .bind .annotation .*;
2714import org .springframework .web .context .request .async .DeferredResult ;
2815
29- import com .alibaba .fastjson .JSON ;
30- import com .alibaba .fastjson .JSONObject ;
31-
32- import cn .com .ttblog .ssmbootstrap_table .model .User ;
33- import cn .com .ttblog .ssmbootstrap_table .service .IUserService ;
16+ import javax .annotation .Resource ;
17+ import javax .servlet .http .HttpServletRequest ;
18+ import javax .servlet .http .HttpServletResponse ;
19+ import javax .servlet .http .HttpSession ;
20+ import java .io .IOException ;
21+ import java .util .Arrays ;
22+ import java .util .Map ;
23+ import java .util .concurrent .Callable ;
24+ import java .util .concurrent .TimeUnit ;
3425
3526/**
3627 * jsonp测试
@@ -73,6 +64,7 @@ public void testModelAttr(HttpSession session, Model model) {
7364 * 通过一个TaskExecutor,Spring
7465 * MVC可以在另外的线程中调用Callable。当Callable返回时,请求再携带Callable返回的值
7566 * ,再次被分配到Servlet容器中恢复处理流程
67+ * <mvc:async-support default-timeout="5000"></mvc:async-support> spring mvc中配置默认的超时时间
7668 * @param str
7769 * @return
7870 */
@@ -91,19 +83,21 @@ public String call() throws Exception {
9183
9284 @ RequestMapping (value = "/asyncd" , method = RequestMethod .GET )
9385 @ ResponseBody
94- public DeferredResult <String > asyncd (final String str ) {
95- DeferredResult <String > dr = new DeferredResult <String >();
96- dr . onCompletion (new Runnable () {
86+ public DeferredResult <String > asyncd (final String str , @ RequestParam ( value = "sleep" , required = false , defaultValue = "3" ) final Integer sleep ) {
87+ final DeferredResult <String > dr = new DeferredResult <String >();
88+ new Thread (new Runnable () {
9789 @ Override
9890 public void run () {
91+ logger .info ("asyncd task start" );
9992 try {
100- TimeUnit .SECONDS .sleep (3 );
93+ TimeUnit .SECONDS .sleep (sleep );
10194 } catch (InterruptedException e ) {
10295 e .printStackTrace ();
10396 }
104- logger .debug ("run with:{}" ,str );
97+ logger .info ("asyncd task end" );
98+ dr .setResult ("hello:" +str );
10599 }
106- });
100+ }, "deffered-execute-thread" ). start ( );
107101 return dr ;
108102 }
109103
@@ -282,4 +276,13 @@ Map receivejsonobj(@RequestBody Map user,HttpServletRequest request) {
282276 logger .debug ("name:{}" , name );
283277 return session .getAttribute ("name" ).toString ();
284278 }
279+
280+ @ ExceptionHandler
281+ @ ResponseBody
282+ public Map handleAllException (Exception ex ) {
283+ Map result = Maps .newHashMap ();
284+ result .put ("success" ,false );
285+ result .put ("msg" ,ex .getMessage ());
286+ return result ;
287+ }
285288}
0 commit comments