springboot设置程序执行超时时间

springboot设置程序执行超时时间

springboot2.x

方法一,通过配置文件:

spring.mvc.async.request-timeout=2s

  

webconfig需要继承WebMvcConfigurerAdapter,有点过时了这个

public class WebMvcConfig extends WebMvcConfigurerAdapter{


}

  

controller代码

 @GetMapping("/index")
    @ResponseBody
    public Callable  index() {

        return new Callable() {
            @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  index() throws InterruptedException {

        

        return new Callable() {
            @Override
            public JsonResult call() throws Exception {
                Thread.sleep(60000);
                return JsonResult.success();
            }
        };

    }

  

 

你可能感兴趣的:(springboot设置程序执行超时时间)