Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add custom timeout handler for async requests

  • Loading branch information...
commit 6d9394eab0c3bfb5306224750e2b9846feab97db 1 parent add76ac
@rstoyanchev rstoyanchev authored
View
41 src/main/java/org/springframework/samples/mvc/async/CustomTimeoutHandlerInterceptor.java
@@ -0,0 +1,41 @@
+package org.springframework.samples.mvc.async;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.context.request.async.WebAsyncManager;
+import org.springframework.web.context.request.async.WebAsyncUtils;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+public class CustomTimeoutHandlerInterceptor extends HandlerInterceptorAdapter {
+
+ private static Logger logger = LoggerFactory.getLogger(CustomTimeoutHandlerInterceptor.class);
+
+ @Override
+ public boolean preHandle(HttpServletRequest request,
+ final HttpServletResponse response, final Object handler) throws Exception {
+
+ WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
+ asyncManager.setTimeoutHandler(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ logger.debug("Custom time out handler called");
+ response.sendError(503, "Async execution of [" + handler + "] did not complete on time");
+ }
+ catch (IOException e) {
+ logger.error("Timeout not handled", e);
+ }
+ }
+ });
+
+ logger.debug("Registered custom timeout handler");
+
+ return true;
+ }
+
+}
View
7 src/main/webapp/WEB-INF/classes/log4j.xml → src/main/resources/log4j.xml
@@ -10,12 +10,10 @@
</layout>
</appender>
- <!-- Application Loggers -->
- <logger name="org.springframework.samples.mvc">
- <level value="info" />
+ <logger name="org.springframework.samples">
+ <level value="debug" />
</logger>
- <!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
@@ -36,7 +34,6 @@
<level value="debug" />
</logger>
- <!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
View
4 src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
@@ -20,6 +20,10 @@
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<resources mapping="/resources/**" location="/resources/" />
+ <interceptors>
+ <beans:bean class="org.springframework.samples.mvc.async.CustomTimeoutHandlerInterceptor" />
+ </interceptors>
+
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
View
58 src/test/java/org/springframework/samples/mvc/async/DeferredResultControllerTests.java
@@ -0,0 +1,58 @@
+package org.springframework.samples.mvc.async;
+
+import static org.hamcrest.Matchers.instanceOf;
+import static org.junit.Assert.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.samples.mvc.AbstractContextControllerTests;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.web.servlet.ModelAndView;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+public class DeferredResultControllerTests extends AbstractContextControllerTests {
+
+ private MockMvc mockMvc;
+
+ @Before
+ public void setup() throws Exception {
+ this.mockMvc = webAppContextSetup(this.wac).build();
+ }
+
+ @Test
+ public void responseBody() throws Exception {
+ this.mockMvc.perform(get("/async/deferred-result/response-body"))
+ .andExpect(status().isOk())
+ .andExpect(request().asyncStarted())
+ .andExpect(request().asyncResult("Deferred result"));
+ }
+
+ @Test
+ public void view() throws Exception {
+
+ MvcResult mvcResult = this.mockMvc.perform(get("/async/deferred-result/model-and-view"))
+ .andExpect(status().isOk())
+ .andExpect(request().asyncStarted())
+ .andExpect(request().asyncResult(instanceOf(ModelAndView.class)))
+ .andReturn();
+
+ ModelAndView mav = (ModelAndView) mvcResult.getAsyncResult();
+ assertEquals("views/html", mav.getViewName());
+ }
+
+ @Test
+ public void exception() throws Exception {
+ this.mockMvc.perform(get("/async/deferred-result/exception"))
+ .andExpect(status().isOk())
+ .andExpect(request().asyncStarted())
+ .andExpect(request().asyncResult(instanceOf(IllegalStateException.class)));
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.