0

我必须从EnvironmentModule触发TestDriver.startPoller()因为 EnvironmentModule 是从 Abstractmodule 扩展的我不确定如何在EnvironmentModule中注入依赖关系是否可能?如果不是,我如何 从EnvironmentModule触发TestDriver.startPoller()

@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");

    }

}

环境模块

    @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)));
    }
}

预期的 :

TestDriver.startPoller();// 这工作正常

当前结果

TestDriver.startPoller();// thorwing 空指针异常,因为它没有被注入并且TestDriver为空

4

1 回答 1

0

我想我找到了答案,我们可以做以下事情

@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();
      
    }


}

我们必须安装它,安装将触发该方法

install(new TestDriver()); //trigger pollers for proxy and task allocation

谢谢Jk

于 2019-07-01T14:40:59.580 回答