Add async request timeout property

Add a new `spring.mvc.async.request-timeout` property which can be used
to configure AsyncSupportConfigurer.setDefaultTimeout(..).

Fixes gh-2900
Closes gh-3236
This commit is contained in:
Sebastien Deleuze 2015-06-15 15:18:45 +02:00 committed by Phillip Webb
parent 295f9c6296
commit 78a7b6ed66
4 changed files with 64 additions and 1 deletions

View File

@ -66,6 +66,7 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@ -87,6 +88,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
* @author Sébastien Deleuze
*/
@Configuration
@ConditionalOnWebApplication
@ -166,6 +168,14 @@ public class WebMvcAutoConfiguration {
converters.addAll(this.messageConverters.getConverters());
}
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
Long timeout = this.mvcProperties.getAsync().getRequestTimeout();
if (timeout != null) {
configurer.setDefaultTimeout(timeout);
}
}
@Bean
@ConditionalOnMissingBean(InternalResourceViewResolver.class)
public InternalResourceViewResolver defaultViewResolver() {

View File

@ -23,6 +23,7 @@ import org.springframework.validation.DefaultMessageCodesResolver;
* {@link ConfigurationProperties properties} for Spring MVC.
*
* @author Phillip Webb
* @author Sébastien Deleuze
* @since 1.1
*/
@ConfigurationProperties("spring.mvc")
@ -49,6 +50,8 @@ public class WebMvcProperties {
*/
private boolean ignoreDefaultModelOnRedirect = true;
private final Async async = new Async();
public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() {
return this.messageCodesResolverFormat;
}
@ -82,4 +85,26 @@ public class WebMvcProperties {
this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
}
public Async getAsync() {
return this.async;
}
public static class Async {
/**
* The amount of time (in milliseconds) before asynchronous request handling times
* out. If this value is not set, the default timeout of the underlying
* implementation is used, e.g. 10 seconds on Tomcat with Servlet 3.
*/
private Long requestTimeout;
public Long getRequestTimeout() {
return this.requestTimeout;
}
public void setRequestTimeout(Long requestTimeout) {
this.requestTimeout = requestTimeout;
}
}
}

View File

@ -385,6 +385,33 @@ public class WebMvcAutoConfigurationTests {
"faviconHandlerMapping"), is(nullValue()));
}
@Test
public void defaultAsyncRequestTimeout() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerAdapter adapter = this.context
.getBean(RequestMappingHandlerAdapter.class);
assertNull(ReflectionTestUtils.getField(adapter, "asyncRequestTimeout"));
}
@Test
public void customAsyncRequestTimeout() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.async.request-timeout:123456");
this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
RequestMappingHandlerAdapter adapter = this.context
.getBean(RequestMappingHandlerAdapter.class);
Object actual = ReflectionTestUtils.getField(adapter, "asyncRequestTimeout");
assertEquals(123456L, actual);
}
@Configuration
protected static class ViewConfig {

View File

@ -120,7 +120,8 @@ content into your application; rather pick only the properties that you need.
spring.mvc.date-format= # set fixed date format, e.g. dd/MM/yyyy
spring.mvc.favicon.enabled=true
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.mvc.ignore-default-model-on-redirect=true # If the the content of the "default" model should be ignored redirects
spring.mvc.ignore-default-model-on-redirect=true # if the the content of the "default" model should be ignored redirects
spring.mvc.async.request-timeout= # async timeout in milliseconds
spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix