You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Josua Troesch (JIRA)" <ji...@apache.org> on 2010/02/11 19:14:36 UTC
[jira] Commented: (VFS-299) Listeners on DefaultFileMonitor not
deregistered on stop()
[ https://issues.apache.org/jira/browse/VFS-299?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12832610#action_12832610 ]
Josua Troesch commented on VFS-299:
-----------------------------------
_Here some example Code to clarify:_
import org.apache.commons.io.FileUtils;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
import java.io.File;
import java.io.IOException;
public class MinimalFileMonitorBug {
private static final String path = "dummy.txt";
public static void main(String[] args) throws IOException, InterruptedException {
MyFileListener listener1 = new MyFileListener();
DefaultFileMonitor fileMonitor = new DefaultFileMonitor(listener1);
fileMonitor.addFile(getFileObject());
fileMonitor.start();
// Normal operation ...
fileMonitor.stop();
// Deliberately writing on file without getting notified.
MyFileListener listener2 = new MyFileListener();
fileMonitor = new DefaultFileMonitor(listener2);
fileMonitor.addFile(getFileObject());
fileMonitor.start();
Thread.sleep(1500);
FileUtils.writeStringToFile(getFile(), "new content");
Thread.sleep(1500);
}
private static FileObject getFileObject() throws IOException {
return VFS.getManager().resolveFile(getFile().getAbsolutePath());
}
private static File getFile() throws IOException {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
static class MyFileListener implements FileListener {
@Override
public void fileCreated(FileChangeEvent fileChangeEvent) throws Exception {
}
@Override
public void fileDeleted(FileChangeEvent fileChangeEvent) throws Exception {
}
@Override
public void fileChanged(FileChangeEvent fileChangeEvent) throws Exception {
System.out.println(String.format("File [%s] changed event from [%s]", fileChangeEvent.getFile().getName(), this));
}
}
}
*Output:*
File [file:///E:/test/dummy.txt] changed event from [MinimalFileMonitorBug$MyFileListener@14b7453]
File [file:///E:/test/dummy.txt] changed event from [MinimalFileMonitorBug$MyFileListener@c21495]
> Listeners on DefaultFileMonitor not deregistered on stop()
> ----------------------------------------------------------
>
> Key: VFS-299
> URL: https://issues.apache.org/jira/browse/VFS-299
> Project: Commons VFS
> Issue Type: Bug
> Affects Versions: 1.0
> Environment: windows xp
> Reporter: Josua Troesch
> Priority: Minor
>
> If I
> 1. register a File to a DefaultFileMonitor
> 2. stop() that DefaultFileMonitor
> 3. create a new DefaultFileMonitor and
> 4. register the same File to it
> 5. write to the File
> I get the {{FileChangeEvent}} on both listeners, on the one registered to the new DefaultFileMonitor (as expected) but also on the one registered to the stopped DefaultFileMonitor. I tracked it down to the {{LocalFileSystem.listenerMap}} containing both listeners for the File. In my project I fixed this behaviour as follows:
> Extended the {{stop()}} method on {{DefaultFileMonitor}}:
> public void stop() {
> this.shouldRun = false;
> // Inserted this bit
> for (FileMonitorAgent agent : (Collection<FileMonitorAgent>)monitorMap.values()) {
> agent.removeListeners();
> }
> }
> And adding the method to the {{DefaultFileMonitor$FileMonitorAgent}}:
> public void removeListeners() {
> file.getFileSystem().removeListener(file, fm.getFileListener());
> }
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.