共计 5665 个字符,预计需要花费 15 分钟才能阅读完成。
前言
在应用框架日常开发中须要在 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 void setReadListener(ReadListener readListener) {}};
}
@Override
public String getHeader(String name) {return headMap.get(name.toLowerCase());
}
@Override
public String getParameter(String name) {return requestParamMap.get(name);
}
}
自定义工作执行器
用于拦挡异步工作执行,在工作执前对立进行 Request 共享操作,且能够定义多个,不影响原有的异步工作代码
public class CustomTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
System.out.println("异步工作共享 request");
return () -> {
try {CommonUtil.shareRequest(request);
runnable.run();} finally {CommonUtil.remove();
}
};
}
}
@Configuration
public class TaskExecutorConfig {@Bean()
public Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("taskExecutor-");
executor.setAwaitTerminationSeconds(60);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@Bean("shareTaskExecutor")
public Executor hpTaskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("shareTaskExecutor-");
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(60);
// 减少 TaskDecorator 属性的配置
executor.setTaskDecorator(new CustomTaskDecorator());
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
调用示例
给 @Anysc 注解指定进行共享拦挡的工作执行器即可
@PostMapping("/testAsync")
@ResponseBody
public Object testAsync(@RequestBody Map<String, Object> params) throws Exception{Result result = Result.okResult();
asyncUtil.executeAsync();
return result;
}
@Component
public class AsyncUtil {@Async("shareTaskExecutor")
public void executeAsync () throws InterruptedException {System.out.println("开始执行 executeAsync");
Thread.sleep(3000);
System.out.println("完结执行 executeAsync");
}
}
正文完
发表至: springboot
2022-04-09