每当这些文件发生任何更改时,它们都会自动刷新 -这是大多数应用程序中常见的非常普遍的问题。每个应用程序都有一些配置,预期该配置文件中的每次更改都会刷新。解决该问题的过去方法包括使用Thread
,根据配置文件的“ 最后更新时间戳 ” 定期轮询文件更改。
现在使用Java 7,情况已经改变。Java 7引入了一项出色的功能:WatchService。我将尽力为您解决上述问题。这可能不是最好的实现,但是肯定会为您的解决方案提供一个很好的开始。我敢打赌!
1.Java WatchService API
A WatchService
是JDK的内部服务,它监视注册对象的更改。这些注册的对象必须是Watchable接口的实例。在向中注册可监视实例时WatchService
,我们需要指定我们感兴趣的变更事件的类型。
到目前为止,有四种类型的事件:
- ENTRY_CREATE,
- ENTRY_DELETE,
- ENTRY_MODIFY, and
- OVERFLOW.
WatchService
interface扩展了Closeable接口,表示可以在需要时关闭服务。通常,应该使用JVM提供的关闭挂钩来完成。
2.应用程序配置提供程序
配置提供程序只是用于包装java,util.Properties实例中的属性集的包装器。它还提供了使用KEY来获取配置属性的方法。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ApplicationConfiguration {
private final static ApplicationConfiguration INSTANCE = new ApplicationConfiguration();
public static ApplicationConfiguration getInstance() {
return INSTANCE;
}
private static Properties configuration = new Properties();
private static Properties getConfiguration() {
return configuration;
}
public void initilize(final String file) {
InputStream in = null;
try {
in = new FileInputStream(new File(file));
configuration.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getConfiguration(final String key) {
return (String) getConfiguration().get(key);
}
public String getConfigurationWithDefaultValue(final String key,
final String defaultValue) {
return (String) getConfiguration().getProperty(key, defaultValue);
}
}
3.配置更改侦听器– File Watcher
现在,当我们有了配置属性的内存中高速缓存的基本包装器时,我们需要一种机制,只要存储在文件系统中的配置文件发生更改,就可以在运行时重新加载此高速缓存。
我已经写了一个示例工作代码来为您提供帮助:
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class ConfigurationChangeListner implements Runnable {
private String configFileName = null;
private String fullFilePath = null;
public ConfigurationChangeListner(final String filePath) {
this.fullFilePath = filePath;
}
public void run() {
try {
register(this.fullFilePath);
} catch (IOException e) {
e.printStackTrace();
}
}
private void register(final String file) throws IOException {
final int lastIndex = file.lastIndexOf("/");
String dirPath = file.substring(0, lastIndex + 1);
String fileName = file.substring(lastIndex + 1, file.length());
this.configFileName = fileName;
configurationChanged(file);
startWatcher(dirPath, fileName);
}
private void startWatcher(String dirPath, String file) throws IOException {
final WatchService watchService = FileSystems.getDefault()
.newWatchService();
Path path = Paths.get(dirPath);
path.register(watchService, ENTRY_MODIFY);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
watchService.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
WatchKey key = null;
while (true) {
try {
key = watchService.take();
for (WatchEvent<?> event : key.pollEvents()) {
if (event.context().toString().equals(configFileName)) {
configurationChanged(dirPath + file);
}
}
boolean reset = key.reset();
if (!reset) {
System.out.println("Could not reset the watch key.");
break;
}
} catch (Exception e) {
System.out.println("InterruptedException: " + e.getMessage());
}
}
}
public void configurationChanged(final String file) {
System.out.println("Refreshing the configuration.");
ApplicationConfiguration.getInstance().initilize(file);
}
}
上面的类是使用线程创建的,该线程将使用侦听配置属性文件的更改WatchService
。
一旦检测到文件中的任何修改,它便会刷新配置中的内存缓存。
上述侦听器的构造函数仅采用一个参数,即受监视的配置文件的标准路径。Listener
在文件系统中更改配置文件时,将立即通知类。
然后,此侦听器类调用ApplicationConfiguration.getInstance().initilize(file);
以重新加载到内存缓存中。
4.测试我们的代码
现在,当我们准备好上课时,我们将对其进行测试。
首先,将test.properties
具有以下内容的'C:/Lokesh/temp'
文件存储在文件夹中。
TEST_KEY = TEST_VALUE
现在,让我们使用以下代码测试以上类。
public class ConfigChangeTest {
private static final String FILE_PATH = "C:/Lokesh/temp/test.properties";
public static void main(String[] args) {
ConfigurationChangeListner listner = new ConfigurationChangeListner(
FILE_PATH);
try {
new Thread(listner).start();
while (true) {
Thread.sleep(2000l);
System.out.println(ApplicationConfiguration.getInstance()
.getConfiguration("TEST_KEY"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//Output of the above program (Change the TEST_VALUE to TEST_VALUE1 and TEST_VALUE2 using any file editor and save).
Refreshing the configuration.
TEST_VALUE
TEST_VALUE
TEST_VALUE
Refreshing the configuration.
TEST_VALUE1
Refreshing the configuration.
TEST_VALUE2
以上输出显示,每次我们对属性文件进行任何更改时,加载的属性都会刷新,并且可以使用新的属性值。到目前为止,已经做好了!
5.重要说明
- 如果在新项目中使用Java 7,并且没有在使用老式方法重新加载属性,则说明操作不正确。
- WatchService提供两种方法take()和poll()。在
take()
方法等待下一个更改发生并被阻止之前,请poll()
立即检查更改事件。如果上次poll()
调用没有任何变化,它将返回null。poll()
方法不会阻止执行,因此应在具有一定睡眠时间的线程中调用该方法。
在评论区域中将您的问题/建议交给我。
学习愉快!