I have to trigger TestDriver.startPoller() from EnvironmentModule since EnvironmentModule is extending from Abstractmodule I am not sure how to inject dependency inside EnvironmentModule is it even possible ?. If not how do I trigger TestDriver.startPoller() from EnvironmentModule
@Log4j2 @AllArgsConstructor(onConstructor = @__(@Inject)) public class TestDriver { private ClientTrafficCalculator clientTrafficCalculator; private TaskAllocator taskAllocator; @SneakyThrows public void startPoller() { new Thread(clientTrafficCalculator::prepareRateLimiterForAllClients).start(); new Thread(taskAllocator::processPendingRecordsInDDB).start(); log.info("All threads are started"); } }
Environment module
@Log4j2 @AllArgsConstructor(onConstructor = @__(@Inject)) public class EnvironmentModule extends AbstractModule { private TestDriver testDriver; @SneakyThrows public EnvironmentModule(final String[] args) { marketplace = System.getProperty("realm", "USTest"); final String realm = getPropOrDefault("realm", "USTest"); final String root = getPropOrDefault("root", "."); AppConfig.initialize(SERVICE_NAME, null, appConfigArgs); TestDriver.startPoller(); } @Override public void configure() { install(new TomcatContainerModule(new ServiceModule(Test))); } }
Expected :
TestDriver.startPoller();
// this works fine
Current result
TestDriver.startPoller();
// thorwing null pointer exception because it is not getting injected and TestDriver is null
Advertisement
Answer
I guess I found an answer we can do something as below
@Log4j2 public class TestDriver extends AbstractModule { @Override public void configure() { requestInjection(this); } @Inject public void startPoller(ClientTrafficCalculator clientTrafficCalculator, TaskAllocator taskAllocator, PManager pManager) { new Thread(pManager::name).start(); } }
And we have to install that, installing will trigger that method
install(new TestDriver()); //trigger pollers for proxy and task allocation
Thanks Jk