SpringBoot詳細(xì)講解異步任務(wù)如何獲取HttpServletRequest
原因分析
- @Anysc注解會(huì)開(kāi)啟一個(gè)新的線程,主線程的Request和子線程是不共享的,所以獲取為null
- 在使用springboot的自定帶的線程共享后,代碼如下,Request不為null,但是偶發(fā)的其中body/head/urlparam內(nèi)容出現(xiàn)獲取不到的情況,是因?yàn)楫惒饺蝿?wù)在未執(zhí)行完畢的情況下,主線程已經(jīng)返回,拷貝共享的Request對(duì)象數(shù)據(jù)被清空
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); //設(shè)置子線程共享 RequestContextHolder.setRequestAttributes(servletRequestAttributes, true); HttpServletRequest request = servletRequestAttributes.getRequest();
解決方案
前置條件
- 啟動(dòng)類添加@EnableAsync注解
- 標(biāo)記@Async的異步方法不能和調(diào)用者在同一個(gè)class中
pom配置
<!-- 阿里線程共享 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.11.0</version>
</dependency>
requrest共享
通過(guò)TransmittableThreadLocal對(duì)象進(jìn)行線程對(duì)象共享
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();
}
}注:系統(tǒng)中所有Request獲取需要統(tǒng)一從CommonUtil指定來(lái)源,例如token鑒權(quán)等
自定義request過(guò)濾器
通過(guò)自定義過(guò)濾器對(duì)Request的內(nèi)容進(jìn)行備份保存,主線程結(jié)束時(shí)Request清除結(jié)束不會(huì)影響到子線程的相應(yīng)參數(shù)的獲取,也適用于增加攔截器/過(guò)濾器后body參數(shù)無(wú)法重復(fù)獲取的問(wèn)題。需要注意的是對(duì)header參數(shù)處理時(shí)key要忽略大小寫(xiě)
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);
}
//獲取請(qǐng)求中的流如何,將取出來(lái)的字符串,再次轉(zhuǎn)換成流,然后把它放入到新request對(duì)象中。
// 在chain.doFiler方法中傳遞新的request對(duì)象
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);
}
}自定義任務(wù)執(zhí)行器
用于攔截異步任務(wù)執(zhí)行,在任務(wù)執(zhí)前統(tǒng)一進(jìn)行Request共享操作,且可以定義多個(gè),不影響原有的異步任務(wù)代碼
public class CustomTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
System.out.println("異步任務(wù)共享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;
}
}
調(diào)用示例
給@Anysc注解指定進(jìn)行共享攔截的任務(wù)執(zhí)行器即可
@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("開(kāi)始執(zhí)行executeAsync");
Thread.sleep(3000);
System.out.println("結(jié)束執(zhí)行executeAsync");
}
}
到此這篇關(guān)于SpringBoot詳細(xì)講解異步任務(wù)如何獲取HttpServletRequest的文章就介紹到這了,更多相關(guān)SpringBoot獲取HttpServletRequest內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在 Spring Boot 中使用異步線程時(shí)的 HttpServletRequest 復(fù)用問(wèn)題記錄
- SpringBoot異步線程父子線程數(shù)據(jù)傳遞的5種方式
- Spring?Boot異步線程間數(shù)據(jù)傳遞的四種方式
- springboot?正確的在異步線程中使用request的示例代碼
- SpringBoot?異步線程間傳遞上下文方式
- SpringBoot獲取HttpServletRequest的3種方式總結(jié)
- SpringBoot實(shí)現(xiàn)任意位置獲取HttpServletRequest對(duì)象
- Spring Boot 中正確地在異步線程中使用 HttpServletRequest的方法
相關(guān)文章
SpringBoot添加自定義攔截器的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot添加自定義攔截器的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Springboot整合mybatis開(kāi)啟二級(jí)緩存的實(shí)現(xiàn)示例
在一級(jí)緩存中,是查詢兩次數(shù)據(jù)庫(kù)的,顯然這是一種浪費(fèi),既然SQL查詢相同,就沒(méi)有必要再次查庫(kù)了,直接利用緩存數(shù)據(jù)即可,這種思想就是MyBatis二級(jí)緩存的初衷,本文就詳細(xì)的介紹了Springboot整合mybatis開(kāi)啟二級(jí)緩存,感興趣的可以了解一下2022-05-05
淺析javax.servlet.Servlet,ServletContext接口
本篇文章是對(duì)javax.servlet.Servlet,ServletContext接口進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下2013-07-07
連續(xù)調(diào)用多個(gè)外部系統(tǒng)寫(xiě)接口保證數(shù)據(jù)一致性的思路
今天小編就為大家分享一篇關(guān)于連續(xù)調(diào)用多個(gè)外部系統(tǒng)寫(xiě)接口保證數(shù)據(jù)一致性的思路,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
關(guān)于@Scheduled不執(zhí)行的原因分析
這篇文章主要介紹了關(guān)于@Scheduled不執(zhí)行的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
可視化定時(shí)任務(wù)quartz集成解析全過(guò)程
在開(kāi)發(fā)中有很多定時(shí)任務(wù)都不是寫(xiě)死的而是可以人為配置并且寫(xiě)到數(shù)據(jù)庫(kù)中的,下面這篇文章主要給大家介紹了關(guān)于可視化定時(shí)任務(wù)quartz集成解析的相關(guān)資料,需要的朋友可以參考下2022-10-10

