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

@Log4j2
@AllArgsConstructor(onConstructor = @__(@Inject))
public class WitcherDriver {

    private ClientTrafficCalculator clientTrafficCalculator;
    private TaskAllocator taskAllocator;


    @SneakyThrows
    public void startPoller() {

        new Thread(ProxyFleetManager::collectProxyIps).start();
        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 WitcherDriver witcherDriver;


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

            witcherDriver.startPoller();

        }

    @Override
    public void configure() {
        install(new TomcatContainerModule(new ServiceModule(Test)));
    }
}


预期:

witcherDriver.startPoller(); //可以正常工作

当前结果

witcherDriver.startPoller(); //解除null指针异常,因为它没有被注入并且witcherDriver为null

最佳答案

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

@Log4j2
public class WitcherDriver extends AbstractModule {


    @Override
    public void configure() {
        requestInjection(this);
    }

    @Inject
    public void startPoller(ClientTrafficCalculator clientTrafficCalculator,
        TaskAllocator taskAllocator, ProxyFleetManager proxyFleetManager) {

        new Thread(proxyFleetManager::collectProxyIps).start();
        new Thread(clientTrafficCalculator::prepareRateLimiterForAllClients).start();
        new Thread(taskAllocator::processPendingRecordsInDDB).start();
        log.info("All threads are started");

        //ProxyFleetManager::collectProxyIps
    }


}


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

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


谢谢
k

10-06 03:03