---
title: "SpringMVC对Servlet3异步请求的支持"
author: "234390216"
date: "2018-07-10T10:46:42Z"
tags: ["Callable", "Servlet", "SpringMVC", "异步请求"]
categories: ["开源软件"]
slug: "springmvc-servlet3"
draft: false
lastmod: "2018-07-10T11:04:29Z"
id: 2426566
url: "https://elim.iteye.com/blog/2426566"
---

# SpringMVC对Servlet3异步请求的支持

SpringMVC对Servlet3异步请求的支持有两种方式，分别是通过处理器方法返回Callable和DeferredResult。按照Servlet3的规范，支持异步请求时需要配置对应的Servlet和Filter支持异步请求，为了使SpringMVC支持异步请求的处理，需要在定义DispatcherServlet时配置其支持异步请求，在DispatcherServlet之前定义的Filter也需要配置支持异步请求。

```
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <!-- 启用异步支持 -->
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
```

## [](https://github.com/elim168/elim168.github.io/blob/master/spring/mvc/14.SpringMVC%E5%AF%B9Servlet3%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E7%9A%84%E6%94%AF%E6%8C%81.md#%E8%BF%94%E5%9B%9Ecallable)返回Callable

当处理器的返回方法是Callable类型时会默认发起异步请求，并使用一个TaskExecutor来调用返回的Callable，之后的处理就跟正常的SpringMVC请求是一样的。Callable的返回结果也跟正常请求SpringMVC的一样，可以返回Model、ModelAndView、String、Object等，也可以结合`@ResponseBody`使用，具体可以参考`CallableMethodReturnValueHandler`的`handleReturnValue()`。

```
    @RequestMapping("/callable")
    public Callable<String> forCallable(Model model) throws Exception {
        return () -> {
            TimeUnit.SECONDS.sleep(1);//睡眠1秒，模仿某些业务操作
            model.addAttribute("a", "aaaaaaa");
            return "async_request_callable";
        };
    }
```

如果需要针对于单个Callable请求指定超时时间，我们可以把Callable用一个WebAsyncTask包裹起来。然后还可以指定超时回调和正常处理完成的回调。

```
    @RequestMapping("/callable/timeout")
    public WebAsyncTask<String> forCallableWithTimeout(Model model) throws Exception {
        long timeout = 5 * 1000L;
        WebAsyncTask<String> asyncTask = new WebAsyncTask<>(timeout, () -> {
            TimeUnit.MILLISECONDS.sleep(timeout + 10);
            model.addAttribute("a", "aaaaaaa");
            return "async_request_callable";
        });
        asyncTask.onTimeout(() -> {
            System.out.println("响应超时回调");
            return "async_request_callable_timeout";
        });
        asyncTask.onCompletion(() -> {
            System.out.println("响应callable调用完成的回调");
        });
        return asyncTask;
    }
```

## [](https://github.com/elim168/elim168.github.io/blob/master/spring/mvc/14.SpringMVC%E5%AF%B9Servlet3%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E7%9A%84%E6%94%AF%E6%8C%81.md#%E8%BF%94%E5%9B%9Edeferredresult)返回DeferredResult

使用DeferredResult的返回结果的编程通常是在处理器方法中创建一个DeferredResult实例，把它保存起来后再进行返回，比如保存到一个队列中，然后在另外的一个线程中会从这个队列中拿到相应的DeferredResult对象进行相应的业务处理后会往DeferredResult中设置对应的返回值。返回了DeferredResult后SpringMVC将创建一个DeferredResultHandler用于监听DeferredResult，一旦DeferredResult中设置了返回值后，DeferredResultHandler就将对返回值进行处理。DeferredResult的处理过程见`DeferredResultMethodReturnValueHandler`的`handleReturnValue()`。

```
@RequestMapping("/deferredresult")
public DeferredResult<String> forDeferredResult() throws Exception {
    DeferredResult<String> result = new DeferredResult<>();
    new Thread(() -> {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        result.setResult("async_request_deferredresult");
    }).start();
    return result;
}
```

对于DeferredResult也是可以单独指定超时时间和超时后的回调的，它的超时时间可以直接通过构造函数传递，单位是毫秒。

```
@RequestMapping("/deferredresult/timeout")
public DeferredResult<String> forDeferredResultWithTimeout() throws Exception {
    DeferredResult<String> result = new DeferredResult<>(10 * 1000);
    new Thread(() -> {
        try {
            TimeUnit.SECONDS.sleep(31);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        result.setResult("async_request_deferredresult");
    }).start();
    
    result.onTimeout(() -> {
        System.out.println("响应超时回调函数");
    });
    
    result.onCompletion(() -> {
        System.out.println("响应完成的回调函数");
    });
    
    return result;
}
```

