Having trouble listen vert.x config change

Viewed 52

I changed my config-test.json,but application did not print "new config:...",the before scanhander has print .

        JsonObject jsonConfig = new JsonObject();
        jsonConfig.put("path", "test.json");
        ConfigStoreOptions config = new ConfigStoreOptions();
        config.setType("file").setOptional(true).setConfig(jsonConfig);
        
        ConfigRetrieverOptions options =
                new ConfigRetrieverOptions().addStore(config).setScanPeriod(5000);
        ConfigRetriever configRetriever = ConfigRetriever.create(vertx, options);
        configRetriever.setBeforeScanHandler(h -> {
            System.out.println("config:" + configRetriever.getCachedConfig());
        });
        configRetriever.listen(change -> {
            JsonObject newConfiguration = change.getNewConfiguration();
            System.out.println("new config:" + newConfiguration);
            JsonObject old = change.getPreviousConfiguration();
            System.out.println("old config:" + old);
        });
1 Answers

The Javadoc of setBeforeScanHandler says:

Registers a handler called before every scan. This method is mostly used for logging purpose.

Which means that in your case the program will check for changes in the JSON file every five seconds as specified in .setScanPeriod(5000). If the program finds the change during one of those checks, first the configRetriever.setBeforeScanHandler method will trigger, followed by configRetriever.listen.

The order of messages when there is a change will be:

config : {...} 
new config:{...} 
old config:{...}

The program does not register the config change immediately, but only on the regularly scheduled interval checks, as is stated in the setScanPeriod JavaDoc:

Configures the scan period, in ms. This is the time amount between two checks of the configuration updates.

Related