javavfsapache-commons-vfs

When a file gets deleted that is being monitored with VFS (apache commons) I don't get notified when the file gets added back


I have a simple file monitor setup that watches one file so I get notified when the contents of that file changes, added, or deleted. But when the file gets deleted I never get notified when it gets added back. Here is a snippet of my code:

String properyPath = "/some/directory/somexml.xml";
FileSystemManager fsManager;
fsManager = VFS.getManager();
FileObject listendir = fsManager.resolveFile( propertyPath );
DefaultFileMonitor fm = new DefaultFileMonitor( this );
fm.setRecursive( true );
fm.addFile( listendir );
fm.start();

When the propertyPath file gets deleted I get notified in my fileDeleted implementation, but the fileAdded method never gets called when I recreate the file again. Is this normal? If so, how do I set it up to get notified on adds after a delete?


Solution

  • Thanks Jk1 for pointing me to the issue. The answer is found here.

    In summary, the FileMonitorAgent class in vfs removes the listener when a file gets deleted. (see the check method) below is the important block:

    // If the file existed and now doesn't
    if (this.exists && !this.file.exists())
    {
      this.exists = this.file.exists();
      this.timestamp = -1;
    
      // Fire delete event
    
      ((AbstractFileSystem)
         this.file.getFileSystem()).fireFileDeleted(this.file);
    
      // Remove listener in case file is re-created. Don't want to fire twice.
      if (this.fm.getFileListener() != null)
      {
         this.file.getFileSystem().removeListener(this.file,
            this.fm.getFileListener());
      }
    
      // Remove from map
      this.fm.queueRemoveFile(this.file);
    }
    

    The link supplies a solution that has been submitted to vfs. I think the only solution at this point is to spawn a thread that re-adds the file to vfs after a few seconds. You have to sleep a few seconds because you get notified (via fireFileDeleted) and then the vfs code clears the listener, so you can re-add the listener until after you get notified and the vfs code clears the existing listener.