Monitor Directory and File(I)Notify

Monitor Directory and File(I)Notify

1. Install inotify on Ubuntu
>sudo apt-get install Inotify-tools

2. Install inotify on redhat
http://inotify-tools.sourceforge.net/
>wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
>tar zxvf inotify-tools-3.14.tar.gz
>cd inotify-tools-3.14
>./configure --prefix=/opt/tools/inotify
>make
>make install

3. we need copy some dll or so to our directory.
find the directory of java.library.path
Put these codes in Java Application and note the directory
System.getProperty("java.library.path")

/usr/lib/jvm/java-6-sun-1.6.0.24/jre/lib/i386/server:
/usr/lib/jvm/java-6-sun-1.6.0.24/jre/lib/i386:
/usr/lib/jvm/java-6-sun-1.6.0.24/jre/../lib/i386:
/usr/lib/jvm/java-6-sun-1.6.0.24/jre/lib/i386/client:
/usr/lib/jvm/java-6-sun-1.6.0.24/jre/lib/i386:
/usr/lib/xulrunner-addons:
/usr/lib/xulrunner-addons:
/usr/lib/xulrunner-addons:
/usr/java/packages/lib/i386:
/lib:
/usr/lib

>sudo cp libjnotify.so /usr/lib/

or we can solve this in another way, at the starting of the java application
java -Djava.library.path=some/folder/path/contain/dll

4. Some test classes
The main test class CategoryNotify.java:
package com.xxxxx.importdata.jnotify;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
public class CategoryNotify {
public static void main(String[] args) {
// path to watch
String path = "/home/luohua/life/work/xxxxx/xxxxx-gen/records/";
// System.out.println(System.getProperty("java.library.path"));
// watch mask, specify events you care about,
// or JNotify.FILE_ANY for all events.
// int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
// | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
int mask = JNotify.FILE_MODIFIED;
// watch subtree?
boolean watchSubtree = true;
// add actual watch
int watchID = 0;
try {
watchID = JNotify.addWatch(path, mask, watchSubtree,
new CategoryListener());
} catch (JNotifyException e) {
e.printStackTrace();
}
// sleep a little, the application will exit if you
// don't (watching is asynchronous), depending on your
// application, this may not be required
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// to remove watch the watch
boolean res = false;
try {
res = JNotify.removeWatch(watchID);
} catch (JNotifyException e) {
e.printStackTrace();
}
if (!res) {
// invalid watch ID specified.
}
}
}

The listener class CategoryListener.java:
package com.xxxxxx.importdata.jnotify;
import net.contentobjects.jnotify.JNotifyListener;
public class CategoryListener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName,
String newName) {
print(wd + "-renamed " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print(wd + "-modified " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print(wd + "-deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print(wd + "-created " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}

And the outputs are as follow:
0-modified records : 2011_06_22/15/15_57_52_5438.txt
0-modified records : 2011_06_22/15/15_57_52_5438.txt/
0-modified records : 2011_06_22/15/15_57_53_2702.txt
0-modified records : 2011_06_22/15/15_57_53_2702.txt/


references:
http://www.iteye.com/topic/1096698
http://hi.baidu.com/lylianyu/blog/item/a26f5dec7e1b16c62e2e2110.html
http://www.blogjava.net/quaff/archive/2006/03/02/33229.html
http://www.java3z.com/cwbwebhome/article/article5/5843.html
http://commons.apache.org/io/apidocs/org/apache/commons/io/monitor/FileAlterationObserver.html

inotify
http://hi.baidu.com/johntech/blog/item/e4a31a3db1ee1ce755e723f4.html
http://www.ibm.com/developerworks/cn/linux/l-inotifynew/
jnotify
http://jnotify.sourceforge.net/
http://whitesock.iteye.com/blog/431147
http://varsoft.iteye.com/blog/873622

你可能感兴趣的:(java)