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

      

  • 相关阅读:
    XSD文件生成C#VO实体类
    WPF根据Oracle数据库的表,生成CS文件小工具
    【求助】WPF 在XP下 有的Textbox光标会消失
    【转】oracle中in和exists的区别
    Spire.DOC生成表格测试
    【转】C#调用Windows图片和传真查看器打开图片
    WPF MVVM下做发送短信小按钮
    SignalR Progress
    C# readonly
    Settings.settings
  • 原文地址:https://www.cnblogs.com/achengmu/p/13043895.html
Copyright © 2020-2023  润新知