方式一
@GetMapping("/test")
public WebAsyncTask<CommonResult> test() throws Exception {
log.info("主线程开始");
Callable<CommonResult> result = (() -> {
log.info("副线程开始");
Thread.sleep(10000);
log.info("副线程结束");
return CommonResult.success(null,"success");
}); //直接Callable也可以,但是不能设置超时时间和超时回调以及成功回调
WebAsyncTask<CommonResult> webAsyncTask = new WebAsyncTask<>(300000, result);
webAsyncTask.onTimeout(timeOutCallBack());
log.info("主线程结束");
return webAsyncTask;
}
/**
* 超时回调
*/
private Callable<CommonResult> timeOutCallBack(){
Callable<CommonResult> callback = (()-> {
log.info("请求超时");
return CommonResult.failed("请求超时");
});
return callback;
}