学会使用 Spring Boot 的异步调用( 二 )

方式二:使用很简单,直接返回的参数包裹一层callable即可,可以继承WebMvcConfigurerAdapter类来设置默认线程池和超时处理 。@RequestMapping(value =https://www.isolves.com/it/cxkf/kj/2020-12-11/ "/email/callableReq", method = GET) @ResponseBody public Callable callableReq () { System.out.println("外部线程:" + Thread.currentThread().getName()); return new Callable() { @Override public String call() throws Exception { Thread.sleep(10000); System.out.println("内部线程:" + Thread.currentThread().getName()); return "callable!"; } }; } @Configuration public class RequestAsyncPoolConfig extends WebMvcConfigurerAdapter { @Resource private ThreadPoolTaskExecutor myThreadPoolTaskExecutor; @Override public void configureAsyncSupport(final AsyncSupportConfigurer configurer) { //处理 callable超时 configurer.setDefaultTimeout(60*1000); configurer.setTaskExecutor(myThreadPoolTaskExecutor); configurer.registerCallableInterceptors(timeoutCallableProcessingInterceptor()); } @Bean public TimeoutCallableProcessingInterceptor timeoutCallableProcessingInterceptor() { return new TimeoutCallableProcessingInterceptor(); }} 
方式三:和方式二差不多,在Callable外包一层,给WebAsyncTask设置一个超时回调,即可实现超时处理 。@RequestMapping(value =https://www.isolves.com/it/cxkf/kj/2020-12-11/ "/email/webAsyncReq", method = GET) @ResponseBody public WebAsyncTask webAsyncReq () { System.out.println("外部线程:" + Thread.currentThread().getName()); Callable result = () -> { System.out.println("内部线程开始:" + Thread.currentThread().getName()); try { TimeUnit.SECONDS.sleep(4); } catch (Exception e) { // TODO: handle exception } logger.info("副线程返回"); System.out.println("内部线程返回:" + Thread.currentThread().getName()); return "success"; }; WebAsyncTask wat = new WebAsyncTask(3000L, result); wat.onTimeout(new Callable() { @Override public String call() throws Exception { // TODO Auto-generated method stub return "超时"; } }); return wat; } 
方式四:DeferredResult可以处理一些相对复杂一些的业务逻辑,最主要还是可以在另一个线程里面进行业务处理及返回,即可在两个完全不相干的线程间的通信 。 
@RequestMapping(value = "/email/deferredResultReq", method = GET)    @ResponseBody    public DeferredResult<String> deferredResultReq () {        System.out.println("外部线程:" + Thread.currentThread().getName());        //设置超时时间        DeferredResult<String> result = new DeferredResult<String>(60*1000L);        //处理超时事件 采用委托机制        result.onTimeout(new Runnable() {            @Override            public void run() {                System.out.println("DeferredResult超时");                result.setResult("超时了!");            }        });        result.onCompletion(new Runnable() {            @Override            public void run() {                //完成后                System.out.println("调用完成");            }        });        myThreadPoolTaskExecutor.execute(new Runnable() {            @Override            public void run() {                //处理业务逻辑                System.out.println("内部线程:" + Thread.currentThread().getName());                //返回结果                result.setResult("DeferredResult!!");            }        });       return result;    }


推荐阅读