我正在Spring mvc服务方法中运行调度程序。
在这里,我尝试在没有httpRequest的情况下从会话中获取数据。
我搜索了一些方法,
我有一些想法添加
org.springframework.web.context.request.RequestContextListener
web.xml
听众。
但它不断在下面给出错误消息:-
ERROR - TaskUtils$LoggingErrorHandler : handleError 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 com.nyngw.common.service.CommonServiceImpl.autoCompute(CommonServiceImpl.java:72)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
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(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
我的网络.xml :-
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <error-page> <exception-type>java.lang.Exception</exception-type> <location>/common/error/exception</location>
</error-page> <error-page> <error-code>500</error-code> <location>/common/error/500</location>
</error-page> <error-page> <error-code>404</error-code> <location>/common/error/404</location>
</error-page> -->
调度程序上下文.xml :-
<task:scheduler id="scheduler" pool-size="100"/>
<task:scheduled-tasks scheduler = "scheduler">
<task:scheduled ref="commonServiceImpl" method="autoCompute" cron = "0/5 * * * * *" />
</task:scheduled-tasks>
我正在尝试获得会话的服务:-
@Service
public class CommonServiceImpl{
public void autoCompute(){
// RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
System.out.println("test1");
if(requestAttributes !=null);{
ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
System.out.println("test2");
if(attributes !=null){
System.out.println("test3");
HttpServletRequest request = attributes.getRequest();
HttpSession httpSession = request.getSession(true);
String auto = (String) httpSession.getAttribute("auto");
if(auto!=null){
System.out.println("schedule log"+auto);
}
}
}
}
}
我每 5 秒在控制台上得到的只有"test1"和"test2"
我检查了会话数据添加是否正确,但仍然无法从服务中获取会话和数据...
需要一些帮助。谢谢!
"RequestContextFilter" 也许这就是你要找的。