Best practice to configure Vert.x Verticle when configuration is more complex than a JsonObject?

Viewed 28

What is the best practice to configure Verticles by class (no-arg constructor-type) when the configuration of the Verticle is quite complicated (needs DAO, gRPC stubs etc) that goes beyond the provided JsonObject.

class MyVerticle extends AbstractVerticle {
    public MyVerticle() {
        //no-args constructor so can be deployed by class
    }
    @Override
    void start() {
        // Access to a JSON object is provided by the super class
        // can get String: e.g. HTTP listen host
        // can get Integer: e.g. HTTP listen port
        // but I need more complex arguments like DAO,
        // REST endpoints, gRPC stubs...
        JsonObject options = config();
    }

}

public class Launcher {

    public void static main(String[] args) {
        
        // lots of initialization here
        // DAO, gRPC client stubs, REST clients
        Vertx root = Vertx.vertx();
        JsonObject options = new JsonObject();
        // can put String, Integer into options
        root.deployVerticle(
            MyVerticle.class,
            new DeploymentOptions().setInstances(16).setConfig(options)
        );
    }
}

If the configuration can be expressed as Strings and Integers, it seems straightforward to do but what about more complex objects? I tried putting various Objects as values but of course that failed during a io.vertx.core.json.impl.JsonUtil.deepCopy().

I suppose I could create a static ConcurrentHashMap<String. Object> somewhere, and store complex configuration keyed by a deploymentId String. Then in the Verticle config I could pass each deployment a lookup String to find the needed objects...but this seems like a horribly "global" way of managing all these Verticles.

1 Answers

Found the answer deeper inside the javadocs: use the version of deployVerticle which takes a Supplier<Verticle> as an argument:

// this factory function can provide
// instances that need complex initialisation
deployVerticle(Supplier<Verticle>, ...)
Related