遇到麻烦听Vert.x配置更改

发布于 2025-02-01 02:45:26 字数 986 浏览 1 评论 0原文

我更改了我的config-test.json,但是应用程序未打印“新配置:...”,扫描范围内的打印。

        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);
        });

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);
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じее 2025-02-08 02:45:26

setBeforesCanhandler的Javadoc说:

在每次扫描之前都会注册一个处理程序。此方法主要用于记录目的。

这意味着,在您的情况下,该程序将每五秒钟检查.setscanperiod(5000)中的JSON文件中的更改。如果程序在其中一项检查过程中找到更改,则首先configRetriever.setBeforesCanhandler方法将触发,然后是config> configreTriever.listen

发生更改时消息的顺序是:

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

该程序不会立即注册配置更改,而仅在定期安排的间隔检查中,如setscanperiod javadoc中所述:

在MS中配置扫描周期。这是两个之间的时间数量
检查配置更新。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文