首页 > 编程语言 >java.lang.IllegalStateException:spring异步方法逻辑错误

java.lang.IllegalStateException:spring异步方法逻辑错误

时间:2022-11-17 10:36:42浏览次数:40  
标签:lang 异步 java 请求 web spring request 线程 方法

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

没有发现线程绑定的请求:您是在引用实际web请求之外的请求属性,还是在最初接收线程之外处理请求?如果你实际上在web请求中操作并且仍然收到这个消息,你的代码可能在DispatcherServlet之外运行:在这种情况下,使用RequestContextListener或RequestContextFilter来公开当前请求。

问题原因:在一个请求调用链中涉及到一些同步方法和异步方法(通过@Async注解实现),在异步方法中会通过HttpServletRequest对象获取请求头数据;但异步方法流程较长,此时这个请求的主线程已经结束,导致在异步方法中出现这个问题。

解决思路:避免在异步线程中操作httpservlet数据,如果异步线程调用链中需要使用此时请求中的信息,可以考虑通过通信技术(方法透传,缓存)将数据传到异步线程中。

标签:lang,异步,java,请求,web,spring,request,线程,方法
From: https://www.cnblogs.com/plumsun/p/16898562.html

相关文章