今天下午遇见一个很坑爹的问题,当spring使用@Scheduled注解方式来调度定时任务时,会出现如下问题:[https://segmentfault.com/q/1010000003035793]
2020-07-09 16:07:31.814 TaskUtils.java:96 - 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: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[?:5.1.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:312) ~[?:5.1.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:65) ~[?:5.1.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:328) ~[?:5.1.9.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:323) ~[?:5.1.9.RELEASE]
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:301) ~[?:5.1.9.RELEASE]
at com.sun.proxy.$Proxy140.setAttribute(Unknown Source) ~[?:?]
at com.fangzhizun.service.impl.LocalDeploymentInstructionsServiceImpl.auto(LocalDeploymentInstructionsServiceImpl.java:74) ~[?:0.0.1-SNAPSHOT]
at com.fangzhizun.service.impl.LocalDeploymentInstructionsServiceImpl$$FastClassBySpringCGLIB$$2153dc60.invoke(<generated>) ~[?:0.0.1-SNAPSHOT]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[?:5.1.9.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[?:5.1.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[?:5.1.9.RELEASE]
at com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor.invoke(DynamicDataSourceAnnotationInterceptor.java:49) ~[?:2.5.6]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[?:5.1.9.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[?:5.1.9.RELEASE]
at com.fangzhizun.service.impl.LocalDeploymentInstructionsServiceImpl$$EnhancerBySpringCGLIB$$b14d33f4.auto(<generated>) ~[?:0.0.1-SNAPSHOT]
at com.fangzhizun.controller.AutoMationRestController.processingInstructions(AutoMationRestController.java:697) ~[?:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[?:5.1.9.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[?:5.1.9.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_231]
at java.util.concurrent.FutureTask.runAndReset(Unknown Source) ~[?:1.8.0_231]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source) ~[?:1.8.0_231]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[?:1.8.0_231]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[?:1.8.0_231]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[?:1.8.0_231]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
代码中使用到了
Scheduled是异步来处理线程来处理你得逻辑,当使用request时会去调用RequestContextHolder.currentReqestAttribute,这个方法需要有request和response绑定到当前线程,由于使用得是异步线程所以没有这个绑定.
@Resource
protected HttpServletRequest request;
request.setAttribute("co", co);
而错误代码中则是一request.setAttribute("co", co);这一步进行切库时报错,换了一种切库方式,使用@DS("co")传入参数co来切库,这样就不会有报错出现
image.png
以上是令我纠结一天的bug得到解决,切库方式不正确导致我在远地徘徊一天.
网友评论