JNotify

JNotify,一个支持动态监控文件和文件夹(支持级联监控)的架包。在linux系统中,调用的是linux底层的inotify服务,只是添加了对子文件夹级联监控的功能。在windows中,需要添加附件的dll文件,因为windows默认没有该服务,这是大拿们自己开发的一个功能。 

使用很简单,以我的ubuntu系统为例: 
1,将jnotify包引入到工程中。 
2,将jnotify依赖的so文件加入到java.library.path路径中。这个变量可能会有多个位置,随便将jnotify压缩包中附带的libjnotify.so文件加入到其中的任何一个路径中即可。如果不知道这个变量的值,可以使用System.getProperty("java.library.path")查看。当然,如果不想这么麻烦,可以在启动程序时指定JVM的参数
Java代码   收藏代码
  1. -Djava.library.path=你的位置  
,这样和上面将so文件加入系统路径中是一样的效果。 

然后,写个测试类就可以看见效果了。 
我这里没有自己写,只是简单的拷贝了一下JNotify官网的测试代码。 
Java代码   收藏代码
  1. public class JnotifyTest {  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             new JnotifyTest().sample();  
  5.         } catch (Exception e) {  
  6.             e.printStackTrace();  
  7.         }  
  8.         // System.out.println(System.getProperty("java.library.path"));  
  9.     }  
  10.   
  11.     public void sample() throws Exception {  
  12.         // path to watch  
  13.         String path = System.getProperty("user.home");  
  14.   
  15.         // watch mask, specify events you care about,  
  16.         // or JNotify.FILE_ANY for all events.  
  17.         int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED  
  18.                 | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;  
  19.   
  20.         // watch subtree?  
  21.         boolean watchSubtree = true;  
  22.   
  23.         // add actual watch  
  24.         int watchID = JNotify  
  25.                 .addWatch(path, mask, watchSubtree, new Listener());  
  26.   
  27.         // sleep a little, the application will exit if you  
  28.         // don't (watching is asynchronous), depending on your  
  29.         // application, this may not be required  
  30.         Thread.sleep(1000000);  
  31.   
  32.         // to remove watch the watch  
  33.         boolean res = JNotify.removeWatch(watchID);  
  34.         if (!res) {  
  35.             // invalid watch ID specified.  
  36.         }  
  37.     }  
  38.   
  39.     class Listener implements JNotifyListener {  
  40.         public void fileRenamed(int wd, String rootPath, String oldName,  
  41.                 String newName) {  
  42.             print("renamed " + rootPath + " : " + oldName + " -> " + newName);  
  43.         }  
  44.   
  45.         public void fileModified(int wd, String rootPath, String name) {  
  46.             print("modified " + rootPath + " : " + name);  
  47.         }  
  48.   
  49.         public void fileDeleted(int wd, String rootPath, String name) {  
  50.             print("deleted " + rootPath + " : " + name);  
  51.         }  
  52.   
  53.         public void fileCreated(int wd, String rootPath, String name) {  
  54.             print("created " + rootPath + " : " + name);  
  55.         }  
  56.   
  57.         void print(String msg) {  
  58.             System.err.println(msg);  
  59.         }  
  60.   
  61.     }  
  62. }  


在实际的使用过程中,如果是web工程,我的习惯是添加一个listener监听器,当监听器初始化时,添加对指定文件或文件夹的监控。这样我们就不必为每次修改了配置文件都需要重启工程而苦恼了。如果是Java工程,就是需要的地方添加监控吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值