Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

Open source Java projects: Vert.x

Enterprise messaging and integration with Vert.x

  • Print
  • Feedback

Page 3 of 6

  • Handler is the base class for all handlers; in short: something happened asynchronously, so handle it!
  • HttpServerRequest represents a server-side HTTP request in Vert.x. An instance of this class will be created for each request that is handled by the server, then passed to your application via the Handler instance (which you will have registered with the HttpServer).
  • Verticle is the primary unit of deployment in a Vert.x application. In order to use Vert.x, you need to extend the 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:

  • method is a String containing the method of the given request, such as GET, POST, PUT, DELETE, and so forth.
  • path is a String containing the requested path, such as /index.html.
  • query is a String containing the query parameters, such as the part that follows the question mark in the following: ?key=value.
  • response is a reference to an HttpServerResponse that represents the response to the HTTP request.
  • uri is the complete URI of the 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.

Building the web server

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.

Listing 3. Maven POM to build the web server

<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:

  • Print
  • Feedback

Resources