Your First Vert.x Application
Once you have installed Vert.x you are ready to create your first Vert.x application. This tutorial will take you through creating your first Vert.x application step by step.
Create Vertx Instance
The first step to using Vert.x embedded in your own Java application is to create a
Vertx instance.
Here is how you create a
Vertx instance:
import io.vertx.core.Vertx; public class VertxApp { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); } }
You create a
Vertx instance by calling
Vertx.vertx().
The
Vertx instance creates a number of threads internally to handle the exchange of messages between
verticles. These threads are not daemon threads, so they prevent the JVM from shutting down, even if the main thread
creating the
Vertx instance terminates.
Creating a Verticle
The
Vertx instance by itself doesn't do much except all the thread management, creating an event bus etc.
which are all communication and infrastructure tasks. In order to get the application to do something useful, you
need to deploy one or more verticles (component) inside the
Vertx instance.
Before you can deploy a verticle you need to create it. You do so by creating a class that extends
AbstractVerticle. Here is a verticle example:
package examples.vertx; 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!"); } }
A verticle has a
start() and a
stop() method which are called when the verticle is
deployed and when it is undeployed. You should perform any necessary initialization work inside the
start()
method, and any necessary cleanup work inside the
stop() method.
Deploying a Verticle
Once you have created a verticle you need to deploy it to the
Vertx instance.
You deploy a verticle using one of the
deployVerticle() methods on the
Vertx instance.
Here is a Vert.x verticle deployment example:
import io.vertx.core.Vertx; public class VertxVerticleMain { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); vertx.deployVerticle(new MyVerticle()); } }
The verticle is deployed using this method call:
vertx.deployVerticle(new MyVerticle());
This method call deploys the
MyVerticle instance passed as parameter to the
deployVerticle()
method.
The
Vertx instance has another
deployVerticle() method which takes the fully qualified
class name of the verticle to deploy as parameter. Here is how deploying the
MyVerticle using that
method would look:
vertx.deployVerticle("examples.vertx.MyVerticle");
There are more options available for deploying verticles. For instance, you can specify how many verticle instances of a given verticle class to deploy. All that will be covered in later texts.
What is Next?
Once you know how to create a
Vertx instance and know how to deploy verticles, you are ready to
start playing around with the various parts of Vert.x . Each part will be covered in later texts in this tutorial
trail.