Our application connects to a remote server, so we are using async functionality to process the requests more efficiently. That remote request is executed asynchronously too, so when the request to the remote server is send, the controller responds with a DeferredResult.
When the remote response is processed, we try to use a bean with scope request. However, this process is not executed within a web-aware Spring ApplicationContext, To make it works, we have previously stored the RequestAttributes and are established in the new thread just after start processing the remote response. The problem is that as the request was completed and we receive an exception.
So, taking into account that the request has not really been completed, we have created this JIRA to not complete the RequestAttributes until the request has been completely processed. Just adding that if condition makes it work
In the case of Callable, which is handled with a Spring MVC thread we create a new ServletRequestAttributes instance for the duration of the thread (see FrameworkServlet.createRequestBindingInterceptor. In the case of DeferredResult, where the thread is not known to Spring MVC I would recommend also creating a new RequestAttributes instance, which should satisfy the need to use a request-scoped bean:
I do understand the point that the overall request is not completed, but the code has never really worked that way. For example, even before Servlet 3 async processing, each new forward used a separate RequestAttributes instance. Trying to extend a single RequestAttributes over the duration of async processing would require changing that behavior.
The best way to explain the use of RequestAttributes, is that it is scoped to a specific dispatch -- be it a forward, an async dispatch, or a thread exiting to give way to async processing.