监听配置文件的更新:commons-vfs
VFS
The main entry point for the VFS. Used to create {@link FileSystemManager} instances.
FileSystemManager
A FileSystemManager manages a set of file systems. This interface is used to locate a {@link FileObject} by name from one of those file systems.
A file system manager can recognise several types of file names
Absolute URI(file:/c:/somefile)
Absolute local file name(/home/someuser/a-file)
Relative path(../somefile)
FileSystemManager fsManager = VFS.getManager();
FileObject directoryObject = fsManager.resolveFile(absoluteDirectory.toURI());
DefaultFileMonitor fileMonitor = new DefaultFileMonitor(new VfsFileListener(listener));
fileMonitor.addFile(directoryObject);
fileMonitor.setRecursive(this.recursive);
if (started) {
System.out.println("started");
fileMonitor.start();
}
监听配置文件的更新:commons-io
可以用于实现配置文件的热部署
功能由三个组件组成:
- 监听器 FileAlterationListener
用于实现文件改变时触发的行为。
- 观察者 FileAlterationObserver
用于观察文件的改变,通知注册的监听器执行相应的事件。
- 监视器 FileAlterationMonitor
通过一线程,每间隔一段时间调用一次注册的观察者检查文件。
FileAlterationListener
public class FileAlterationReload extends FileAlterationListenerAdaptor {
@Override
public void onFileChange(File file) {
System.out.println("文件改变了:"+file.getName());
}
@Override
public void onDirectoryChange(File directory) {
System.out.println("onDirectoryChange:"+directory.getName());
}
}
FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(new File(System.getProperty("user.dir")+"\\src\\test\\properties"));
FileAlterationListener fileAlterationListener =new FileAlterationReload();
//注册监听器
fileAlterationObserver.addListener(fileAlterationListener);
FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor();
//注册观察者
fileAlterationMonitor.addObserver(fileAlterationObserver);
//启动监听
try {
fileAlterationMonitor.start();
System.out.println("開始監聽");
//让主线程别这么快结束。
Thread.sleep(1000000);
} catch (Exception e) {
e.printStackTrace();
}
动态更新运行环境信息-Introspector
//找到field 的setter方法
BeanInfo beanInfo = Introspector.getBeanInfo(getClass(), Object.class);
System.out.println("findSetter-fieldLength:"+beanInfo.getPropertyDescriptors().length);
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
if (propertyDescriptor.getName().equals(fieldName)) {
method = propertyDescriptor.getWriteMethod();
break;
}
return method;
//更新field的value
Method method = findSetter(fieldName);
if (method != null) {
if (newValue != null && !newValue.getClass().equals(method.getParameterTypes()[0])) {
newValue = JSON.parseObject(newValue.toString(), method.getParameterTypes()[0]);
}
method.setAccessible(true);
method.invoke(this, newValue);
success = true;
else {
Field field = ReflectionUtils.findField(this.getClass(), fieldName);
if (field != null && (isPrimitive(field.getType()) || field.getType().equals(String.class))) {
field.setAccessible(true);
if (newValue != null && !newValue.getClass().equals(field.getType())) {
newValue = JSON.parseObject(newValue.toString(), field.getType());
}
field.set(this, newValue);
success = true;
后续再根据业务重置服务
----待续