2

I'm running scheduler in spring mvc service method.

Here I'm trying to get data from session without httpRequest.

I searched some ways to do so,

and i got some ideas adding

org.springframework.web.context.request.RequestContextListener

to web.xml listener.

but it keeps giving error message below:-

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)

my web.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> -->

scheduler-context.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 that I'm trying to get session :-

@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);
                }
            }
        }
    }
}

what I get on console every 5 seconds are only "test1" and "test2"

I checked session data adding correctly but still can't get the session and the data from the service...

need some help. thanks!

ahmetcetin
  • 2,621
  • 1
  • 22
  • 38
sy choi
  • 43
  • 1
  • 6

1 Answers1

-1

"RequestContextFilter" Maybe that's what you're looking for.

Lion.Z
  • 11
  • 2
  • Please Explain more. – Farhad Jul 25 '17 at 06:33
  • you mean use not "RequestContextListener" but "RequestContextFilter"?? – sy choi Jul 25 '17 at 06:38
  • The RequestContextFilter implements the ServletRequestListener listener interface, which listens for HTTP request events, and each request received by the Web server notifies the listener. By configuring RequestContextFilter, the Spring container is more closely integrated with the Web container. – Lion.Z Jul 27 '17 at 02:08
  • ContextLoaderListener implements the ServletContextListener listener interface, while the ServletContextListener is only responsible for monitoring the startup and shutdown events of the Web container. – Lion.Z Jul 27 '17 at 02:09