`
sillycat
  • 浏览: 2539171 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    FileMonitor 基于jdk7文件监控WatchService

    《基于JDK7的FileMonitor:精简版文件监控服务详解》 在Java开发中,对文件系统的实时监控是一项常用且重要的任务。JDK7引入了`WatchService`接口,为开发者提供了一种监听文件系统变化的能力。然而,`WatchService...

    FileNotify.rar_CE 2005_Win CE SDK_filenoti_notify _win CE 5

    标题 "FileNotify.rar_CE 2005_Win CE SDK_filenoti_notify_win CE 5" 暗示了这是一个关于Windows CE操作系统中文件通知功能的开发资源包,主要用于Windows CE 5.0版本。这个资源可能包含了在Windows CE平台上实现...

    文件监控 c++

    然后,我们调用ReadDirectoryChangesW函数,设置要监控的事件类型(如FILE_NOTIFY_CHANGE_FILE_NAME、FILE_NOTIFY_CHANGE_DIR_NAME等),并提供一个FILE_NOTIFY_INFORMATION结构体数组来接收变化信息: ```cpp ...

    WM_NOTIFY.zip_WM_NOTI_WM_NOTIFY

    这个压缩包文件`WM_NOTIFY.zip_WM_NOTI_WM_NOTIFY`包含了关于`WM_NOTIFY`消息的深入分析和理解,以及可能的资源链接。我们将详细探讨`WM_NOTIFY`消息的原理、使用场景及其在实际应用中的价值。 `WM_NOTIFY`消息是...

    淘宝消息中间件Notify

    Notify_1.4_truncate.docx Notify_1.7_client_design.doc Notify_1.7_client_develop_guide.docx ... ... Notify1.7_server_design.docx Notify1.7UserGuide.pptx Notify1.8_SEDA.docx Notify2010plan.pptx Notify...

    file_change_notify.rar_Change

    标题中的"file_change_notify.rar_Change"暗示了这个压缩包包含了一个关于文件变更通知的程序或库。描述中提到的“在文件改变时得到通知,更具人性化风格”表明这是一个能够实时监控并提醒用户文件变化的工具,可能...

    Java多线程wait和notify

    在Java中,`wait()` 和 `notify()` 方法是实现线程间通信和协作的重要工具,它们属于 `java.lang.Object` 类,这意味着所有类都默认继承了这两个方法。本文将详细探讨如何使用 `wait()` 和 `notify()` 来控制子线程...

    VC中 ReadDirectoryChangesW实时监视文件操作简单实例

    HANDLE dirHandle = CreateFileW(L"C:\\path\\to\\directory", FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);...

    MFC 全面解读WM_NOTIFY

    ### MFC全面解读WM_NOTIFY 在MFC(Microsoft Foundation Classes)框架中,`WM_NOTIFY`消息扮演着极其关键的角色,特别是在处理控件间通信时。本文将深入解析`WM_NOTIFY`消息的机制、应用场景以及其在MFC中的实现...

    windows系统目录监控代码

    FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE, &dwBytesReturned, &overlapped, NULL)) { _tprintf(_T("Failed to read directory ...

    文件目录监视 附源码

    FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SECURITY); // 监视的事件类型...

    notify.js 好用的提示工具

    notify.js 好用的提示工具

    wait和notify讲解

    wait和notify讲解

    Java 同步方式 wait和notify/notifyall

    这些方法主要用于解决线程等待和唤醒的问题,是基于Java Monitor(监视器)模型的。 首先,我们来看一下`synchronized`关键字。在Java中,`synchronized`用于创建临界区,确保同一时间只有一个线程能够执行特定代码...

    VB 監視文件 源码

    hChange = FindFirstChangeNotification(path, False, FILE_NOTIFY_CHANGE_FILE_NAME Or FILE_NOTIFY_CHANGE_DIR_NAME) If hChange <> -1 Then Do If Not FindNextChangeNotification(hChange) Then ' 处理文件...

    活动日志监控log-monitor.zip

    $notify = new \Jenner\LogMonitor\Notification\EchoNotification(); $process = new \Jenner\LogMonitor\MonitorTask($reader, $filter, $notify); $process->run(); $process->wait(); 标签:log...

    4.20170706_从机自动使能notify.zip

    "自动使能 notify" 是一种功能,它涉及自动化流程和通知机制。在20170706这个日期点,可能是一个系统或软件更新,或者一个特定项目的一部分,其中从机被配置为能够自动启用通知功能。这可能是为了提高效率,减少人工...

    Laravel开发-laravel-notify

    在Laravel框架中,`laravel-notify`是一个非常实用的扩展包,它为开发者提供了方便的通知接口,以便在Laravel4项目中实现各种通知功能。这个包的主要目的是简化通知的创建、管理和展示,使开发者可以更加专注于业务...

    bootstrap-notify.js

    bootstrap-notify.js bootstrap的提示框插件

    IBM Thread And Monitor Dump Analyser

    【IBM Thread And Monitor Dump Analyser】是一款专门用于分析Java应用程序线程状态的工具,它能够帮助开发者深入理解程序在特定时刻的运行情况,特别是在遇到性能问题或者系统挂起时,提供宝贵的诊断信息。...

Global site tag (gtag.js) - Google Analytics