在Eclipse中停止vertx verticle

时间:2016-04-03 18:21:13

标签: java eclipse vertx3 vert.x

我在vertx上关注Jenkov's tutorial。这里我有两个文件:

MyVerticle.java:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

public class MyVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> startFuture) {
        System.out.println("MyVerticle started!");
    }

    @Override
    public void stop(Future stopFuture) throws Exception {
        System.out.println("MyVerticle stopped!");
    }
}

和VertxVerticleMain.java:

import io.vertx.core.Vertx;

public class VertxVerticleMain {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new MyVerticle());
    }
}

运行VertxVerticleMain.java后,我在Eclipse的控制台中看到了"MyVerticle started!",但我不知道如何在stop中呼叫MyVerticle

Jenkov说当Vert.x关闭并且您的Verticle需要停止时,会调用stop()方法。我究竟如何关闭Vert.x并停止此Verticle?我想在控制台中看到MyVerticle stopped!

2 个答案:

答案 0 :(得分:8)

来自the Vert.x docs

Vert.x calls this method when un-deploying the instance. You do not call it yourself. 

如果从main方法运行Vert.x并终止JVM进程(例​​如,通过单击Eclipse中的&#39; stop&#39;按钮),Vert.x可能未发出信号通知取消部署Verticle,或者在Vert.x有时间取消部署Verticle之前JVM终止。

你可以做很多事情来确保取消部署Verticle并调用stop()方法:

  1. 使用vertx命令行启动Verticle。当您停止该过程(或告诉vert.x停止)时,Vert.x将确保取消部署所有Verticle。
  2. 您可以通过获取deploymentId列表并为所有ID调用undeploy以编程方式取消部署已部署的Verticle:

    vertx.deploymentIDs().forEach(vertx::undeploy);
    
  3. 您可以通过编程方式告诉Vert.x停止:

    vertx.close();
    
  4. 您可以添加一个关闭挂钩,以确保在JVM终止上执行上述选项之一:

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            vertx.close();
        }
    });
    
  5. 您可以通过调用Vert.x API以编程方式取消部署Verticle,或者只是停止Java进程,该进程在术语中触发Vert.x进程停止。

    顺便说一下,当运行Verticle的进程停止时,是否真的有必要始终调用stop()方法,这是值得的。你永远无法确定是否会发生这种情况;当进程被强制停止或终止时,可能不会调用stop()方法。

答案 1 :(得分:0)

您的代码应添加super.stop()和super.start()这样的函数:

public class MyVerticle extends AbstractVerticle {

    @Override
    public void start(Future<Void> startFuture) {
        //must call super.start() or call startFuture.complete()
        super.start(startFuture); 
        System.out.println("MyVerticle started!");
        System.out.println("Verticle_stopFuture.start(): deployId=" + context.deploymentID());
    }

    @Override
    public void stop(Future stopFuture) throws Exception {
        //must call super.stop() or call stopFuture.complete()
        super.stop(stopFuture);
        System.out.println("MyVerticle stopped!");
    }
}

和VertxVerticleMain.java:

public class VertxVerticleMain {
    static String verticle_deployId;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("start main(): thread="+Thread.currentThread().getId());


        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new MyVerticle(), new Handler<AsyncResult<String>>(){

            @Override
            public void handle(AsyncResult<String> asyncResult) {

                if (asyncResult.succeeded()) { // khi startFuture.complete() đc gọi
                    System.out.println("asyncResult = DeployId =" + asyncResult.result());

                    verticle_deployId = asyncResult.result();
                } else { //khi startFuture.fail() đc gọi
                    System.out.println("Deployment failed!");  //vì chưa đc cấp id
                }
            }
        });

        // waiting for Verticle context is allocate by Vertx
        Thread.currentThread().sleep(500);

        Set<String> deploymentIDs = vertx.deploymentIDs();
        System.out.println("==============  (sleeped 500ms wait for Context allocated), list of deploymentIDs: number Deployments =" + deploymentIDs.size());
        for(String depId: deploymentIDs){
            //
            System.out.println(depId);
        }

        //close verticle here
        vertx.undeploy(verticle_deployId);


    }

}