Reputation: 2470
I'm totally new to vert.x and I wrote a hello world program in JavaScript:
console.log("Hello, World");
When I run the program from the linux command line (on an old ubuntu development system, if it matters), the program runs but never exits:
hostname$ ./node_modules/.bin/vertx run server.js
Hello, World
Succeeded in deploying verticle
^Chostname$
I have to press CTRL-C to terminate the process, which seems wrong. In our production environment it is desirable for processes to exit when they are done.
I found this: http://vertx.io/docs/vertx-core/java/#_causing_vert_x_to_exit, so I added a call to the vertx.close
method:
console.log("Hello, World");
vertx.close( function onClose() {
console.log("Goodbye, cruel world.");
});
And while the onClose
function is called, the process is not exiting:
hostname$ node_modules/.bin/vertx run server.js
Hello, World
Succeeded in deploying verticle
Goodbye, cruel world.
^Chostname$
I also tried vertx.exit()
as suggested here https://groups.google.com/forum/#!topic/vertx/5ROEKq4STEw, but still the process does not exit.
Finally, I found this discussion (https://groups.google.com/forum/#!topic/vertx/L2r1NUvJcXs) that appears to include a Java code patch for vert.x when running on linux, but this seems awfully heavy-handed.
Here is a list of version numbers for those who might be curious:
hostname$ node_modules/.bin/vertx -version
3.0.0
hostname$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) Server VM (build 25.91-b14, mixed mode)
hostname$ uname -a
Linux hostname 3.11.0-15-generic #25~precise1-Ubuntu SMP Thu Jan 30 17:42:40 UTC 2014 i686 athlon i386 GNU/Linux
hostname$
So several failed google attempts later I'm asking SO for help. What am I doing wrong here?
[POSTSCRIPT]
So it looks like I TL;DR'ed the "Java code patch" at https://groups.google.com/forum/#!topic/vertx/L2r1NUvJcXs. The key line of code is:
System.exit(0);
Works like a charm. I'm posting this here for others to learn from my mistake. awkward blush
Upvotes: 1
Views: 3324
Reputation: 11001
When I'm extending AbstractVerticle
such as: public class MainVerticle extends AbstractVerticle
I can use:
this.getVertx().close();
Cite: Vert.x Core Manual,Causing Vert.x to exit
Threads maintained by Vert.x instances are not daemon threads so they will prevent the JVM from exiting.
If you are embedding Vert.x and you have finished with it, you can call close
to close it down.
This will shut-down all internal thread pools and close other resources, and will allow the JVM to exit.
remember that if your verticle has simple synchronous clean-up tasks to complete then override stop
method and put your clean-up code in here.
@Override
public void stop() {
System.out.println("stopping...");
}
Upvotes: 1
Reputation: 2792
I think it wasn't the intention of vert.x to run in a batch mode like this. Vert.x provides a runtime environment to process events. It provides eventloops which you can use to process your events.
Check out this presentation, which explains the core concepts of vert.x very well: https://www.youtube.com/watch?v=qL5BGHPXrac.
Vert.x is implemented in java and when you start vertx like this, then you start the jvm, which executes vertx core, which then executes your javascript. The problem is, that I think you have no access to the java execution environment from your javascript file.
If you are willing to write a little bit java code I wrote you a possible solution: https://github.com/floriankammermann/vertx-examples/tree/master/self-terminating
Be aware, that you have to communicate everytime over the eventbus if you need to communicate between verticles
Upvotes: 0