Vert.x Verticle Config

Vert.x Verticle Config

In this tutorial, we are going to discuss about the Vert.x Verticle Config. In the previous tutorial, we have seen Vert.x Verticle Scaling. Now we are going to configure them.

In Vert.x, you can pass configuration data to verticles during deployment using the DeploymentOptions class. This configuration data can be accessed within the verticle to adjust its behavior based on external inputs (like environment variables, JSON configuration files, or custom settings).

Previously, we defined the deployment options when deploying a verticle. The deployment options offer a lot of properties. One very useful property is the config property. With that, a Json object can be passed to a new verticle during initialization and this config option is then available inside the verticle.

Vert.x Verticle Config
How Vert.x Verticle Config Works
  1. You define the configuration data (usually as a JSON object).
  2. You pass the configuration to the verticle when deploying it using DeploymentOptions.
  3. The verticle can access the configuration through the config() method, which returns the configuration as a JsonObject.

So let’s do this in our main verticle.

package com.ashok.vertx.vertx_starter.verticles;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;

/**
 *
 * @author ashok.mariyala
 *
 */
public class MainVerticle extends AbstractVerticle {

  private static final Logger LOG = LoggerFactory.getLogger(MainVerticle.class);

  public static void main(String[] args) {
    final Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(new MainVerticle());
  }

  @Override
  public void start(final Promise<Void> startPromise) throws Exception {
    LOG.debug("Start {}", getClass().getName());
    vertx.deployVerticle(new VerticleA());
    vertx.deployVerticle(new VerticleB());
    vertx.deployVerticle(VerticleN.class.getName(),
      new DeploymentOptions()
        .setInstances(4)
        .setConfig(new JsonObject()
          .put("id", UUID.randomUUID().toString())
          .put("name", VerticleN.class.getSimpleName())
        )
    );
    startPromise.complete();
  }
}

We’re setting a new config, so after the set instances we add dot set config and then we are creating a new Json object. Make sure to use the Json object from io.vertx.core.json package. This is the common way to operate with Json objects in the vertex world. For this Json object we can add some fields with .put. We can define key and value pairs.

We are going to use the ID as key and a UUID.randomUUID().toString() as value and a second value. We are defining a name and the value is the VerticleN.class.getSimpleName().

Then we’re going into the Verticle N class and in the start method we can access this config object.

package com.ashok.vertx.vertx_starter.verticles;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;

/**
 *
 * @author ashok.mariyala
 *
 */
public class VerticleN extends AbstractVerticle {

  private static final Logger LOG = LoggerFactory.getLogger(VerticleN.class);

  @Override
  public void start(final Promise<Void> startPromise) throws Exception {
    LOG.debug("Start {} with config {}", getClass().getName(), config().toString());
    startPromise.complete();
  }
}

Okay, now we’re ready to try it out. So let’s run the main method of the main verticle and see what’s happening.

6:26:32 pm: Executing ':MainVerticle.main()'...
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :MainVerticle.main()
Start com.ashok.vertx.vertx_starter.verticles.MainVerticle
Start com.ashok.vertx.vertx_starter.verticles.VerticleA
Start com.ashok.vertx.vertx_starter.verticles.VerticleB
Start com.ashok.vertx.vertx_starter.verticles.VerticleAA
Start com.ashok.vertx.vertx_starter.verticles.VerticleAB
Deployed com.ashok.vertx.vertx_starter.verticles.VerticleAA
Start com.ashok.vertx.vertx_starter.verticles.VerticleN with config  {"id":"42af3d18-8476-4384-8eda-be8b014e9dbb", "Name":"VerticleN"}
Start com.ashok.vertx.vertx_starter.verticles.VerticleN with config  {"id":"42af3d18-8476-4384-8eda-be8b014e9dbb", "Name":"VerticleN"}
Start com.ashok.vertx.vertx_starter.verticles.VerticleN with config  {"id":"42af3d18-8476-4384-8eda-be8b014e9dbb", "Name":"VerticleN"}
Start com.ashok.vertx.vertx_starter.verticles.VerticleN with config  {"id":"42af3d18-8476-4384-8eda-be8b014e9dbb", "Name":"VerticleN"}
Stop com.ashok.vertx.vertx_starter.verticles.VerticleAA
Deployed com.ashok.vertx.vertx_starter.verticles.VerticleAB

Now we see an output that is printing start vertical N with config and then we see a Json representation inside the Json object. There are two fields ID and name. The name is always vertical N and also the ID is always the same. So we actually didn’t generate different uuids but always the same. With that, we can ensure that each vertical gets the same configuration. So it’s not possible to pass different kind of configs if the vertical is deployed multiple times.

Dynamic Configuration Updates:

You can update a verticle’s configuration dynamically by redeploying it with new settings. Verticles, however, don’t automatically reload their configuration while running unless you implement a custom reloading mechanism.

Summary
  • DeploymentOptions.setConfig() allows you to pass configuration to a verticle during deployment.
  • Configuration data is accessed via the config() method inside the verticle.
  • You can pass configurations as JSON objects and load them from files, environments, or databases.
  • Configuration is useful for adjusting verticle behavior dynamically and ensuring flexible deployments across different environments (e.g., dev, prod).
  • In a clustered environment, you can share configuration across nodes while deploying multiple instances of the verticle.

Using Vert.x Verticle Config, you can decouple application logic from specific environmental settings, making your Vert.x applications more flexible and maintainable.

That’s all about the Vert.x Verticle Config with example. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Vert.x tutorials..!!

Vert.x Verticle Config
Scroll to top