For XXFor XX

作为一个高手
我习惯了孤独

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

@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

转载请注明出处:For XX » 使用Spring的@Scheduled实现定时任务时报TaskUtils$LoggingErrorHandler错
标签:speing Scheduled 报错解决
分享到: 更多