java - 使用 vertx embedded 和 Guice 为 Verticles 设置多个实例

标签 java guice inject vert.x

我在同一台机器上使用 Vertx 集群(开发模式)。 我也在使用 guice 注入(inject)一些 pojo。

但是,当我尝试增加 Verticle 实例时,我得到:

java.lang.IllegalArgumentException: Can't specify > 1 instances for already created verticle
    at io.vertx.core.impl.DeploymentManager.deployVerticle(DeploymentManager.java:70)
    at io.vertx.core.impl.VertxImpl.deployVerticle(VertxImpl.java:516)
    at io.vertx.core.impl.VertxImpl.deployVerticle(VertxImpl.java:511)
    at com.mycompany.world_map_service.web.starter.StarterVerticle.lambda$main$0(StarterVerticle.java:39)

我是这样配置的:

 public static void main(String[] args) throws Exception {

        final Logger logger = Logger.getLogger(StarterVerticle.class);


        ClusterManager mgr = new HazelcastClusterManager();
        VertxOptions options = new VertxOptions().setClusterManager(mgr);
        Vertx.clusteredVertx(options, res -> {
            DeploymentOptions deploymentOptions = new DeploymentOptions().setConfig(config);
            if (res.succeeded()) {
                Vertx vertx = res.result();
                //  Injector injector = Guice.createInjector(new AppInjector(vertx));
                Injector injector = Guice.createInjector(new AppInjector(vertx, deploymentOptions));
                vertx.deployVerticle(injector.getInstance(VertxHttpServerVerticle.class), deploymentOptions.setInstances(3));
                ...
}

那是我的 AppInjector 类:

public class AppInjector extends AbstractModule {

    private Vertx vertx = null;
    private Context context = null;
    DeploymentOptions deploymentOptions = null;


    public AppInjector(Vertx vertx, DeploymentOptions deploymentOptions) {
        this.vertx = vertx;
        this.context = vertx.getOrCreateContext();
        this.deploymentOptions = deploymentOptions;
    }


    @Override
    protected void configure() {
        bind(LocationService.class).to(LocationServiceImpl.class).in(Singleton.class);
        bind(LocationServiceDAO.class).to(LocationServiceDaoImpl.class).in(Singleton.class);
        bind(RedisRepo.class).toProvider(() -> {
            return new RedisRepo(deploymentOptions.getConfig());
        });
        bind(Neo4jRepo.class).toProvider(() -> {
            return new Neo4jRepo(deploymentOptions.getConfig());
        });
    }
}

知道为什么我会发生碰撞吗? 我知道我应该按名称使用:com.mycompany.world_map_service.web.http.VertxHttpServerVerticle 但如果我注入(inject)依赖项,它们将为每个实例复制,不是吗?

最佳答案

我知道我在这里有点晚了,但对于可能在路上发现这个问题的人来说,这是“为什么”。按照错误信息:

Can't specify > 1 instances for already created verticle

您正在使用 Guice 创建一个 Verticle 实例,然后尝试部署 3 个实例,但您已经创建了一个 Verticle 实例:

vertx.deployVerticle(injector.getInstance(VertxHttpServerVerticle.class), deploymentOptions.setInstances(3));

这就是为什么要传递类名的原因。你可以在io.vertx.core.impl.DeploymentManager中看到流程这也解释了上述错误。

I know that I should use by name: "com.mycompany.world_map_service.web.http.VertxHttpServerVerticle" but than if I inject dependencies they going to be duplicated for each instance wont they?

我不确定你的意思是什么。如果您出于某种原因不想复制数据/依赖项,也许您可​​以涉及另一个对象或单例,或利用 Vertx's Shared Data

关于java - 使用 vertx embedded 和 Guice 为 Verticles 设置多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32847345/

相关文章:

java - 如何在Google-Guice中实现动态绑定(bind)?

java - Spring框架中的属性注入(inject)

java - Google Collections API 是否具有与 Ruby Enumerable#inject 方法等效的方法?

java - 我应该如何使用异步通信实现 GET 端点?

java - 是否有任何模式或设计模式可以减少 Activity 或 fragment 类的代码? - 安卓

java - Guice 在不使用@Singleton 的情况下将单个实例注入(inject)多个对象

吉斯 : How to bind classes that are dynamically obtained by an already binded object?

javascript - MobX,有没有办法在与相关组件相同的文件中定义域存储?

java - 为 Java 音频应用程序调优 GC

java - Oracle表中如何存储特殊字符?