使用Spring的@Scheduled实现定时任务时报TaskUtils$LoggingErrorHandler错

2018-01-18
1578
0
0

温馨提示:本文最后更新于 2023-07-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

@Scheduled实现定时任务时报TaskUtils$LoggingErrorHandler错

报错日志:

2018-01-18 17:02:00,026 [pool-1-thread-1] ERROR: org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
  at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)

...

报错原因:

我的aop日志记录方法中注入了Request,导致此报错!
Scheduled是异步线程来处理你的逻辑的,而我们代码中使用了RequestcurrentRequestAttributes方法是需要有requestresponse绑定到当前的线程中,也就是说只有http请求的时候才不会报错。但我们是异步的,肯定没有这个。所以会报错。

解决方法:

更改架构,避免使用到Request


评论

Polar.
最重要的,是拥有跟随内心与直觉的勇气

文章目录