我使用以下控制器在Tomcat 8.5上部署Spring MVC webapp:
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控制的?控制超时的属性是什么?
好吧,下面的工作(即两个方法在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);
}
...