Jnotify 文件监控

Jnotify Note

简介

Jnotiy, 支持动态监控(支持级联监控)文件夹和文件的jar包。
在linux中,调用linux底层的jnotify服务。
在windows中,需要添加附件的dll文件。

下载安装(Mavne)

  <dependency>
    <groupId>net.contentobjects.jnotify</groupId>
    <artifactId>jnotify</artifactId>
    <version>0.94</version>
  </dependency>
  
  <!-- 在central库中没有这个包,需要添加以下的repo -->
  <repositories>
    <repository>
        <id>bintray</id>
        <url>http://dl.bintray.com/typesafe/maven-releases/</url>
    </repository>
  </repositories>

使用

  • 首先从jar包中解目录压出dll文件,并放到工程lib/目录下。比如
    /project/lib/native_libraries/...

  • 测试代码

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Properties;
import org.apache.log4j.Logger;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;

public class TestJnotify {
    
    static Logger log = Logger.getLogger(TestJnotify.class);
    /**
     * jnotify动态库 - 32位
     */
    static final String NATIVE_LIBRARIES_32BIT = "/lib/native_libraries/32bits/";
    /**
     * jnotify动态库 - 64位
     */
    static final String NATIVE_LIBRARIES_64BIT = "/lib/native_libraries/64bits/";
        
    public static void main(String[] args) throws JNotifyException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        
        log.debug("-----------Jnotify test ---------");
        
        Properties sysProps = System.getProperties();
        String osArch = (String) sysProps.get("os.arch");
        String osName = (String) sysProps.get("os.name");
        String userDir = (String) sysProps.getProperty("user.dir");
        LOG.debug("os.arch: " + osArch);
        LOG.debug("os.name: " + osName);
        LOG.debug("userDir: " + userDir);
        LOG.debug("java.class.path: " + sysProps.get("java.class.path"));
        
        // 直接调用Jnotify时, 会发生异常:java.lang.UnsatisfiedLinkError: no jnotify_64bit in java.library.path
        // 这是由于Jnotify使用JNI技术来加载dll文件,如果在类路径下没有发现相应的文件,就会抛出此异常。
        // 因此可以通过指定程序的启动参数: java -Djava.library.path=/path/to/dll,
        // 或者是通过修改JVM运行时的系统变量的方式来指定dll文件的路径,如下:

        // 判断系统是32bit还是64bit,决定调用对应的dll文件
        String jnotifyDir = NATIVE_LIBRARIES_64BIT;
        if (!osArch.contains("64")) {
            jnotifyDir = NATIVE_LIBRARIES_32BIT;
        }
        LOG.debug("jnotifyDir: " + jnotifyDir);
        // 获取目录路径
        String pathToAdd = userDir + jnotifyDir ;
        boolean isAdded = false;
        final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
        usrPathsField.setAccessible(true);
        final String[] paths = (String[]) usrPathsField.get(null);
        LOG.debug("usr_paths: " + Arrays.toString(paths));
        for (String path : paths) {
            if (path.equals(pathToAdd)) {
                isAdded  = true;
                break;
            }
        }
        if (!isAdded) {
            final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
            newPaths[newPaths.length - 1] = pathToAdd;
            usrPathsField.set(null, newPaths);
        }

        LOG.debug("java.library.path: " + System.getProperty("java.library.path"));
        LOG.debug("usr_paths: " + Arrays.toString((String[]) usrPathsField.get(null)));
        usrPathsField.setAccessible(false);
        LOG.debug("类路径加载完成");
        
        // 监听F盘下的文件事件
        JNotify.addWatch("F:\\", JNotify.FILE_ANY, true, new JNotifyListener() {
            @Override
            public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
                log.debug("wd = " + wd + ", rootPath = " + rootPath);
                log.debug("oldName = " + oldName + ", newName = " + newName);
            }
            @Override
            public void fileModified(int wd, String rootPath, String fileName) {
                log.debug("fileModified");
            }
            @Override
            public void fileDeleted(int wd, String rootPath, String fileName) {
                log.debug("fileDeleted");
            }
            @Override
            public void fileCreated(int wd, String rootPath, String fileName) {
                log.debug("fileDeleted");
            }
        });
        while (true) {
            
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,969评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,959评论 6 342
  • 动态链接,在可执行文件装载时或运行时,由操作系统的装载程序加载库。大多数操作系统将解析外部引用(比如库)作为加载过...
    小5筒阅读 5,564评论 0 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,422评论 25 708
  • 你的原形是一粒微小的沙 蚌 是你的家 历经无数次的磨炼与痛苦 修炼成一颗美丽的珍珠 离开家脱颖而出 璀璨夺目 你的...
    文采乐阅读 271评论 4 11