## [](https://github.com/elim168/elim168.github.io/blob/master/spring/mvc/14.SpringMVC%E5%AF%B9Servlet3%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E7%9A%84%E6%94%AF%E6%8C%81.md#%E9%85%8D%E7%BD%AE)配置

可以通过`<mvc:annotation-driven/>`的子元素`<mvc:async-support/>`来定义处理异步请求默认的超时时间和需要使用的TaskExecutor。如果不指定默认超时时间则默认会使用容器的异步请求超时时间，如果不指定需要使用的TaskExecutor，则默认会使用一个SimpleAsyncTaskExecutor。在下面的配置中我们就配置了默认的超时时间是15秒，且处理异步请求的TaskExecutor是bean容器中名为asyncTaskExecutor的TaskExecutor。

```
<mvc:annotation-driven>
    <mvc:async-support default-timeout="15000" task-executor="asyncTaskExecutor"/>
</mvc:annotation-driven>
```

### [](https://github.com/elim168/elim168.github.io/blob/master/spring/mvc/14.SpringMVC%E5%AF%B9Servlet3%E5%BC%82%E6%AD%A5%E8%AF%B7%E6%B1%82%E7%9A%84%E6%94%AF%E6%8C%81.md#%E6%8B%A6%E6%88%AA%E5%99%A8)拦截器

返回Callable类型的请求可以通过实现`CallableProcessingInterceptor`接口自定义一个拦截器来拦截，也可以通过继承`CallableProcessingInterceptorAdapter`抽象类来定义拦截器，这样就只需要选择自己感兴趣的方法进行实现。`CallableProcessingInterceptor`接口定义如下：

```
public interface CallableProcessingInterceptor {

	static final Object RESULT_NONE = new Object();

	static final Object RESPONSE_HANDLED = new Object();

	/**
	 * Invoked <em>before</em> the start of concurrent handling in the original
	 * thread in which the {@code Callable} is submitted for concurrent handling.
	 *
	 * <p>
	 * This is useful for capturing the state of the current thread just prior to
	 * invoking the {@link Callable}. Once the state is captured, it can then be
	 * transferred to the new {@link Thread} in
	 * {@link #preProcess(NativeWebRequest, Callable)}. Capturing the state of
	 * Spring Security's SecurityContextHolder and migrating it to the new Thread
	 * is a concrete example of where this is useful.
	 * </p>
	 *
	 * @param request the current request
	 * @param task the task for the current async request
	 * @throws Exception in case of errors
	 */
	<T> void  beforeConcurrentHandling(NativeWebRequest request, Callable<T> task) throws Exception;

	/**
	 * Invoked <em>after</em> the start of concurrent handling in the async
	 * thread in which the {@code Callable} is executed and <em>before</em> the
	 * actual invocation of the {@code Callable}.
	 *
	 * @param request the current request
	 * @param task the task for the current async request
	 * @throws Exception in case of errors
	 */
	<T> void preProcess(NativeWebRequest request, Callable<T> task) throws Exception;

	/**
	 * Invoked <em>after</em> the {@code Callable} has produced a result in the
	 * async thread in which the {@code Callable} is executed. This method may
	 * be invoked later than {@code afterTimeout} or {@code afterCompletion}
	 * depending on when the {@code Callable} finishes processing.
	 *
	 * @param request the current request
	 * @param task the task for the current async request
	 * @param concurrentResult the result of concurrent processing, which could
	 * be a {@link Throwable} if the {@code Callable} raised an exception
	 * @throws Exception in case of errors
	 */
	<T> void postProcess(NativeWebRequest request, Callable<T> task, Object concurrentResult) throws Exception;

	/**
	 * Invoked from a container thread when the async request times out before
	 * the {@code Callable} task completes. Implementations may return a value,
	 * including an {@link Exception}, to use instead of the value the
	 * {@link Callable} did not return in time.
	 *
	 * @param request the current request
	 * @param task the task for the current async request
	 * @return a concurrent result value; if the value is anything other than
	 * {@link #RESULT_NONE} or {@link #RESPONSE_HANDLED}, concurrent processing
	 * is resumed and subsequent interceptors are not invoked
	 * @throws Exception in case of errors
	 */
	<T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception;

	/**
	 * Invoked from a container thread when async processing completes for any
	 * reason including timeout or network error.
	 *
	 * @param request the current request
	 * @param task the task for the current async request
	 * @throws Exception in case of errors
	 */
	<T> void afterCompletion(NativeWebRequest request, Callable<T> task) throws Exception;

}
```

