Monitor Directories and File(II)Timer and commons-vfs

Monitor Directories and File(II)Timer and commons-vfs

1. Timer and TimerTask
The Interface FileChangeListener.java:
package com.xxxx.importdata.filemonitor.timer;
public interface FileChangeListener
{
public void fileChanged(String filename);
}
The ClassFileChangeListener.java:
package com.xxxxx.importdata.filemonitor.timer;
public class ClassFileChangeListener implements FileChangeListener {
public void fileChanged(String filename) {
System.out
.println("File " + filename + " modified ,it must  reload  !");
}
}
The most import class FileMonitor.java:
package com.xxxxx.importdata.filemonitor.timer;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class FileMonitor
{
    private static final FileMonitor     instance = new FileMonitor();
    private Timer                        timer;
    private Map<String, FileMonitorTask> timerEntries;
    private FileMonitor()
    {
        this.timerEntries = new HashMap<String, FileMonitorTask>();
        this.timer = new Timer();
    }
    public static FileMonitor getInstance()
    {
        return instance;
    }
    public void addFileChangeListener(FileChangeListener listener, String filename, long period)
    {
        this.removeFileChangeListener(filename);
        FileMonitorTask task = new FileMonitorTask(listener, filename);
        this.timerEntries.put(filename, task);
        // schedule(TimerTask task,long delay,long period)
        // schedule(TimerTask task,long delay)
        // schedule(TimerTask task,Date time)
        // schedule(TimerTask task,Date firstTime,long period)
        this.timer.scheduleAtFixedRate(task, period, period);
    }
    public void removeFileChangeListener(String filename)
    {
        FileMonitorTask task = (FileMonitorTask) this.timerEntries.remove(filename);
        if (task != null)
        {
            task.cancel();
        }
    }
    private static class FileMonitorTask extends TimerTask
    {
        private FileChangeListener listener;
        private String             filename;
        private File               monitoredFile;
        private long               lastModified;
        public FileMonitorTask(FileChangeListener listener, String filename)
        {
            this.listener = listener;
            this.filename = filename;
            this.monitoredFile = new File(filename);
            if (!this.monitoredFile.exists())
            {
                return;
            }
            this.lastModified = this.monitoredFile.lastModified();
        }
        public void run()
        {
            long latestChange = this.monitoredFile.lastModified();
            if (this.lastModified != latestChange)
            {
                this.lastModified = latestChange;
                this.listener.fileChanged(this.filename);
            }
        }
    }
    public static void main(String[] args)
    {
        String path = "/home/luohua/tmp/t1.txt";
        //String path = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
        FileMonitor.getInstance().addFileChangeListener(new ClassFileChangeListener(), path, 2000);
    }
}

I am using Linux system, I notice that this lastModified can only detect the file changes. But if there are some changes in the
sub directory. It can not detect that.

And one more thing, it is said that the Timer will cost a lot. I will check if there is another better way.

2. commons-vfs
ivy.xml:
<dependency org="commons-vfs" name="commons-vfs" rev="1.0" />

the test java class is VFSMonitor.java:
package com.xxxxx.importdata.filemonitor.vfs;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
public class VFSMonitor
{
    private static final String PATH = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
    public static void main(String[] args)
    {
        FileSystemManager fsManager = null;
        FileObject listendir = null;
        try
        {
            fsManager = VFS.getManager();
            listendir = fsManager.resolveFile(PATH);
        }
        catch (FileSystemException e)
        {
            e.printStackTrace();
        }
        DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener() {
            public void fileCreated(FileChangeEvent event) throws Exception
            {
                monitor(event);
            }
            public void fileDeleted(FileChangeEvent event) throws Exception
            {
                monitor(event);
            }
            public void fileChanged(FileChangeEvent event) throws Exception
            {
                monitor(event);
            }
            private void monitor(FileChangeEvent event)
            {
                FileObject fileObject = event.getFile();
                FileName fileName = fileObject.getName();
                System.out.println(fileName + " has changed.");
            }
        });
        fm.setRecursive(true);
        fm.addFile(listendir);
        fm.start();
        try
        {
            Thread.sleep(1000000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        fm.stop();
    }
}

references:
http://www.iteye.com/topic/1096698
http://www.blogjava.net/quaff/archive/2006/03/02/33229.html

猜你喜欢

转载自sillycat.iteye.com/blog/1113940