java JNotify (基于内核)实时监控文件

 JNotify 下载地址 

jnotify(包含dll与so).rar

win 将dll文件放入  jdk bin目录下

linux 将so文件放入  jdk bin目录下

maven增加JNotify 依赖

		<dependency>
			<groupId>net.contentobjects.jnotify</groupId>
			<artifactId>jnotify</artifactId>
			<version>0.94</version>
		</dependency>

工具编写

首先在D盘下创建test文件夹

然后执行以下程序

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.concurrent.atomic.AtomicInteger;

public class Jnotify {

    public static void main(String[] args) throws Exception {

        // 设置监听的文件夹,或者文件
        String linstenerPath = "D:" + File.separator + "test";

        // 指定监听的模式,创建、删除、修改、和重命名
        int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;

        // 是否监控子目录
        boolean watchSubtree = true;

        // 设置监听器,得到一个监听器id,可以通过这个id来移除监听器
        int watchId = JNotify.addWatch(linstenerPath, mask, watchSubtree, new MyJnotifyListner());

        // 如果需要去掉监听器,比如不需要监听某个目录了
        //JNotify.removeWatch(watchId);

        //启动线程,不断的创建、重命名和删除文件,测试jnotify的响应速度
        new DynamicBuildFile(linstenerPath).start();

        // 一定要让程序等待,只要程序不退出,那么上面的监听器就一直有效
        Thread.sleep(Integer.MAX_VALUE);
    }

    static class MyJnotifyListner implements JNotifyListener {
        AtomicInteger integer = new AtomicInteger(0);

        @Override
        public void fileCreated(int i, String rootPath, String name) {
            System.out.println("创建文件 : " + rootPath + "/" + name + ";integer:" + integer.incrementAndGet());
        }

        @Override
        public void fileDeleted(int i, String rootPath, String name) {
            System.out.println("删除文件 : " + rootPath + "/" + name + ";integer:" + integer.incrementAndGet());
        }

        @Override
        public void fileModified(int i, String rootPath, String name) {
            System.out.println("更改文件 : " + rootPath + "/" + name + ";integer:" + integer.incrementAndGet());
        }

        @Override
        public void fileRenamed(int i, String rootPath, String oldName, String newName) {
            System.out.println("重命名文件 : " + (rootPath + "/" + oldName) + ";新文件名 : " + (rootPath + "/" + newName) + "integer:" + integer.incrementAndGet());
        }
    }

    /**
     * 动态生成和删除文件,测试jnotify的响应速度
     * 总结
     * 1、create 只会触发create
     * 2、delete 会触发 delete和modified
     * 3、rename 会出发一次rename 和modified
     * 4、如果编辑那个文件,那么会先产生一个临时文件(create),然后modified,在然后rename
     */
    static class DynamicBuildFile extends Thread {
        String basePath;

        DynamicBuildFile(String basePath) {
            this.basePath = basePath;
        }

        @Override
        public void run() {
            while (true) {
                File file1 = new File(basePath + File.separator + "test1.txt");
                File file2 = new File(basePath + File.separator + "test2.txt");
                File file3 = new File(basePath + File.separator + "test3.txt");
                try {
                    PrintWriter printWriter1 = new PrintWriter(new FileOutputStream(file1));
                    //下面两行,都会触发modify
                    printWriter1.println("测试测试");
                    printWriter1.close();

                    PrintWriter printWriter2 = new PrintWriter(new FileOutputStream(file2));
                    printWriter2.println("测试测试");
                    printWriter2.close();

                    PrintWriter printWriter3 = new PrintWriter(new FileOutputStream(file3));
                    printWriter3.println("测试测试");
                    printWriter3.close();

                    file1.delete();
                    file2.delete();
                    file3.delete();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 使用线程频繁增删改测试响应

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章