How can I make sure Provider<MyFacade> returns a new MyFacade in a new async thread?

Viewed 139

I'm trying to make my myFacadeProvider.get() return a thread specific MyFacade. This works whenever I'm in the normal WebApplication RequestContext since it returns the same MyFacade throughout the app. But if I create a new thread and do the myFacadeProvider.get() it will return the same MyFacade referenced in the WebApplication RequestContext instead of a new one for that specific thread. How can I make myFacadeProvider.get() return a thread specific MyFacade no matter what thread I'm in?

Some example code:

@Component
@Scope(value = WebapplicationContext.SCOPE_REQUEST)
public class MyFacade {

  @Setter
  private String work = "the main work";
  
  public String doWork() { 
    return work;
  }
}

@Component
public class QueryES {
   @Autowired
   private Provider<MyFacade> myFacadeProvider;

   public void doThings() {
     // This call gets a MyFacade that is thread safe in this scoped context and works as expected
     MyFacade myFacade = myFacadeProvider.get();
     myFacade.doWork(); // returns `the main work`
     myFacade.setWork("the secondary work");
     myFacade.doWork(); // returns `the secondary work`

     // When I spin up a new thread and do the same call it will give me back the same myFacade 
     // that was returned previously instead of a new thread specific myFacade
     Future<String> future = getFacadeAsyncDoWork();  // returns `the secondary work` instead of `the main work`

   }
   
   public ListenableFuture<String> getFacadeAsyncDoWork() {
     return new AsyncResult<String>(doQueryWork());
   }

   public String doQueryWork() {
     // Returns same myFacade as above, I want this to return a thread specific provider
     MyFacade asyncMyFacade= myFacadeProvider.get();
     return asyncMyFacade.doWork(); // returns `the secondary work` instead of `the main work`
   }
}
1 Answers

You can use CustomScopeConfigurer for register custom scopes. Spring include SimpleThreadScope implementation but not register it by default.

Example:

import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.SimpleThreadScope;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public CustomScopeConfigurer customScopeConfigurer() {
        CustomScopeConfigurer configurer = new CustomScopeConfigurer();
        configurer.addScope("thread", new SimpleThreadScope());
        return configurer;
    }
}
@Component
@Scope(value = "thread")
public class MyFacade {

    private final Thread currentThread = Thread.currentThread();

    public String getThreadName() {
        return currentThread.getName();
    }

}
Related