Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

watchservice - Java Watch Service : Not Working for Remote Files mounted in the local Server

I have Java program monitoring a remote folder mounted in my local server. But it is not detecting any changes / modification whenever something changed in the remote folder.

It is working fine if the changes / modification is made in the mounted folder.

Searching through net, as mention in the Java docs

If a watched file is not located on a local storage device then it is implementation specific if changes to the file can be detected. In particular, it is not required that changes to files carried out on remote systems be detected.

Anyone could help provide me sample on how to do this? below is my current code

WatchService watcher = FileSystems.getDefault().newWatchService();
    Path dir = Paths.get(directory);
    dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

    while (true) {

        WatchKey key;
        try {
            key = watcher.take();
        } catch (Exception ex) {
            return;
        }

        for (WatchEvent<?> event : key.pollEvents()) {
            WatchEvent.Kind<?> kind = event.kind();

            @SuppressWarnings("unchecked")
            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path fileName = ev.context();

            if (kind == ENTRY_MODIFY) {
                    System.out.println("file has changed");
                   // other process    
            }
           if (kind == ENTRY_CREATE) {
                  System.out.println("file has created");
                  // other process
           }
        }

        boolean valid = key.reset();
        if (!valid) {
            break;
        }
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have same issue and used org.apache.commons.io.monitor.FileAlterationMonitor. The pom.xml changes as suggested in the post before is as below

<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
</dependency>

Code Snippet for usage which is working for me is as below:

    String monitoringDirectory= "<YOUR CODE HERE>"; 
    FileAlterationObserver observer = new FileAlterationObserver(monitorDirectory);

    logger.info("Start ACTIVITY, Monitoring "+monitorDirectory);
    observer.addListener(new FileAlterationListenerAdaptor(){
         @Override
         public void onDirectoryCreate(File file) {
            logger.info("New Folder Created:"+file.getName());
         }

         @Override
         public void onDirectoryDelete(File file) {
             logger.info("Folder Deleted:"+file.getName());
         } 

         @Override
         public void onFileCreate(File file) {
             logger.info("File Created:"+file.getName()+": YOUR ACTION");

         }

         @Override
         public void onFileDelete(File file) {
             logger.info("File Deleted:"+file.getName()+": NO ACTION");
         }  
      });
    /* Set to monitor changes for 500 ms */     
    FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer);
    try {
        monitor.start();
    } catch (Exception e) {
        logger.error("UNABLE TO MONITOR SERVER" + e.getMessage());
        e.printStackTrace();

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...