Stop vertical vertex in Eclipse

I follow the Jenkov tutorial regarding 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 starting VertxVerticleMain.java I saw "MyVerticle started!" in the Eclipse console, but don’t know how to call stop in MyVerticle .

Jenkov said the Stop () method is called when Vert.x is turned off and your vertical point should stop. How exactly do I close my Vert.x and stop this top? I want to see MyVerticle stopped! in the console.

+9
source share
2 answers

From Vert.x docs :

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

If you start Vert.x from the main method and terminate the JVM process (for example, by clicking the stop button in Eclipse), Vert.x probably does not signal a vertical abandonment or the JVM terminates before Vert.x manages to expand the vertices .

You can do several things to ensure that the vertical will not be expanded, and will call the stop () method:

  • Launch Verticle using the vertx command line. When you stop the process (or return vert.x to stop), Vert.x will make sure that all vertices are not expanded.
  • You can programmatically expand expanded vertices by retrieving the deployId list and calling undeploy for all id:

     vertx.deploymentIDs().forEach(vertx::undeploy); 
  • You can programmatically stop Vert.x:

     vertx.close(); 
  • You can add a shutdown crane to make sure that one of the above options is executed when the JVM completes:

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

You can either programmatically expand the index by calling the Vert.x API, or simply stop the Java process, which at the end starts the Vert.x process to stop.

By the way, you should ask yourself if it really is necessary that the stop () method is always called when the process that starts the vertical stops. You can never be sure that this will happen; when a process is forcibly stopped or killed, the stop () method may not be called.

+11
source

Your code should add super.stop () and super.start () functions as follows:

 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); } } 
0
source

Source: https://habr.com/ru/post/1246350/


All Articles