我有一个自定义客户端对象,我正在main((方法中实例化它,需要将它传递给我要部署的所有垂直实例。以下是我尝试过的
1.(
private static Connection connection;
.
.
.
//method to initialise client
initialiseClient();
final DeploymentOptions deployOpts = new DeploymentOptions().setInstances(num_instances);
vertx.deployVerticle(new MyVerticle(connection), deployOpts, ar -> {
if (ar.succeeded()) {
startFuture.complete();
} else {
startFuture.fail(ar.cause());
}
});
在这种情况下,我得到了以下错误,因为垂直实例已经创建。
Can't specify > 1 instances for already created verticle
2.(
JsonObject object = new JsonObject().put("connection-object", connection);
final DeploymentOptions deployOpts = new DeploymentOptions().setInstances(num_instances);
vertx.deployVerticle(MyVerticle.class.getName(), deployOpts, ar -> {
if (ar.succeeded()) {
startFuture.complete();
} else {
startFuture.fail(ar.cause());
}
});
在这种情况下,我得到错误
java.lang.IllegalStateException: Illegal type in JsonObject: class com.abc.def.Connection
所以我的问题是,在部署一个verticale的多个实例时,有没有任何方法可以传递自定义类对象?
您可以使用接受Supplier<Verticle>
的deployVerticle
方法(此处为javadocs(。
DeploymentOptions deployOpts = new DeploymentOptions().setInstances(num_instances);
vertx.deployVerticle(() -> { return new MyVerticle(connection) }, deployOpts);