当前位置:网站首页>千万不要随便把 Request 传递到异步线程里面 , 有坑 你拿捏不住,得用 startAsync 方法才行
千万不要随便把 Request 传递到异步线程里面 , 有坑 你拿捏不住,得用 startAsync 方法才行
2022-07-26 18:08:00 【胡安民】
前沿
前几天在网上冲浪的时候看到一篇技术文章,讲的是他把一个 request 请求传递到了线程池里面,文章还是挺不错的,把发现问题和解决问题都写的很明白了。但是,我觉得写的太复杂的,没人想听你到底怎么找到bug的过程显摆自己牛逼吗?, 直接把问题和原因描述出来不就行了,废了半天的力气看了一大堆没用的东西, 说到这个“坑”我记得我刚刚入行没两年的也遇到过,我已经不记得自己当时是怎么解决的了,但是我肯定也没有深入的去研究。因为那个时候遇到问题,就去网上费尽心思的查,粘一个方案过来看能不能用。 如果不能用的话,心里暗骂一句,然后接着找。直到找到一个可以用的。至于为什么能用?管它呢,研究这玩意干啥
前几天突然又遇到了,又废了一番功夫把问题解决了, 这次我要记录下来以后在遇到就能迎刃而解了…
问题原因

简单描述下:
Java Servlet 设计规范里说了: 每个 request 对象只在 servlet 的服务方法的范围内有效,或者在过滤器的 doFilter 方法的范围内有效。规范里面是建议了容器里面实现 request 的时候尽量复用,而不是回收,目的是节约性能。 程序员朋友们必须要意识到,我不建议在上述范围之外维护 request 的引用,因为它可能会产生不确定的结果。
而我们创建一个线程在线程中使用request 那么就脱离了servlet了, 会导致request 脱离了原本的生命周期从而导致使用完毕后没有执行request内清除资源的动作,导致其他的接口请求复用request 的时候出现了问题,因为还保存上次request的值,那么其他的接口在解析request 的时候就会跳过参数解析,继续复用上次的值, 然后就会导致接口请求失败参数映射不上去,或者请求后的值不对的问题
解决办法

正确使用request传入异步中的方式
@GetMapping(value = "/testRequest")
@SemaphoreDoc(key = "testRequest")
public void testRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
AsyncContext asyncContext = request.startAsync(request, response);
new Thread(()->{
String name = request.getParameter("name");
System.out.println(name);
//手动释放request资源
asyncContext.complete();
}).start();
logger.info("2222");
Thread.sleep(10000);
}

边栏推荐
- Paged query design of scenarios
- How to become an excellent test / development programmer? Focus on planning and then move
- Comparison of advantages and disadvantages between SD NAND and EMMC
- Racher deploys kubernetes cluster
- LeetCode笔记:Weekly Contest 303
- Tensor RT's int8 quantization principle
- 【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引
- EN 1504-6混凝土结构保护和修理用产品钢筋锚固—CE认证
- JS question brushing plan - linked list
- 2022年化工自动化控制仪表考题模拟考试平台操作
猜你喜欢

Sentinel 隔离与降级

Verification palindrome string II of leetcode simple question

Mathematical basis of deep learning
![[postgraduate entrance examination vocabulary training camp] day 14 - Panini, predict, access, apologize, sense, transport, aggregation](/img/fb/0338559494f31db8657f0e4767138b.png)
[postgraduate entrance examination vocabulary training camp] day 14 - Panini, predict, access, apologize, sense, transport, aggregation

分布式事务-seata

MySQL - 多表查询与案例详解

MySQL日志介绍

ReentrantLock学习之---基础方法

Gongfu developer community is settled! On July 30!

我酷故我在
随机推荐
Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release
Mongodb stats counts the space occupied by the collection
模型定义#pytorch学习
Covos: no need to decode! Semi supervised Vos acceleration using motion vectors and residuals of compressed video bitstreams (CVPR 2022)
.Net CLR GC 动态加载短暂堆阈值的计算及阈值超量的计算
Likeshop takeout order system is open source, 100% open source, no encryption
分布式事务-seata
基础模块及算例#pytorch学习
What aspects should be considered in the selection of MES system?
Seata 入门简介
Some time series modeling strategies (I)
【AUTOSAR-RTE】-1-聊一聊RTE(Run-Time Environment)
Tensorflow-GPU 1.15安装
微软默默给 curl 捐赠一万美元,半年后才通知
【考研词汇训练营】Day 14 —— panini,predict,access,apologize,sense,transport,aggregation
Complete MySQL database commands
VPC nat (Sant, nant) experiment
一些时序建模策略(一)
Multi thread learning notes -1.cas
JVM内存模型之Volatile关键字