遇到麻烦听Vert.x配置更改
我更改了我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setBeforesCanhandler
的Javadoc说:这意味着,在您的情况下,该程序将每五秒钟检查
.setscanperiod(5000)
中的JSON文件中的更改。如果程序在其中一项检查过程中找到更改,则首先configRetriever.setBeforesCanhandler
方法将触发,然后是config> configreTriever.listen
。发生更改时消息的顺序是:
该程序不会立即注册配置更改,而仅在定期安排的间隔检查中,如
setscanperiod
javadoc中所述:The Javadoc of
setBeforeScanHandler
says: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 theconfigRetriever.setBeforeScanHandler
method will trigger, followed byconfigRetriever.listen
.The order of messages when there is a change will be:
The program does not register the config change immediately, but only on the regularly scheduled interval checks, as is stated in the
setScanPeriod
JavaDoc: