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.
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:
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);
You can programmatically tell Vert.x to stop:
vertx.close();
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With