Chapter 4. Developing an application for the Eclipse Vert.x runtime

4.1. Creating a basic Eclipse Vert.x application

In addition to using a booster, you can create new Eclipse Vert.x applications from scratch and deploy them to OpenShift.

4.1.1. Creating an application

Create a simple Greeting application to run on OpenShift using Eclipse Vert.x. The following procedure shows you how to:

  • Write some simple application code that makes use of functionalities provided by Eclipse Vert.x.
  • Declare dependencies and configure the application build using a pom.xml file.
  • Start your application on localhost and verify that it works.

Prerequisites

  • Maven installed.
  • JDK 8 or later installed.

Procedure

  1. Create the application directory and navigate to it.

    $ mkdir myApp
    $ cd myApp
  2. Create a pom.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <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.example</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>My Application</name>
      <description>Example application using RHOAR Vert.x</description>
    
      <properties>
        <vertx.version>3.5.4.redhat-00002</vertx.version>
        <vertx-maven-plugin.version>1.0.17</vertx-maven-plugin.version>
        <vertx.verticle>com.example.MyApp</vertx.verticle>
        <fabric8.generator.from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:1.2</fabric8.generator.from>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      </properties>
    
      <!-- Import dependencies from the RHOAR Vert.x BOM file. -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-dependencies</artifactId>
            <version>${vertx.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <!-- Specify the RHOAR Vert.x artifacts that your application depends on. -->
      <dependencies>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-core</artifactId>
        </dependency>
        <dependency>
          <groupId>io.vertx</groupId>
          <artifactId>vertx-web</artifactId>
        </dependency>
      </dependencies>
    
      <!-- Specify the repositories containing RHOAR artifacts. -->
      <repositories>
        <repository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </repository>
      </repositories>
    
      <!-- Specify the repositories containing the plugins used to execute the build of your application. -->
      <pluginRepositories>
        <pluginRepository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </pluginRepository>
      </pluginRepositories>
    
      <!-- Configure your application to be packaged using the Vert.x Maven Plugin. -->
      <build>
        <plugins>
          <plugin>
            <groupId>io.reactiverse</groupId>
            <artifactId>vertx-maven-plugin</artifactId>
            <version>${vertx-maven-plugin.version}</version>
            <executions>
              <execution>
                <id>vmp</id>
                <goals>
                  <goal>initialize</goal>
                  <goal>package</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
      <!-- Configure your application to be deployed to OpenShift using the Fabric8 Maven Plugin. -->
      <profiles>
        <profile>
          <id>openshift</id>
          <build>
            <plugins>
              <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>fabric8-maven-plugin</artifactId>
                <version>3.5.40</version>
                <executions>
                  <execution>
                    <goals>
                      <goal>resource</goal>
                      <goal>build</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
        </profile>
      </profiles>
    </project>
  3. Create a new class in src/main/java/com/example/.

    As a recommended practice, ensure that the location of your class within the directory structure of your project reflects the value that you set for groupId in your pom.xml file. For example, for <groupId>my.awesome.project</groupId>, the location of the class should be src/main/java/my/awesome/project/.

    package com.example;
    
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.Future;
    
    public class MyApp extends AbstractVerticle {
    
        @Override
        public void start(Future<Void> fut) {
            vertx
                .createHttpServer()
                .requestHandler(r ->
                    r.response().end("Greetings!"))
                .listen(8080, result -> {
                    if (result.succeeded()) {
                        fut.complete();
                    } else {
                        fut.fail(result.cause());
                    }
                });
        }
    }
  4. Start your application. Execute the following command in the directory containing you application.

    $ mvn vertx:run
  5. Using curl or your browser, verify your application is running at http://localhost:8080.

    $ curl http://localhost:8080
    Greetings!

Additional information

  • As a recommended practice, you can configure liveness and readiness probes to enable health monitoring for your application when running on OpenShift. To learn how application health monitoring on OpenShift works, try the Health Check booster.

4.1.2. Deploying an application to OpenShift

This procedure shows you how to:

  • Build your application and deploy it to OpenShift using the Fabric8 Maven Plugin.
  • Use the command line to interact with your application running on OpenShift.

Prerequisites

  • The oc CLI client installed.
  • Maven installed.
  • A Maven-based application.

Procedure

  1. Log in to your OpenShift instance with the oc client.

    $ oc login ...
  2. Create a new project.

    $ oc new-project MY_PROJECT_NAME
  3. In a terminal application, navigate to the directory containing your application:

    $ cd myApp
  4. Use Maven to start the deployment to OpenShift.

    $ mvn clean fabric8:deploy -Popenshift

    This command uses the Fabric8 Maven Plugin to launch the S2I process on OpenShift and to start the pod.

  5. Check the status of your booster and ensure your pod is running.

    $ oc get pods -w
    NAME                             READY     STATUS      RESTARTS   AGE
    MY_APP_NAME-1-aaaaa               1/1       Running     0          58s
    MY_APP_NAME-s2i-1-build           0/1       Completed   0          2m

    The MY_APP_NAME-1-aaaaa pod should have a status of Running once it is fully deployed and started. Your specific pod name will vary. The number in the middle will increase with each new build. The letters at the end are generated when the pod is created.

  6. Once your booster is deployed and started, determine its route.

    Example Route Information

    $ oc get routes
    NAME                 HOST/PORT                                                     PATH      SERVICES        PORT      TERMINATION
    MY_APP_NAME         MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME      MY_APP_NAME      8080

    The route information of a pod gives you the base URL which you use to access it. In the example above, you would use http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME as the base URL to access the application.

  7. Using curl or your browser, verify your application is running in OpenShift.

    $ curl http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME
    Greetings!

4.2. Deploying an existing Eclipse Vert.x application to OpenShift

You can easily deploy your existing application to OpenShift using the Fabric8 Maven plugin.

Prerequisites

  • A Eclipse Vert.x–based application

Procedure

  1. Add the following profile to the pom.xml file in the root directory of your application:

    <profiles>
        <profile>
          <id>openshift</id>
          <build>
            <plugins>
              <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>fabric8-maven-plugin</artifactId>
                <version>3.5.40</version>
                <executions>
                  <execution>
                    <goals>
                      <goal>resource</goal>
                      <goal>build</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
        </profile>
    </profiles>

    In this profile, the Fabric8 Maven plugin is invoked for building and deploying the application to OpenShift.

  2. Deploy the application to OpenShift according to instructions in Section 4.1.2, “Deploying an application to OpenShift”.