How to do HK2 injection for child classes?

33 Views Asked by At

I am just moving from C++ to Java and this hk2 injection is kind of hard to get a hang of. I am trying to inject a variable to multiple child classes and it looks like it is not injecting the variable properly. How to do this HK2 injection for a variable used in children of abstract class?

public abstract class ClientFactory<C extends BaseClient> implements Factory<C> {

    public static final String NAME = "executor"

    @Named(ClientFactory.NAME)
    protected final ScheduledExecutorService executor;

    @Inject
    public ClientFactory(ScheduledExecutorService executorService){
       this.executor = executorService;
    }
}
 
public class TestClientFactory extends ClientFactory<TestClient> {
    
   @Inject
   public TestClientFactory(ScheduledExecutorService executorService){
       super(executorService);
   }

   @Override
   public TestClient provide(){
        var eventLoopGroup = new NioEventLoopGroup(16, executor); //This is not getting injected properly.
        /* If I replace the line above with var eventLoopGroup 
            = new NioEventLoopGroup(16, Executors.newScheduledThreadPool(16)
           everything works correctly */
        var httpClient = asyncHttpClient(config().setEventLoopGroup(eventLoopGroup)); 
        //and more
   }
   ///////

   public class TestClientFactory2 extends ClientFactory<TestClient2> {
    
   @Inject
   public TestClientFactory2(ScheduledExecutorService executorService){
       super(executorService);
   }

   @Override
   public TestClient2 provide(){
        var eventLoopGroup = new NioEventLoopGroup(16, executor);
        var httpClient = asyncHttpClient(config().setEventLoopGroup(eventLoopGroup)); 
        //and more
   }

And the bindings

var executorService = Executors.newScheduledThreadPool(32); //How to make this available/injectable for all child classes?

bind(executorService).to(ScheduledExecutorService.class).named(ClientFactory.NAME);
bindFactory(TestClientFactory.class, Singleton.class).to(TestClient.class).in(Singleton.class)
0

There are 0 best solutions below