Skip to content

Vertx Config

DodaTech 1 min read

In this tutorial, you'll learn about Fix Vert.x Configuration Not Loading. We cover key concepts, practical examples, and best practices.

The Problem

Vert.x configuration from files or environment variables is not loaded.

Quick Fix

Use ConfigRetriever

Wrong:

// No config retriever
String port = System.getenv("PORT");

Output:

Manual config

Right:

ConfigRetriever retriever = ConfigRetriever.create(vertx);
retriever.getConfig()
    .onSuccess(config -> {
        int port = config.getInteger("http.port", 8080);
    });

Output:

Config loaded from file/env

Add config store

Wrong:

retriever.getConfig();
// No store configured

Output:

Empty config

Right:

ConfigStoreOptions fileStore = new ConfigStoreOptions()
    .setType("file")
    .setFormat("yaml")
    .setConfig(new JsonObject().put("path", "conf/config.yaml"));
retriever = ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(fileStore));

Output:

Config loaded from YAML

Use environment variables

Wrong:

// No env config
String dbUrl = config.getString("db.url");

Output:

Missing value

Right:

ConfigStoreOptions envStore = new ConfigStoreOptions()
    .setType("env");
retriever = ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(envStore));

Output:

Env variables loaded

Prevention

  • Use ConfigRetriever for dynamic configuration
  • Configure file, env, and sys properties stores
  • Use ConfigRetrieverOptions to add multiple stores

Common Mistakes with config

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world VERTX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What config store types does Vert.x support?

File, environment variables, system properties, HTTP, Redis, and more.

This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro