title: Scope 'session' is not active for the current thread while accessing Spring session bean inside RxJava thread
body: <p>I'm getting an exception while trying to access a Spring session scoped bean inside a thread of rxjava Schedulers.io()</p>
  
  <pre><code>Caused by: 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.
  </code></pre>
  
  <p>Here is my scoped bean</p>
  
  <pre><code>@Component
  @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
  @Getter
  @Setter
  public class SearchSession {
      List&lt;String&gt; results;
  }
  </code></pre>
  
  <p>And my controller</p>
  
  <pre><code>@Controller
  @AllArgsConstructor(onConstructor=@__(@Autowired))
  @Slf4j
  public class SearchController {
      private SearchService searchService;
      private SearchSession searchSession;
  
      @GetMapping("/search")
      public DeferredResult&lt;ModelAndView&gt; getResults() {
          DeferredResult&lt;ModelAndView&gt; deferredResult = new DeferredResult&lt;&gt;();
  
          searchService.search("param1", "param2")
            .map(results -&gt; {
                searchSession.setResults(results);
                return results;
            )
            .subscribeOn(Schedulers.io())
            .subscribe(
                results -&gt; {
                    ModelAndView view = new ModelAndView("search");
                    view.addObject("results", results);
                    deferredResult.setResult(view);
            );
          return deferredResult;
      }
  }
  </code></pre>
  
  <p>I tried to define a class which extends RequestContextListener and set parameter inheritable to true when calling RequestContextHolder.setRequestAttributes to allow inheritance between the thread which initially handle the request and the RxJava thread but it didn't work.</p>
  
  <pre><code>public class InheritableRequestContextListener extends RequestContextListener {
  private static final String REQUEST_ATTRIBUTES_ATTRIBUTE =
      InheritableRequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";
  
      @Override
      public void requestInitialized(ServletRequestEvent requestEvent) {
          if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
              throw new IllegalArgumentException(
                  "Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
          }
          HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
          ServletRequestAttributes attributes = new ServletRequestAttributes(request);
          request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
          LocaleContextHolder.setLocale(request.getLocale());
          RequestContextHolder.setRequestAttributes(attributes, true);
      }
  }
  </code></pre>
  
  <p>I also tried to create a custom RxJava scheduler managed by Spring but it didn't work.</p>
  
  <pre><code>@Bean
  public Scheduler scheduler() {
      final ThreadFactory threadFactory = new ThreadFactoryBuilder()
              .setNameFormat("SearchThread-%d")
              .setDaemon(true)
              .build();
      return Schedulers.from(Executors.newFixedThreadPool(10, threadFactory));
  }
  </code></pre>
  
  <p>Do you have any idea on how could I access my Spring session scoped bean inside RxJava thread ?</p>