Logo

dev-resources.site

for different kinds of informations.

Build web application in Vert.x [Part 1/ ♾️]

Published at
7/2/2023
Categories
vertx
java
backend
api
Author
smolthing
Categories
4 categories in total
vertx
open
java
open
backend
open
api
open
Author
9 person written this
smolthing
open
Build web application in Vert.x [Part 1/ ♾️]

TL;DR: Start a simple vert.x web application with health check.

Steps

  1. Download Vertx Starter.zip: Java, Gradle and everything nice. Or you can use my exquisite buffet selection below:
curl -G https://start.vertx.io/starter.zip -d "groupId=com.example" -d "artifactId=starter" -d "vertxVersion=4.4.4" -d "vertxDependencies=vertx-web,vertx-web-client,vertx-web-graphql,vertx-web-sstore-redis,vertx-web-sstore-cookie,vertx-web-validation,vertx-web-openapi,vertx-service-discovery,vertx-circuit-breaker,vertx-config,vertx-kafka-client,vertx-consul-client,vertx-tcp-eventbus-bridge,vertx-micrometer-metrics,vertx-health-check,vertx-junit5,vertx-zookeeper,vertx-grpc-server,vertx-grpc-client,vertx-service-proxy,vertx-grpc-context-storage,vertx-http-service-factory,vertx-json-schema,vertx-shell" -d "language=java" -d "jdkVersion=11" -d "buildTool=gradle" --output starter.zip
Enter fullscreen mode Exit fullscreen mode
  1. Run command: ./gradlew run ./gradlew run
  2. Go to http://localhost:8888 localhost saying hello
  3. Add a router and health check using Using Vert.x Health Checks a. Add
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.ext.healthchecks.HealthCheckHandler;
import io.vertx.ext.healthchecks.HealthChecks;
import io.vertx.ext.web.Router;

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    HealthCheckHandler healthCheckHandler = HealthCheckHandler
      .createWithHealthChecks(HealthChecks.create(vertx));
    HealthCheckManager.configureHealthChecks(healthCheckHandler);

    Router router = Router.router(vertx);
    router.get("/ping").handler(healthCheckHandler);
    router.get("/*").handler(routingContext -> {
      routingContext.response()
        .putHeader("content-type", "text/plain; charset=utf-8")
        .end("Hello smolthing \uD83D\uDCA9.");
    });

    vertx.createHttpServer().requestHandler(router).listen(8888, http -> {
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server is running on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
  }
}
Enter fullscreen mode Exit fullscreen mode
import io.vertx.ext.healthchecks.HealthCheckHandler;
import io.vertx.ext.healthchecks.Status;

public class HealthCheckManager {
  public static void configureHealthChecks(HealthCheckHandler healthCheckHandler) {
    healthCheckHandler.register("App connection", promise -> {
      promise.complete(Status.OK());
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

healthcheck

Add catch all regex to say Hello smolthing 💩

Reference

vertx Article's
28 articles in total
Favicon
Error handlers and failure handlers in Vert.x
Favicon
Why we discarded Reactive systems architecture from our code?
Favicon
Build web application in Vert.x [Part 1/ ♾️]
Favicon
Yet another ode to Vert.x, or how to write a performance-wise expiring map in less than 100 lines of code.
Favicon
Surprising Qualities of Event Driven System
Favicon
Idiomatic Kotlin Abstractions for the Vert.x EventBus
Favicon
Vert.x Circuit Breaker
Favicon
Writing Async Tests for Vert.x using Kotlin
Favicon
Reducing Boilerplate in Vert.x Tests written in Kotlin
Favicon
Writing Vert.x Integration Tests with Kotlin & Testcontainers
Favicon
Quarkus: Entendendo a relação entre o Mutiny e o Vert.x
Favicon
HTTPS Client Certificate Authentication With Java
Favicon
Throttle HTTP requests on paged resources with Vert.x
Favicon
Supercharge Your Kotlin Vert.x Application with EventBus Extensions
Favicon
Handle backpressure between Kafka and a database with Vert.x
Favicon
Handling unknown JSON structures
Favicon
Introduction to Vert.x
Favicon
Future Composition in Vert.x
Favicon
How to extend Vert.x EventBus API to save on serialization.
Favicon
How to write beautiful unit tests in Vert.x
Favicon
Scaling Vert.x application for session dependent data processing.
Favicon
KVision v3.7.0 is released (with Vert.x support)
Favicon
Reactive Java using the Vert.x toolkit
Favicon
Vert.x Kotlin Coroutines
Favicon
How we built a RESTful API with Vert.x, Kotlin Coroutines and Keycloak
Favicon
vertx-jooq 2.4 released
Favicon
Sirix - Released 0.9.1 (time travel queries and versioning made easy)
Favicon
Reactive Programming with Kotlin - Quick Intro to Vert.x

Featured ones: