Java 使用commons配置自动重新加载属性文件无效
我有一个经常更新的属性文件。为了自动重新加载文件,我使用了commons配置,但更改不会立即反映在属性文件中。我只能在重新启动服务器后才能看到更改,这意味着自动重新加载不起作用。我已经包括了所有需要的jar文件Java 使用commons配置自动重新加载属性文件无效,java,apache,Java,Apache,我有一个经常更新的属性文件。为了自动重新加载文件,我使用了commons配置,但更改不会立即反映在属性文件中。我只能在重新启动服务器后才能看到更改,这意味着自动重新加载不起作用。我已经包括了所有需要的jar文件 PropertiesConfiguration property = null; try { property = new PropertiesConfiguration(PROPERTY_FILENAME); property.setReload
PropertiesConfiguration property = null;
try {
property = new PropertiesConfiguration(PROPERTY_FILENAME);
property.setReloadingStrategy(new FileChangedReloadingStrategy());
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您必须提供文件的完整路径,否则它将检测不到任何更改。如果该文件存在于类路径中,并且您只提供了文件名,则将加载该文件,但不会检测到任何更改;您甚至可以删除该文件,应用程序将继续运行,就像什么都没有发生一样
- 不检测更改。删除文件而不停止应用程序
public class PropStandaloneConfigurationTest { public static void main(String[] args) throws ConfigurationException, InterruptedException { final PropertiesConfiguration config = new PropertiesConfiguration("app-project.properties"); config.setListDelimiter(','); config.setAutoSave(true); FileChangedReloadingStrategy reload = new FileChangedReloadingStrategy(); reload.setRefreshDelay(2000L); config.setReloadingStrategy(reload); Provider<Integer> cacheRetriesProvider = new Provider<Integer>() { @Override public Integer get() { return config.getInt("cache.retries"); } }; while (true) { System.out.println(cacheRetriesProvider.get()); Thread.sleep(3000L); } } }
...
final PropertiesConfiguration config = new PropertiesConfiguration("/usr/local/ws/app-project/src/test/resources/app-project.properties");
...