Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop vertx verticle in Eclipse

I'm following Jenkov's tutorial on vertx. Here I have two files:

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

and VertxVerticleMain.java:

import io.vertx.core.Vertx;

public class VertxVerticleMain {

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

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

After running VertxVerticleMain.java, I saw "MyVerticle started!" in Eclipse's console but don't know how to call stop in MyVerticle.

Jenkov said that The stop() method is called when Vert.x shuts down and your verticle needs to stop. How exactly do I shut down my Vert.x and stop this verticle? I want to see MyVerticle stopped! in the console.

like image 524
Diep TL Avatar asked Apr 03 '16 18:04

Diep TL


2 Answers

From the Vert.x docs:

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

If you run Vert.x from a main method and you terminate the JVM process (by clicking the 'stop' button in Eclipse, for example), Vert.x probably isn't signaled to undeploy the verticles, or the JVM terminates before Vert.x has time to undeploy the verticles.

You can do a number of things to ensure that the verticle will be undeployed and the stop() method will be called:

  1. Start the Verticle using the vertx commandline. When you stop the process (or tell vert.x to stop), Vert.x will make sure that all verticles are undeployed.
  2. You can programmatically undeploy the deployed verticles by fetching the list of deploymentId's and calling undeploy for all id's:

    vertx.deploymentIDs().forEach(vertx::undeploy);
    
  3. You can programmatically tell Vert.x to stop:

    vertx.close();
    
  4. You can add a shutdown hook to make sure that one of the options above is executed on JVM termination:

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            vertx.close();
        }
    });
    

You can either programmatically undeploy the verticle by calling the Vert.x API, or just stop the Java process, which in term triggers the Vert.x process to stop.

By the way, it's worth asking yourself whether it's really necessary that the stop() method is always called when the process running the verticle stops. You can never be sure that that happens; when the process is forced to stop or killed, the stop() method might not be called.

like image 62
Bert Jan Schrijver Avatar answered Oct 13 '22 12:10

Bert Jan Schrijver


your code should add super.stop() and super.start() function like that:

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

and 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);


    }

}
like image 26
HungNM2 Avatar answered Oct 13 '22 12:10

HungNM2