|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
Handler instance (which you will have registered with the HttpServer).
Verticle class and override the start() method, which is the entry-point to your Verticle.
See the Vert.x Javadoc to learn more about these classes.
Notice the vertx variable in Listing 2: What is it for? The Verticle class defines vertx as a protected member variable (that your Verticle inherits), which provides access to the Vert.x runtime. The vertx variable is of type Vertx, which exposes the following methods:
createHttpClient() creates an HTTP/HTTPS client
createHttpServer() creates an HTTP/HTTPS server
createNetClient() creates a TCP/SSL client
createNetServer() creates a TCP/SSL server
creatSockJSServer() creates a SockJS server that wraps an HTTP server
eventBus() provides your application access to the event bus
fileSystem() provides your application access to the file system
sharedData() provides your application access to the shared data object, which can be used to share data between Verticles
The code in Listing 2 creates a new HTTP server, retrieves its request-handler reference, and sets the request handler to
a newly created HttpServerRequest handler. The Handle interface defines a method named handler() and uses generics to define the type of instance that is passed to it in the class definition; in this case HttpServerRequest. The HttpServerRequest then defines the following fields:
String containing the method of the given request, such as GET, POST, PUT, DELETE, and so forth.
String containing the requested path, such as /index.html.
String containing the query parameters, such as the part that follows the question mark in the following: ?key=value.
HttpServerResponse that represents the response to the HTTP request.
Listing 2 completes by mapping an empty request -- "/" -- to index.html, and then invoking the HttpServerResponse's sendFile() method to tell Vert.x to stream the specified file back to the caller.
In summary, the Server class accesses the Vert.x runtime, asks it to create a new HTTP server, and registers a Handler (that expects an HttpServerRequest variable) with the HttpServer. In the Handler's handle() method, the Server class serves files from the filesystem located in the webroot directory, which is relative to wherever you launched the Server Verticle from.
Let's build the sample application, then we'll use Vert.x to execute it. The Maven POM file for this project is shown in Listing 3.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.geekcap</groupId>
<artifactId>vertx-examples</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>vertx-examples</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>2.0.0-final</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-platform</artifactId>
<version>2.0.0-final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The two dependencies added to the POM file are vertx-core and vertx-lang-java, which are needed to develop Vert.x applications in Java. To build this application, execute the following Maven command: