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

橙三吉。 提交于 2019-12-05 13:54:41

I guess, Oracle's watch service doesn't detect remote events - that service is for local directories.

You have to use - org.apache.commons.io.monitor.FileAlterationMonitor class of API -

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

You should google for code samples using that class.

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();

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!