springboot设置程序执行超时时间


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();
            }
        };

    }

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM