首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用Spring的@Scheduled实现定时任务时报TaskUtils$LoggingErrorHandler错

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

作者头像
4xx.me
发布2022-06-09 19:05:42
发布2022-06-09 19:05:42
3.3K0
举报

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

报错日志:

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 报错日志:
  • 报错原因:
  • 解决方法:
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档