Difficulty: beginner
Estimated Time: 15 minutes

In this scenario, you will learn more about Eclipse Vert.x, one of the runtimes included in Red Hat OpenShift Application Runtimes.

What you will learn

You will learn how to get started with building a reactive web application that can serve static content as well as server-side business logic using Eclipse Vert.x

What is Eclipse Vert.x?

Local Web Browser Tab

Eclipse Vert.x is a reactive toolkit for the Java Virtual Machine that is polyglot (e.g., supports multiple programming languages). In this session, we will focus on Java, but it would be possible to build the same application in JavaScript, Groovy, Ruby, Ceylon, Scala, and Kotlin.

Eclipse Vert.x is event-driven and non-blocking, which means that applications in Vert.x can handle a lot of concurrent requests using a small number of kernel threads. Vert.x lets your app scale with minimal hardware.

Vert.x is incredibly flexible - whether it's network utilities, sophisticated modern web applications, HTTP/REST microservices, high volume event processing or a full-blown back-end message-bus application, Vert.x is a great fit.

Vert.x is used by many different companies from real-time gaming to banking and everything in between.

Vert.x is not a restrictive framework or container and we don't tell you a correct way to write an application. Instead, we give you a lot of useful bricks and let you create your app the way you want to.

Vert.x is fun - Enjoy being a developer again. Unlike restrictive traditional application containers, Vert.x gives you incredible power and agility to create compelling, scalable, 21st-century applications the way you want to, with a minimum of fuss, in the language you want.

  • Vert.x is lightweight - Vert.x core is around 650kB in size.
  • Vert.x is fast. Here are some independent numbers.
  • Vert.x is not an application server. There's no monolithic Vert.x instance into which you deploy applications. You just run your apps wherever you want to.
  • Vert.x is modular - when you need more bits just add the bits you need and nothing more.
  • Vert.x is simple but not simplistic. Vert.x allows you to create powerful apps, simply.
  • Vert.x is an ideal choice for creating light-weight, high-performance, microservices.

In this scenario, you learned a bit more about what Eclipse Vert.x is, and how it can be used to create reactive microservice-oriented applications. We will add additional scenarios explain more how to build resilient reactive microservices shortly, so check back to learn.openshift.com

To summarize, you developed a web application that both returned static content as well as server-side generated content using Eclipse Vert.x. After testing them locally (in your terminal window) you also deployed your application to the OpenShift Container Platform, using a single maven command.

NOTE: If you don't like building and deploying from the command line, everything we have done in this scenario is possible in a reasonable modern IDE that supports using maven. For example Eclipse IDE, IntelliJ, or NetBeans. JBoss Developer Studio also provides more advanced tooling.

Eclipse Vert.x is implemented using reactive concepts and also support using reactive programming like JavaRx,

Eclipse Vert.x is one of many components of Red Hat OpenShift Application Runtimes - check out other scenarios (for example the Eclipse Vert.x scenario) to learn about other runtimes you can use.

Additional Resources

More background and related information on Red Hat OpenShift Application Runtimes and Eclipse Vert.x can be found here:

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Eclipse Vert.x development on OpenShift

Step 1 of 5

Step 1

Build your first application

For your convenience, this scenario has been created with a base project using the Java programming language and the Apache Maven build tool.

For educational purposes, this scenario uses one single Java class named HttpApplication.java. Now open that file, by clicking on the link below:

src/main/java/com/example/HttpApplication.java

As you review the content, you will notice that there are a lot of TODO comments. Do not remove them! These comments are used as a marker and without them, you will not be able to finish this scenario.

Notice that HttpApplication class extends another class called AbstractVerticle. Before we start implementing our logic, let's discuss a bit what a Verticle is.

What is a verticle?

Verticles — the Building Blocks of Eclipse Vert.x

Vert.x gives you a lot of freedom in how you can shape your application and code. But it also provides bricks to start writing reactive applications. Verticles are chunks of code that get deployed and run by Vert.x. An application, such as a microservice, would typically be comprised of many verticles. A verticle typically creates servers or clients, registers a set of Handlers', and encapsulates a part of the business logic of the system.

In Java, a verticle is a class extending the Abstract Verticle class:

    import io.vertx.core.AbstractVerticle;

    public class MyVerticle extends AbstractVerticle {
        @Override
        public void start() throws Exception {
            // Executed when the verticle is deployed
        }

        @Override
        public void stop() throws Exception {
            // Executed when the verticle is un-deployed
        }
    }

Creating a simple web server that can serve static content

1. Compile and run the application Eclipse Vert.x application

Before we add code to create our web server you should build and test that current application starts as it should.

Since this is already a working application, you can run it without any code changes locally directly using maven with goal vertx:run

mvn compile vertx:run

NOTE: The vert.x maven plugin replays the phases executed before it starts the application and it's therefor good practice to also specify the goals.

At this stage, the application doesn't do anything, but after a while, you should see the following two lines in your console window:

[INFO] Starting vert.x application...
[INFO] THE HTTP APPLICATION HAS STARTED

NOTE: The "Starting vert.x application..." line indicates that your application is starting and that the Verticles are triggered asynchronously. The message "THE HTTP APPLICATION HAS STARTED" that comes from a System.out.println in HttpApplication.java Verticle.

2. Add a router that can serve static content

Your first job will be to add an HTTP server that can return HTML pages.

NOTE: For your convenience the HttpApplication already has the necessary import statements.

First, you need to create a Router object. This router will handle all incoming requests. Add the following line in the start method HttpApplication.java where the matching TODO statement is (or click the handy button that will insert the code in the correct place)

Router router = Router.router(vertx);

We also need to tell the router to map incoming requests to files in the default location (e.g. src/main/resources/webroot). You can do that by adding the following line to the matching TODO statement.

router.get("/*").handler(StaticHandler.create());

Now we are ready to start the HTTP server,

vertx.createHttpServer().requestHandler(router::accept).listen(8080);

NOTE: As you may have noticed Vert.x will automatically detect when you change and files an immediately redeploy the changes. The automatic redeploy is very convenient for development purposes but can be turned off for production usage.

3. Test the application

To begin, click on the Local Web Browser tab in the console frame of this browser window, which will open another tab or window of your browser pointing to port 8080 on your client.

Local Web Browser Tab

You should now see an HTML page that looks like this:

Local Web Browser Tab

or use this link.

NOTE: The Invoke button doesn't work yet, but we will fix that in the next step.

Congratulations

You have now successfully executed the first step in this scenario.

Now you've seen how you with basically three lines of code can create an HTTP Server that is capable of serving static content using the Vert.x Toolkit.

In next step of this scenario, we will add server-side business logic to our application.

Terminal
Local Web Browser
OpenShift Console