SpringMVC 源码之异步处理

SpringMVC 如何实现实现异步处理

原创文章

在 SpringMVC 中为了方便使用异步请求专门提供了 AsyncWebRequest 类型的 request,并且提供了处理异步请求的管理器 WebAsyncManager 和工具 WebAsyncUtils。

AsyncWebRequest:专门来处理异步请求的,会将原来的 HttpServletRequest 类型做一层封装

WebAsyncManager:SpringMVC 异步请求处理过程中最核心的类型,管理着整个异步处理的过程

WebAsyncUtils:异步工具类,用来获取 WebAsyncManager 以及创建 AsyncWebRequest 对象

在 SpringMVC 中,对异步请求的处理提供了四个地方的支持:

(1)FrameworkServlet 中给当前请求的 WebAsyncManager 添加了 CallableProcessingInterceptor 类型的拦截器->RequestBindingInterceptor,
目的是为了在请求处理前将当前请求的 LocaleContext 和 ServletRequestAttributes 设置到 LocaleContextHolder 和 RequestContextHolder 中,
并在请求处理完成后恢复,添加过程在 FrameworkServlet#processRequest 方法中:

1
2
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());

(2)RequestMappingHandlerAdapter#invokeHandlerMethod 方法中提供了对异步请求的核心支持,其中做了时间跟异步处理相关的事情:

  • 创建 AsyncWebRequest,并设置了超时时间
  • 对当前请求的 WebAsyncManager 设置四个属性
  • 如果当前请求是异步请求而且已经处理出了结果,则将异步结果与之前保存到 WebAsyncManager 里的 ModelAndViewContainer 取出来,并将 WebAsyncManager 异步执行结果取出,然后调用 ServletInvocableHandlerMethod#wrapConcurrentResult 方法创建 ConcurrentResultHandlerMethod 子类来替换 ServletInvocableHandlerMethod,创建出来的 ConcurrentResultHandlerMethod 并不执行请求,主要是判断异步处理的结果是不是异常类型,如果是则抛出,如果不是则使用返回值处理器 HandlerMethodReturnValueHandler 对其进行解析并将处理后的结果返回
  • 如果 requestMappingMethod#invokeAndHandler 方法执行完成后检查到当前请求已经启动了异步处理,则会直接返回 null
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 创建 AsyncWebRequest 异步请求对象
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
asyncWebRequest.setTimeout(this.asyncRequestTimeout);

// 创建 WebAsyncManager 异步请求管理器对象
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.setTaskExecutor(this.taskExecutor);
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.registerCallableInterceptors(this.callableInterceptors);
asyncManager.registerDeferredResultInterceptors(this.deferredResultInterceptors);
// 如果当前异步请求已经处理并得到结果,则将返回的结果放到 mavContainer 对象中,然后将 invocable 对象进行包装转换,转成需要的执行对象然后开始执行
if (asyncManager.hasConcurrentResult()) {
Object result = asyncManager.getConcurrentResult();
mavContainer = (ModelAndViewContainer) asyncManager.getConcurrentResultContext()[0];
asyncManager.clearConcurrentResult();
LogFormatUtils.traceDebug(logger, traceOn -> {
String formatted = LogFormatUtils.formatValue(result, !traceOn);
return "Resume with async result [" + formatted + "]";
});
// 转换具体的invocable执行对象
invocableMethod = invocableMethod.wrapConcurrentResult(result);
}
// 执行调用
invocableMethod.invokeAndHandle(webRequest, mavContainer);
if (asyncManager.isConcurrentHandlingStarted()) {
return null;
}

(3)返回值处理器:一个有四个处理异步请求的返回值处理器,分别是 AsyncTaskMethodReturnValueHandler、CallableMethodReturnValueHandler、DeferredResultMethodReturnValueHandler、ListenableFutureReturnValueHandler,每一种对应一种类型的返回值,主要作用是使用 WebAsyncManager 启动异步处理.

(4)在 DispatcherServlet#doDispatcher 方法中,当 HandlerAdpater 使用 handler 处理完请求时,会检查是否已经启动了异步处理器,如果启动了则不再往下处理,直接返回。

1
2
3
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}

注意处理流程如下:首先在处理器中返回需要启动异步处理的类型时,相应的返回值处理器会调用 WebAsyncManager 相关方法启动异步处理,
然后再 DispatcherServlet 中将原来请求直接返回,当异步处理完成后会重新发出一个相同的请求,这个是在 RequestMappingAdapter 中会使用特殊的 ServletInvocableHandlerMethod 来处理请求,处理方法是:如果是异步处理返回的结果是异常类型则抛出异常,否则直接返回异常处理结果,然后使用返回值处理器对其处理,接着返回 DispatcherServlet 中按正常流程往下处理。

WebAsyncTask和Callable类型异步请求的处理过程及方法

当处理器方法返回WebAsyncTask或者Callable类型时将自动启用异步处理,使用WebAsyncTask类型返回值的处理器AsyncTaskMethodReturnValueHandler,如果返回值为null,就会给mavContainer设置为请求已处理,然后返回,如果返回值不为空,调用WebAsyncManager的startCallableProcessing方法处理请求

1
2
3
4
5
6
7
8
9
10
11
<servlet>
<servlet-name>mvc-test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--SpringMVC配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@ResponseBody
@RequestMapping(value = "/web_async_task",produces = "text/plain; charset=UTF-8")
public WebAsyncTask<String> webAsyncTask(){
System.out.println("WebAsyncTask处理器主线程进入");
WebAsyncTask<String> task = new WebAsyncTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(5*1000L);
System.out.println("WebAsyncTask处理执行中。。。");
return "久等了";
}
});
System.out.println("WebAsyncTask处理器主线程退出");
return task;
}
@ResponseBody
@RequestMapping(value = "/callable",produces = "text/plain; charset=UTF-8")
public Callable<String> callable(){
System.out.println("Callable处理器主线程进入");
Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(5 * 1000L);
System.out.println("Callable处理执行中。。。");
return "久等了";
}
};
System.out.println("Callable处理器主线程退出");
return callable;
}