在Tomcat 8.5/Spring MVC上设置异步处理超时 [英] Set async processing timeout on Tomcat 8.5 / Spring MVC

查看:84
本文介绍了在Tomcat 8.5/Spring MVC上设置异步处理超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下控制器在Tomcat 8.5上部署Spring MVC Web应用程序:

I deploy a Spring MVC webapp on Tomcat 8.5 with the following controller:

import java.util.concurrent.Callable;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AppController {

    @RequestMapping(value="getOkSync", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String getOkSync() {

        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "ok";
    }

    @RequestMapping(value="getOkAsync", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Callable<String> getOkAsync() {

        return new Callable<String>() {
            @Override
            public String call()  {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return "ok";
            }
        };
    }

}

第一种方法在60秒后返回正确的结果,而第二种方法在约30秒后返回HTTP响应代码500和相应的Spring类日志

while the first method returns after 60 sec the correct result, the second method returns after approximately 30 seconds with HTTP response code 500 and the corresponding Spring class logs

Could not complete async processing due to timeout or network error.

(如果将延迟设置为20秒,则两个方法都将在预期的20秒后返回"ok").
超时是由Spring MVC还是由Tomcat控制的?控制超时的属性是什么?

(if the delay is set on the other hand to 20 seconds both metods return "ok" after 20 seconds as expected).
Is the timeout controlled by Spring MVC or by Tomcat? What is the property which controls the timeout?

推荐答案

嗯,虽然我不理解Spring和Tomcat之间的交互作用,但以下工作原理(即两种方法在60秒后现在都返回"ok")目前完全(无论如何,如果我不通过Spring设置该属性,则超时将是Tomcat的超时,尽管我不知道如何设置后者)

Well, the following works (i.e. both methods return now "ok" after 60 seconds), though there is an interaction between Spring and Tomcat which I do not understand completely at the moment (in any case it appears that if I don't set the property via Spring, the timeout will be that of Tomcat, though I don't know how to set the latter)

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="my.base.package")
public class AppConfig extends WebMvcConfigurerAdapter implements WebApplicationInitializer {

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        // TODO Auto-generated method stub
        configurer.setDefaultTimeout(120000);
        super.configureAsyncSupport(configurer);
    }

    ...

这篇关于在Tomcat 8.5/Spring MVC上设置异步处理超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