java 监听jnotify.dll_java实现文件实时监控:Jnotify和FileAlteration

  • Post author:
  • Post category:java


一、实时监控使用场景

(a)实时刷新文件缓存,比如页面图片

(b)实时刷新工程配置文件

(c)实时同步文件到镜像路径,比如与rsync结合,实现跨平台实时同步

二、Jnotify官网地址:Jnotify

JNotify is a java library that allow java application to listen to file system events, such as:File created;File modified;File renamed;File deleted

它能让java程序监听文件系统的事件,如:文件(夹)创建,文件(夹)修改,文件(夹)重命名,文件(夹)删除。

支持Linux/Windows/MacOS,由于使用JNI调用了本地库所以支持的的并不完美。

Win下使用:要在java.library.path下加入依赖的dll (jnotify.dll/jnotify_64bit.dll)。

Linux下使用:要在java.library.path下加入依赖的so(libjnotify.so)。

特别说明:

0.94版:仅仅支持GLIBC库版本在2.1.2之上的,它的实现基于inotify本地库。32位,64位都支持。

0.93版:支持GLIBC库版本在2.1.2之下的,但仅仅支持32位,64位的so库为提供。0.92同0.93。

所以Jnotify还无法跨平台。但是使用起来还是很简单的。

看代码,代码注释有详细的说明:

public class JnotifyTest {

public static void main(String[] args) {

try {

new JnotifyTest().test();

} catch (Exception e) {

e.printStackTrace();

}

}

public void test() throws Exception {

//监控路径

String path = “E:\\test\\rsync\\base”;

//可随意组合监听事件类型

int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED

| JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;

//是否监控子目录

boolean watchSubtree = false;

//添加监听,返回监听唯一标示

int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());

System.out.println(watchID);

//为啥睡眠?如果不睡眠,程序运行结束,监听就被移除 (Common-io的FileAlterationMonitor会继续运行不休眠)

Thread.sleep(1000000);

//根据watchID手动移除监听

boolean res = JNotify.removeWatch(watchID);

if (!res) {

//返回FALSE,监听标识无效

}

}

//可以在下面的监控方法中添加自己的代码。比如在fileModified中添加重新加载配置文件的代码

//可以结合rsync,实现实时同步文件

class Listener implements JNotifyListener {

public void fileRenamed(int wd, String rootPath, String oldName,

String newName) {

print(“renamed ” + rootPath + ” : ” + oldName + ” -> ” + newName);

}

public void fileModified(int wd, String rootPath, String name) {

print(“modified ” + rootPath + ” : ” + name);

}

public void fileDeleted(int wd, String rootPath, String name) {

print(“deleted ” + rootPath + ” : ” + name);

}

public void fileCreated(int wd, String rootPath, String name) {

print(“created ” + rootPath + ” : ” + name);

}

void print(String msg) {

System.err.println(msg);

}

}

}

三、FileAlterationMonitor

commons-io-2.2新出工具类,在monitor包下。功能类似Jnotify,不过是纯java实现,完美跨平台。

看代码,代码注释有详细的说明:

monitor工具类:

public class FileMonitorUtil {

public static FileAlterationMonitor addMonitor(String path){

FileAlterationMonitor monitor = new FileAlterationMonitor(100);//监控器

FileAlterationObserver observer = new FileAlterationObserver(path);//对某个路径的观察者

observer.addListener(new PushFileAlterationListener(path));//添加监听事件响应,与path无关,rsync同步需要

monitor.addObserver(observer);//将观察者添加到监控器

try {

monitor.start();//启动

} catch (Exception e) {

e.printStackTrace();

}

return monitor;//便于停止

}

public static void main(String[] args) {

String path = “E:\\test”;

FileAlterationMonitor monitor = FileMonitorUtil.addMonitor(path);

try {

Thread.sleep(10000);//如果不加休眠和停止操作,monitor将会一直监听

monitor.stop();//停止监控

} catch (Exception e) {

e.printStackTrace();

}

}

}监听响应类:实现FileAlterationListener接口,大家可以按需实现。

public class PushFileAlterationListener implements FileAlterationListener{

private String rootpath = “”;

public PushFileAlterationListener(String rootpath) {

this.rootpath = rootpath;

}

@Override

public void onDirectoryChange(File file) {

}

@Override

public void onDirectoryCreate(File file) {

RsyncQueue.addJob(new RsyncJob(rootpath, file.getParent(), “0”));//Rsync同步队列

System.out.println(“Directory create: ” + file.getAbsolutePath()+” “+file.getParent());

}

@Override

public void onDirectoryDelete(File file) {

RsyncQueue.addJob(new RsyncJob(rootpath, file.getParent(), “0”));

System.out.println(“Directory delete: ” + file.getAbsolutePath()+” “+file.getParent());

}

@Override

public void onFileChange(File file) {

RsyncQueue.addJob(new RsyncJob(rootpath, file.getParent(), “0”));

System.out.println(“File change: ” + file.getAbsolutePath()+” “+file.getParent());

}

@Override

public void onFileCreate(File file) {

RsyncQueue.addJob(new RsyncJob(rootpath, file.getParent(), “0”));

System.out.println(“File created: ” + file.getAbsolutePath()+” “+file.getParent());

}

@Override

public void onFileDelete(File file) {

RsyncQueue.addJob(new RsyncJob(rootpath, file.getParent(), “0”));

System.out.println(“File deleted: ” + file.getAbsolutePath()+” “+file.getParent());

}

@Override

public void onStart(FileAlterationObserver filealterationobserver) {

System.out.println(“start”);

}

@Override

public void onStop(FileAlterationObserver filealterationobserver) {

System.out.println(“end”);

}

}注:

1、public FileAlterationMonitor():Construct a monitor with a default interval of 10 seconds。默认响应时间为10s,及你添加文件后可能要过10s你才接到通知。所以一般使用带时间参数的构造器(单位毫秒)。

2、也可以通过继承FileAlterationListenerAdaptor来实现监听,目前该类为空实现。两种方式各有优劣。

3、父目录的下的文件变动时,会触发2个事件:a,子文件FileChange;b,父目录的DirectoryChange事件;

4、重命名文件(夹),会同时触发:FileCreate和FileDelete

5、如果拷贝某个工程时,工程内的每个文件都会触发事件,所以队列添加事件的算法需要好好设计。

6、windows系统,文件拷贝过程不触发事件,拷完后触发FileChange,Linux,拷贝过程一直触发FileChange

总结:由于在window2003等系统,在拷贝文件结束后也不触发FileChange,所以要真正跨平台,还得结合Jnotify和FileAlteration

O(∩_∩)O哈哈~,等写《rsync和FileAlteration组合实现跨平台发布工程》的时候我再上代码。



版权声明:本文为weixin_42376671原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。