@GetMapping(path = "get") public Callable<String> get(){ Callable<String> callable = new Callable<String>() { @Override public String call()throws Exception { System.out.println("do some thing"); Thread.sleep(1000); System.out.println("执行完毕,返回!!!"); return"over!"; } };
return callable; }
@GetMapping(path = "exception") public Callable<String> exception(){ Callable<String> callable = new Callable<String>() { @Override public String call()throws Exception { System.out.println("do some thing"); Thread.sleep(1000); System.out.println("出现异常,返回!!!"); thrownew RuntimeException("some error!"); } };
return callable; } }
请注意上面的两种case,一个正常返回,一个业务执行过程中,抛出来异常
分别请求,输出如下
1 2 3
# http://localhost:8080/call/get do some thing 执行完毕,返回!!!
异常请求: http://localhost:8080/call/exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14
do some thing 出现异常,返回!!! 2020-03-29 16:12:06.014 ERROR 24084 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] threw exception
java.lang.RuntimeException: some error! at com.git.hui.boot.async.rest.CallableRest$2.call(CallableRest.java:40) ~[classes/:na] at com.git.hui.boot.async.rest.CallableRest$2.call(CallableRest.java:34) ~[classes/:na] at org.springframework.web.context.request.async.WebAsyncManager.lambda$startCallableProcessing$4(WebAsyncManager.java:328) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_171] at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_171] at java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_171] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_171] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_171] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]