что такое проблемам в моем коде?(Ошибка - IllegalStateException) - PullRequest
0 голосов
/ 26 декабря 2018

как я могу использовать сессию?Я использую в «@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)
...