Event bus

The event bus is the core of the system of a Vert.x application. Each verticle communicates with other verticles using the event bus. This means that, once a message or event is looped into the event bus, this message gets delivered to the appropriate verticle. The verticle then reacts by reading the event (it can contain objects or any primitive data type such as strings or numbers) and by taking the proper action.
Events and messages are handled asynchronously, so every time a verticle produces a message, the latter is sent to the bus in a queue and control is returned to the verticle. Later, as computational resource are available, the listening verticle gets the event and it sends back a response using Future and callback methods.

Creating a microservice with Vert.x is very simple; just add the following settings in your Maven project file (that is, pom.xml):

<properties>   
   <java.version>1.8</java.version>   
   <vertx.version>3.5.3</vertx.version>   
</properties>   
   
<build>   
   <plugins>   
         <plugin>   
         <groupId>org.apache.maven.plugins</groupId>   
         <artifactId>maven-compiler-plugin</artifactId>   
         <version>3.5.1</version>   
         <configuration>   
               <source>${java.version}</source>   
               <target>${java.version}</target>   
         </configuration>   
         </plugin>   
         <plugin>   
         <groupId>org.apache.maven.plugins</groupId>   
         <artifactId>maven-shade-plugin</artifactId>   
         <version>2.4.3</version>   
         <executions/>   
         </plugin>   
         <plugin>   
         <groupId>org.codehaus.mojo</groupId>   
         <artifactId>exec-maven-plugin</artifactId>   
         <version>1.5.0</version>   
         <configuration>   
               <mainClass>io.vertx.core.Launcher</mainClass>   
               <arguments>   
                     <argument>run</argument>   
                     <argument>com.example.demo.MainVerticle</argument>   
               </arguments>   
         </configuration>   
         </plugin>   
   </plugins>   
</build>   
   
<dependencies>   
         <dependency>   
         <groupId>io.vertx</groupId>   
         <artifactId>vertx-unit</artifactId>   
         <version>${vertx.version}</version>   
         </dependency>   
         <dependency>   
         <groupId>io.vertx</groupId>   
         <artifactId>vertx-core</artifactId>   
         <version>${vertx.version}</version>   
         </dependency>   
         <dependency>   
         <groupId>io.vertx</groupId>   
         <artifactId>vertx-web</artifactId>   
         <version>${vertx.version}</version>   
         </dependency>   
</dependencies>   

And an example application will look like the following:

package com.packt.vertx;   
   
import io.vertx.core.AbstractVerticle;   
   
public class MainVerticle extends   AbstractVerticle {   
   
   @Override   
   public void start() throws Exception {   
         vertx.createHttpServer().requestHandler(req   -> {   
               req.response()   
               .putHeader("content-type", "text/plain")   
               .end("Running Vert.x!");   
         }).listen(8080);   
         System.out.println("HTTP server started on port 8080");   
   }   
}   

Furthermore, Vert.x's website offers a bootstrap page (http://start.vertx.io/), where you can specify the needs for your microservice by filling in the form with all of the dependencies you need, as shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset