springboot設置程序執行超時時間
springboot2.x
方法一,通過配置文件:
spring.mvc.async.request-timeout=2s
webconfig需要繼承WebMvcConfigurerAdapter,有點過時了這個
public class WebMvcConfig extends WebMvcConfigurerAdapter{
}
controller代碼
@GetMapping("/index")
@ResponseBody
public Callable<JsonResult> index() {
return new Callable<JsonResult>() {
@Override
public JsonResult call() {
try {
Thread.sleep(60000);
} catch (InterruptedException e){
Thread.interrupted();
return JsonResult.failure("執行失敗");
}catch (Exception e){
e.printStackTrace();
return JsonResult.failure("執行失敗");
}
return JsonResult.success();
}
};
}
方法二:
webconfig實現WebMvcConfigurationSupport 的bean
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(1);
configurer.registerCallableInterceptors(timeoutInterceptor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
}
controller代碼
@GetMapping("/index")
@ResponseBody
public Callable<JsonResult> index() throws InterruptedException {
return new Callable<JsonResult>() {
@Override
public JsonResult call() throws Exception {
Thread.sleep(60000);
return JsonResult.success();
}
};
}
