1

I have a web application, in which i am using ScheduledThreadPoolExecutor to schedule some logic to be executed later (maybe after original web request is over). Is it possible to access/use HttpServletRequest of original request inside child thread (created via "schedule" call on ScheduledThreadPoolExecutor)?

I tried autowiring HttpServletRequest inside class whose function is running in child thread, but got following error (which i understand the reason for)

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.

Is there any possible way? Can RequestContextFilter be possibly used?

Danielson
  • 2,605
  • 2
  • 28
  • 51
Gaurav Singh
  • 287
  • 6
  • 19
  • 3
    No. The request object, and the request-scoped spring beans, are not supposed to be used after the request has been handled, or even in a separate thread. Extract the data you need for the scheduled job, then submit a task using this data to the executor. – JB Nizet Sep 26 '14 at 06:38
  • can [BeanUtils] (http://grepcode.com/file/repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.8.0/org/apache/commons/beanutils/BeanUtils.java) be used to copy HttpServletRequest object? – Gaurav Singh Sep 26 '14 at 07:05
  • 2
    Why would you need an HttpServletRequest object in the scheduled job? Extract the data needed by the scheduled job to an object, and pass that object to the job. – JB Nizet Sep 26 '14 at 07:12

1 Answers1

0

Seems like what you are trying to accomplish is to have the HTTP Connection kept open while the processing of the request is handled asynchronously in a separate thread?

In this case you might want to take a look at the DefferedResult support in Spring MVC - http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/context/request/async/DeferredResult.html - also explained here - http://java.dzone.com/articles/long-polling-spring-32s

ab2000
  • 364
  • 2
  • 8