我的代码中有什么问题? (错误为IllegalStateException)

时间:2018-12-26 07:50:09

标签: java servlets request scheduler illegalstateexception

如何使用会话?我在'@Scheduled'中使用。但是“ HttpServletRequest”中存在问题,如何使用?

@Controller
public class SchedulerController {

    private static final Logger logger = LoggerFactory.getLogger(SchedulerController.class);

     @Resource private SchedulerYoutubeSearchService schedulerYoutubeSearchService;
     @Autowired(required=true)
     private HttpServletRequest request;

     private static int read = 0;

     @Scheduled(cron = "0/30 * * * * ?")
     protected void testScheduler() {
         UserInfoDto udto = (UserInfoDto) request.getSession().getAttribute("UserInfoDto"); 
         System.out.println(udto);
         try {
             if(udto !=null) {
                schedulerYoutubeSearchService.searchYoutube(udto, read);
                read += 1;
             }
        } catch (Exception e) {
            logger.error("SchedulerController.testScheduler error : ",e);
        } 
     }
   }  

例外:

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)
    at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:309)
    at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:64)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:325)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:320)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:307)
    at com.sun.proxy.$Proxy22.getSession(Unknown Source)
    at datafactory.factory.scheduler.web.SchedulerController.testScheduler(SchedulerController.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案