出于测试目的,我有以下内容:
public class UserVerticle extends AbstractVerticle {
private static final Logger log = LoggerFactory.getLogger(UserVerticle.class);
@Override
public void start(Future<Void> sf) {
log.info("start()");
JsonObject cnf = config();
log.info("start.config={}", cnf.toString());
sf.complete();
}
@Override
public void stop(Future<Void> sf) {
log.info("stop()");
sf.complete();
}
private void onMessage(Message<JsonObject> message) { ... }
log.info("onMessage(message={})", message);
}
}
是通过
从主顶点部署的 vertx.deployVerticle("org.buguigny.cluster.UserVerticle",
new DeploymentOptions()
.setInstances(1)
.setConfig(new JsonObject()
.put(some_key, some_data)
),
ar -> {
if(ar.succeeded()) {
log.info("UserVerticle(uname={}, addr={}) deployed", uname, addr);
// continue when OK
}
else {
log.error("Could not deploy UserVerticle(uname={}). Cause: {}", uname, ar.cause());
// continue when KO
}
});
此代码可以正常工作。
我查看了Verticle
文档,并发现了 一种我从未见过的init()
回调方法。由于文档并没有详细说明其实际功能,因此我对其进行了定义,以查看其在生命周期中的哪个位置被调用。
@Override
public void init(Vertx vertx, Context context) {
log.info("init()");
JsonObject cnf = context.config();
log.info("init.config={}", cnf.toString());
}
但是,当定义了init()
时,我在java.lang.NullPointerException
中调用JsonObject cnf = config();
的行上得到了一个start()
:
java.lang.NullPointerException: null
at io.vertx.core.AbstractVerticle.config(AbstractVerticle.java:85)
at org.buguigny.cluster.UserVerticle.start(UserVerticle.java:30)
at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$8(DeploymentManager.java:494)
at io.vertx.core.impl.ContextImpl.executeTask(ContextImpl.java:320)
at io.vertx.core.impl.EventLoopContext.lambda$executeAsync$0(EventLoopContext.java:38)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
我的问题是:
第1季度:为何抛出NullPointerException
的任何线索?
第二季度:init()
的目的是什么?它是Vertx内部的还是可以由客户端代码实现,例如在部署配置中传递的verticle对象中定义某些字段?
答案 0 :(得分:2)
public List<String> merge(){
List<String> result = new ArrayList();
result.addAll(listOne);
result.addAll(listTwo);
result.addAll(myObjectTwo.getListOne());
result.addAll(myObjectTwo.getListTwo());
return result;
}
方法仅供内部使用,并在Javadoc中进行了记录。这是源代码:
init
如果 /**
* Initialise the verticle.<p>
* This is called by Vert.x when the verticle instance is deployed. Don't call it yourself.
* @param vertx the deploying Vert.x instance
* @param context the context of the verticle
*/
@Override
public void init(Vertx vertx, Context context) {
this.vertx = vertx;
this.context = context;
}
被记录在任何用户文档中,则表示错误,请报告。