Writing Verticles
There are two alternatives to create verticles in Groovy:
a plain Groovy script
a Groovy class implementing the Verticle
interface or extending the AbstractVerticle
class
For example, the next snippet is a valid Groovy verticle:
println "Hello from vertx"
On deployment, by default, Vert.x executes the script. Optionally, your script can provide the startVertx
and
stopVertx
methods. Theses methods are called respectively when the verticle starts and stops:
void vertxStart() {
println "starting"
}
void vertxStop() {
println "stopping"
}
Alternatively, you can extend the AbstractVerticle
class and implement the start
and
stop
methods:
import io.vertx.core.AbstractVerticle;
public class HelloWorldHttpVerticle extends AbstractVerticle {
public void start() {
println("Starting")
}
public void stop() {
println("Stopping")
}
}
When Vert.x deploys the verticle it will call the start
method, and when the method has completed the
verticle will be considered started.
You can also optionally override the stop
method. This will be called by Vert.x when the verticle is undeployed
and when the method has completed the verticle will be considered stopped.
Accessing the vertx instance from a verticle
Regardless the way you use to implement your verticle, you access the vert.x instance using the vertx
variable /
field.
vertx.deployVerticle("another_verticle.rb")
import io.vertx.lang.groovy.GroovyVerticle;
public class HelloWorldHttpVerticle extends GroovyVerticle {
public void start() {
vertx.deployVerticle("another_verticle.js")
}
}
Asynchronous Verticle start and stop
Sometimes you want to do something in your verticle start-up which takes some time and you don’t want the verticle to be considered deployed until that happens. For example you might want to deploy other verticles in the start method.
You can’t block waiting for the other verticles to deploy in your start method as that would break the Golden Rule.
So how can you do this?
The way to do it is to implement theasynchronous* start method. This version of the method takes a Future as a parameter.When the method returns the verticle willnot* be considered deployed.
Some time later, after you’ve done everything you need to do (e.g. start other verticles), you can call complete on the Future (or fail) to signal that you’re done. Similarly, there is an asynchronous version of the stop method too. You use this if you want to do some verticle cleanup that takes some time.
When your verticle is implemented as a script, asynchronous start and stop are implemented as follows:
import io.vertx.core.Future
void vertxStart(Future<Void> future) {
println "starting"
vertx.deployVerticle("v.rb", { res ->
if (res.succeeded()) {
future.complete()
} else {
future.fail()
}
})
}
void vertxStop(Future<Void> future) {
println "stopping"
future.complete()
}
If your verticle extends AbstractVerticle
, you override the
start
and
stop
methods:
import io.vertx.core.Future
import io.vertx.core.AbstractVerticle
public class HelloWorldHttpVerticle extends AbstractVerticle {
public void start(Future<Void> future) {
println "starting"
vertx.deployVerticle("v.rb",
{ res ->
if (res.succeeded()) {
future.complete()
} else {
future.fail()
}
})
}
public void stop(Future<Void> future) {
println("stopping")
future.complete()
}
}
Note
|
You don’t need to manually undeploy child verticles started by a verticle, in the verticle’s stop method. Vert.x will automatically undeploy any child verticles when the parent is undeployed. |
API changes from previous versions
Vert.x for Groovy has been revamped in Vert.x 3.4.x and provided an automatic migration path for Verticles written against the previous API.
Vert.x 3.5.0 assumes that applications have been migrated to the new API.