zoukankan      html  css  js  c++  java
  • 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();
                }
            };
    
        }
    

      

  • 相关阅读:
    python三大神器
    centos安装fish shell
    linux查看日志
    web攻击
    web安全之XSS
    JSONP && CORS
    css 定位
    MIT-线性代数笔记(1-6)
    es6 Object.assign
    ECMAScript 6 笔记(六)
  • 原文地址:https://www.cnblogs.com/achengmu/p/13043895.html
Copyright © 2011-2022 走看看