我正在寻找当前正在运行的Verticle的部署ID。
目标是允许一个vertcle取消部署自己。我目前通过事件总线将deploymentID传递到已部署的Verticle来完成此任务,但更喜欢一些直接的访问方式。
答案 0 :(得分:0)
有两种方法可以获得部署ID。如果您有一些启动并处理所有模块部署的Verticle,您可以添加异步结果处理程序,然后以这种方式获取部署ID,或者您可以使用反射从容器中获取平台管理器。
异步处理程序如下:
container.deployVerticle("foo.ChildVerticle", new AsyncResultHandler<String>() {
public void handle(AsyncResult<String> asyncResult) {
if (asyncResult.succeeded()) {
System.out.println("The verticle has been deployed, deployment ID is " + asyncResult.result());
} else {
asyncResult.cause().printStackTrace();
}
}
});
按如下方式访问Platform Manager:
protected final PlatformManagerInternal getManager() {
try {
Container container = getContainer();
Field f = DefaultContainer.class.getDeclaredField("mgr");
f.setAccessible(true);
return (PlatformManagerInternal)f.get(container);
}
catch (Exception e) {
e.printStackTrace();
throw new ScriptException("Could not access verticle manager");
}
}
protected final Map<String, Deployment> getDeployments() {
try {
PlatformManagerInternal mgr = getManager();
Field d = DefaultPlatformManager.class.getDeclaredField("deployments");
d.setAccessible(true);
return Collections.unmodifiableMap((Map<String, Deployment>)d.get(mgr));
}
catch (Exception e) {
throw new ScriptException("Could not access deployments");
}
}
参考文献: