Simple test example of JNotify monitoring file changes

1. Reason

More comprehensive and faster monitoring with JNotify.

2. Reference code

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

public class Main implements JNotifyListener {
    public static void main(String[] args) throws JNotifyException, InterruptedException {
        // path to watch
        String path = "/weatherdata";
        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;


        // watch subtree?
        boolean watchSubtree = true;


        // add actual watch
        int watchID = JNotify.addWatch(path, mask, watchSubtree, new Main());


        // sleep a little, the application will exit if you
        // don't (watching is asynchronous), depending on your
        // application, this may not be required
        Thread.sleep(1000000);


        // to remove watch the watch
        boolean res = JNotify.removeWatch(watchID);
    }

    int count = 0;

    @Override
    public void fileCreated(int i, String s, String s1) {
        System.out.println("fileCreated s=" + i + " i=" + s + " s1=" + s1);
    }

    @Override
    public void fileDeleted(int i, String s, String s1) {
        System.out.println("fileDeleted s=" + s + " i=" + i + " s1=" + s1);
    }

    @Override
    public void fileModified(int i, String s, String s1) {
        System.out.println("fileModified s=" + s + " i=" + i + " s1=" + s1);
    }

    @Override
    public void fileRenamed(int i, String s, String s1, String s2) {
        System.out.println("fileRenamed s=" + s + " i=" + i + " s1=" + s1 + " s2=" + s2 + " count:" + (++count));

    }
}

3. Description

If the JNotify library cannot be found during startup (https://sourceforge.net/projects/jnotify/files/jnotify/jnotify-0.94/ download), please copy the latest version of JNotify to the corresponding library directory of the system.

System.getProperty("java.library.path")

On Windows, copy jnotify_64bit.dll to: c:/windows.

On Linux, you can copy libjnotify.so to the /usr/lib64 directory.

 Attachment: https://files.cnblogs.com/files/songxingzhu/jnotify-lib-0.94.zip

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325017136&siteId=291194637