title: Pass request scope data to async methods in CDI
body: <p>Java EE 7 application is running on Wildfly 9.0.2.Final. There is a problem to access request scoped data from within @Asynchronous methods. </p>
  
  <p>In web filter data (e.g. token) is set into RequestScoped CDI bean. Later we want to access this data. Everything works fine if we work in one thread. But if there is need to run code asynchronously the problem appears. CDI injects empty bean and request data is lost. </p>
  
  <p>Here is the example:</p>
  
  <pre><code>@RequestScoped
  public class CurrentUserService implements Serializable {
    public String token;
  }
  
  @Stateless
  public class Service {
     @Inject
     private RestClient client;
  
      @Resource
      private ManagedExecutorService executorService;
  
      @Resource
      private ContextService contextService;
  
      @Asynchronous
      private &lt;T&gt; Future&lt;T&gt; getFuture(Supplier&lt;T&gt; supplier) {
          Callable&lt;T&gt; task = supplier::get;
          Callable&lt;T&gt; callable = contextService.createContextualProxy(task, Callable.class);
          return executorService.submit(callable);
      }
  
     public String getToken() throws Exception {
        return getFuture(client::getToken).get();
     }
  }
  
  @ApplicationScoped
  public class RestClient {
      @Inject
      private CurrentUserService currentUserBean;
  
      public String getToken() {
          return currentUserBean.token;
      }
  }
  </code></pre>
  
  <p>In the given example we want to access current user token (CurrentUserService#token) from asynchronous Service.getToken method. As the result we will receive null.</p>
  
  <p>It's expected that 'request scoped' data should be accessible from tasks executed within request scope. Something like InheritableThreadLocal should be used to allow assess to original thread data from new threads. </p>
  
  <p>Is it a bug? May be I'm doing something wrong? If yes - what it the correct way to propagate such data into async calls?</p>
  
  <p>Thanks in advance.</p>