3

我正在寻找当前正在运行的 Verticle 的部署 ID。

目标是允许 Verticle 自行取消部署。我目前通过事件总线将deploymentID 传递到已部署的verticle 以完成此操作,但更喜欢一些直接的访问方式。

container.undeployVerticle(deploymentID)

4

1 回答 1

1

有两种方法可以获得部署 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();
    }
}
});

访问平台管理器如下:

 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");
   }
  }

参考:

于 2014-09-27T09:57:56.267 回答