它的配置是通过`<mvc:callable-interceptors/>`配置的。

```
<mvc:annotation-driven>
    <mvc:async-support default-timeout="15000" task-executor="asyncTaskExecutor">
        <mvc:callable-interceptors>
            <bean class="YourCallableProcessingInterceptor"/>
        </mvc:callable-interceptors>
    </mvc:async-support>
</mvc:annotation-driven>
```

返回DeferredResult的也可以进行拦截，这需要我们实现`DeferredResultProcessingInterceptor`接口或者继承自`DeferredResultProcessingInterceptorAdapter`。`DeferredResultProcessingInterceptor`接口定义如下：

```
public interface DeferredResultProcessingInterceptor {

	/**
	 * Invoked immediately before the start of concurrent handling, in the same
	 * thread that started it. This method may be used to capture state just prior
	 * to the start of concurrent processing with the given {@code DeferredResult}.
	 *
	 * @param request the current request
	 * @param deferredResult the DeferredResult for the current request
	 * @throws Exception in case of errors
	 */
	<T> void  beforeConcurrentHandling(NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception;

	/**
	 * Invoked immediately after the start of concurrent handling, in the same
	 * thread that started it. This method may be used to detect the start of
	 * concurrent processing with the given {@code DeferredResult}.
	 *
	 * <p>The {@code DeferredResult} may have already been set, for example at
	 * the time of its creation or by another thread.
	 *
	 * @param request the current request
	 * @param deferredResult the DeferredResult for the current request
	 * @throws Exception in case of errors
	 */
	<T> void preProcess(NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception;

	/**
	 * Invoked after a {@code DeferredResult} has been set, via
	 * {@link DeferredResult#setResult(Object)} or
	 * {@link DeferredResult#setErrorResult(Object)}, and is also ready to
	 * handle the concurrent result.
	 *
	 * <p>This method may also be invoked after a timeout when the
	 * {@code DeferredResult} was created with a constructor accepting a default
	 * timeout result.
	 *
	 * @param request the current request
	 * @param deferredResult the DeferredResult for the current request
	 * @param concurrentResult the result to which the {@code DeferredResult}
	 * @throws Exception in case of errors
	 */
	<T> void postProcess(NativeWebRequest request, DeferredResult<T> deferredResult, Object concurrentResult) throws Exception;

	/**
	 * Invoked from a container thread when an async request times out before
	 * the {@code DeferredResult} has been set. Implementations may invoke
	 * {@link DeferredResult#setResult(Object) setResult} or
	 * {@link DeferredResult#setErrorResult(Object) setErrorResult} to resume processing.
	 *
	 * @param request the current request
	 * @param deferredResult the DeferredResult for the current request; if the
	 * {@code DeferredResult} is set, then concurrent processing is resumed and
	 * subsequent interceptors are not invoked
	 * @return {@code true} if processing should continue, or {@code false} if
	 * other interceptors should not be invoked
	 * @throws Exception in case of errors
	 */
	<T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception;

	/**
	 * Invoked from a container thread when an async request completed for any
	 * reason including timeout and network error. This method is useful for
	 * detecting that a {@code DeferredResult} instance is no longer usable.
	 *
	 * @param request the current request
	 * @param deferredResult the DeferredResult for the current request
	 * @throws Exception in case of errors
	 */
	<T> void afterCompletion(NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception;

}
```

自定义的DeferredResultProcessingInterceptor是通过`<mvc:deferred-result-interceptors>`配置的。

```
<mvc:annotation-driven>
    <mvc:async-support default-timeout="15000" task-executor="asyncTaskExecutor">
        <mvc:deferred-result-interceptors>
            <bean class="YourDeferredResultProcessingInterceptor"/>
        </mvc:deferred-result-interceptors>
    </mvc:async-support>
</mvc:annotation-driven>
```

> 当发起异步请求时，SpringMVC传统的HandlerInterceptor的postHandle()和afterCompletion()不会执行，但是等异步请求结束后它们还是会执行的。如果需要在异步处理完成之后做一些事情，也可以选择实现AsyncHandlerInterceptor接口的afterConcurrentHandlingStarted()，AsyncHandlerInterceptor接口继承了HandlerInterceptor。

（注：本文是基于Spring4.1.0所写）