在Tomcat 8.5 / Spring MVC上设置异步处理超时时间

3

我在Tomcat 8.5上部署了一个Spring MVC Web应用程序,其中包含以下控制器:

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类日志

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

如果将延迟设置为20秒,则两种方法在20秒后都如预期地返回“ok”。
超时是由Spring MVC还是Tomcat控制的?哪个属性控制超时?


在我的情况下,TC在10秒后返回503,并且日志中没有任何内容!我不使用MVC,所以不需要@EnableWebMvc。为什么它不能通过属性来获取? - undefined
1个回答

1

好的,以下方法都可以正常工作(即两种方法在60秒后都返回“ok”),尽管Spring和Tomcat之间存在一些交互,我目前还没有完全理解(无论如何,如果我不通过Spring设置属性,则超时时间将是Tomcat的超时时间,但我不知道如何设置后者)

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

    ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接