Best practice to set VertxOptions in a class that extends AbstractVerticle

1,065 views
Skip to first unread message

Franco Rondini

unread,
Dec 31, 2017, 4:05:39 AM12/31/17
to vert.x

coding my customizations on the scaffold generated by **developers.redhat.com/launch** using **Eclipse Vert.x 3.4.2**  . 

I think the proposed approach is quite a *classic*  but I do not understand what is the best practice  to set `VertxOptions` in `public class HttpApplication extends AbstractVerticle {` 

What I'm trying:

<!-- language: java -->    

    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.VertxOptions;

    public class HttpApplication extends AbstractVerticle {
      protected final int DEFAULT_ADDRESS_RESOLUTION_TIMEOUT = 8000;
      @Override
      public void start(Future<Void> future) {
        //  [snip] Create a router object and routes
        // my custom options code start-here:[
        VertxOptions options = new VertxOptions()
                    .setWarningExceptionTime(1500000000)
                    .setAddressResolverOptions(new AddressResolverOptions()
                            .addServer("192.168.0.1")
                            .addServer("192.168.2.1")
                            .setMaxQueries(5)
                            .setCacheNegativeTimeToLive(0) // discard failed DNS lookup results immediately
                            .setCacheMaxTimeToLive(0) // support DNS based service resolution
                            .setRotateServers(true)
                            .setQueryTimeout(DEFAULT_ADDRESS_RESOLUTION_TIMEOUT));
    
        Vertx vertx = Vertx.vertx(options);
        // my custom options code:  end-here:]
    
        // Create the HTTP server and pass the "accept" method to the request handler.
        vertx
            .createHttpServer()
            .requestHandler(router::accept)
            .listen(
    

in particular I am very doubtful whether it is correct or not to get a vertx in `Vertx vertx = Vertx.vertx(options);` while the gerated code simply uses the *Reference to the Vert.x instance that deployed this verticle* provided by the abstract base class `AbstractVerticle`.

It's my approach safe?  has it contraindications or risks breaking something? Is there a better practice to setup my custom `VertxOptions` in `HttpApplication`? 

PS: I've just realized that  asking this group is the best way to get help yesterday , btw yesterday I opened the same question on s.o.: [Best practice to set VertxOptions in a class that extends AbstractVerticle](https://stackoverflow.com/q/48032726/1657028) thanks in advance for any advice.

Jez P

unread,
Dec 31, 2017, 4:12:48 AM12/31/17
to vert.x
If you're starting from the command line you should set the options with system properties, i think https://groups.google.com/forum/#!topic/vertx/D5ZX8D33hmY (see Tim Fox's replies). In your case it may make the command line quite long. 

Alternatively you could override Launcher to have it set the options 

I don't think you want to create different Vertx instances, which is what will happen when you use Vertx.vertx()

Grey Seal

unread,
Jan 2, 2018, 2:21:53 AM1/2/18
to vert.x
Try creating a Launcher class and extend VertxCommandLauncher implementing VertxLifecycleHooks. Then you can re-set vertxOptions inside the method beforeStartingVertx(VertxOptions options)

Franco Rondini

unread,
Jan 4, 2018, 1:00:28 PM1/4/18
to vert.x
Thanks for the suggestions, in my use case the implementation of Laucher is the choice. I am therefore leaning and coding this way that seems to work perfectly ( I'm still testing )

Franco Rondini

unread,
Jan 4, 2018, 1:04:56 PM1/4/18
to vert.x
Thanks for the examples , I'm evaluating the option of VertxCommandLauncher implementing VertxLifecycleHooks ,  so far so good  ;) 
Reply all
Reply to author
Forward
0 new messages