SpringBoot异步任务获取HttpServletRequest
前言
在使用框架日常开发中需要在controller中进行一些异步操作减少请求时间,但是发现在使用@Anysc注解后会出现Request对象无法获取的情况,本文就此情况给出完整的解决方案
原因分析
- @Anysc注解会开启一个新的线程,主线程的Request和子线程是不共享的,所以获取为null
- 在使用springboot的自定带的线程共享后,代码如下,Request不为null,但是偶发的其中body/head/urlparam内容出现获取不到的情况,是因为异步任务在未执行完毕的情况下,主线程已经返回,拷贝共享的Request对象数据被清空
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//设置子线程共享
RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);
HttpServletRequest request = servletRequestAttributes.getRequest();
解决方案
前置条件
-
启动类添加@EnableAsync注解
-
标记@Async的异步方法不能和调用者在同一个class中
pom配置
<!-- 阿里线程共享 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.11.0</version>
</dependency>
requrest共享
通过TransmittableThreadLocal对象进行线程对象共享
public class CommonUtil {
public static TransmittableThreadLocal<HttpServletRequest> requestTransmittableThreadLocal = new TransmittableThreadLocal<HttpServletRequest>();
public static void shareRequest(HttpServletRequest request){
requestTransmittableThreadLocal.set(request);
}
public static HttpServletRequest getRequest(){
HttpServletRequest request = requestTransmittableThreadLocal.get();
if(request!=null){
return requestTransmittableThreadLocal.get();
}else{
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(requestAttributes!=null){
return requestAttributes.getRequest();
}else{
return null;
}
}
}
public static void remove(){
requestTransmittableThreadLocal.remove();
}
}
注:系统中所有Request获取需要统一从CommonUtil指定来源,例如token鉴权等
自定义request过滤器
通过自定义过滤器对Request的内容进行备份保存,主线程结束时Request清除结束不会影响到子线程的相应参数的获取,也适用于增加拦截器/过滤器后body参数无法重复获取的问题。需要注意的是对header参数处理时key要忽略大小写
public class HttpServletRequestReplacedFilter implements Filter, Ordered {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if (request instanceof HttpServletRequest) {
requestWrapper = new RequestWrapper((HttpServletRequest) request);
}
//获取请求中的流如何,将取出来的字符串,再次转换成流,然后把它放入到新request对象中。
// 在chain.doFiler方法中传递新的request对象
if (requestWrapper == null) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public int getOrder() {
return 10;
}
}
public class RequestWrapper extends HttpServletRequestWrapper{
private final byte[] body;
private final HashMap<String,String> headMap;
private final HashMap<String,String> requestParamMap;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
body = CommonUtil.getBodyString(request).getBytes(Charset.forName("UTF-8"));
headMap = new HashMap();
Enumeration<String> headNameList = request.getHeaderNames();
while (headNameList.hasMoreElements()){
String key = headNameList.nextElement();
headMap.put(key.toLowerCase(),request.getHeader(key));
}
requestParamMap = new HashMap<>();
Enumeration<String> parameterNameList = request.getParameterNames();
while (parameterNameList.hasMoreElements()){
String key = parameterNameList.nextElement();
requestParamMap.put(key,request.getParameter(key));
}
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public