Java Code Examples for org.apache.commons.io.monitor.FileAlterationMonitor#addObserver()

The following examples show how to use org.apache.commons.io.monitor.FileAlterationMonitor#addObserver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileMonitor.java    From util4j with Apache License 2.0 6 votes vote down vote up
/**
 * 只监控文件发送变化,如果是子目录的文件改变,则目录会变,由于没有过滤目录发生变化,则目录下的文件改变不会监控到
 * @param dir
 * @throws Exception 
 */
public void check1(String dir) throws Exception
{
	File directory = new File(dir);
    // 轮询间隔 5 秒
    long interval = TimeUnit.SECONDS.toMillis(5);
    // 创建一个文件观察器用于处理文件的格式
    IOFileFilter filter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),
    		FileFilterUtils.suffixFileFilter(".jar"));
    FileAlterationObserver observer = new FileAlterationObserver(directory,filter);
    //设置文件变化监听器
    observer.addListener(new MyFileListener());
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
    monitor.addObserver(observer);//文件观察
    monitor.start();
}
 
Example 2
Source File: FileMonitor.java    From util4j with Apache License 2.0 6 votes vote down vote up
public void check2(String dir) throws Exception
	{
		File directory = new File(dir);
	    // 轮询间隔 5 秒
	    long interval = TimeUnit.SECONDS.toMillis(5);
	    //后缀过滤器
	    IOFileFilter filefilter=FileFilterUtils.or(FileFilterUtils.suffixFileFilter(".class"),
	    		FileFilterUtils.suffixFileFilter(".jar"));
	    //子目录的后缀
	    IOFileFilter subFilefilter=FileFilterUtils.or(FileFilterUtils.directoryFileFilter(),filefilter);
	    //根目录和子目录变化
	    IOFileFilter filter = FileFilterUtils.or(filefilter,subFilefilter);
	    FileAlterationObserver observer = new FileAlterationObserver(directory,filter);
	    //设置文件变化监听器
	    observer.addListener(new MyFileListener());
	    FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
	    monitor.addObserver(observer);//文件观察
	    monitor.start();
//	    monitor.addObserver(observer);//文件观察,如果在start后面加,则会触发所有文件创建
	}
 
Example 3
Source File: FileMonitor.java    From MercuryTrade with MIT License 6 votes vote down vote up
public void start() {
    String gamePath = Configuration.get().applicationConfiguration().get().getGamePath();

    File folder = new File(gamePath + "logs");
    this.fileHandler = new MessageFileHandler(gamePath + "logs/Client.txt");
    FileAlterationObserver observer = new FileAlterationObserver(folder);
    monitor = new FileAlterationMonitor(POLLING_INTERVAL);
    FileAlterationListener listener = new FileAlterationListenerAdaptor() {
        @Override
        public void onFileChange(File file) {
            fileHandler.parse();
        }
    };

    observer.addListener(listener);
    monitor.addObserver(observer);
    try {
        monitor.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: DefaultScriptSource.java    From util4j with Apache License 2.0 5 votes vote down vote up
protected void init() throws Exception
{
	FileAlterationObserver observer=buildFileAlterationObserver(scriptDir);
	observer.addListener(new FileListener());
	FileAlterationMonitor monitor=new FileAlterationMonitor(updateInterval);
	monitor.addObserver(observer);
	monitor.start();
	scanResources();
}
 
Example 5
Source File: Pipeline.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the watching.
 *
 * @return the current pipeline.
 */
public Pipeline watch() {
    // Delete all error reports before starting the watcher.
    error = new File(baseDir, "target/pipeline");
    FileUtils.deleteQuietly(error);
    mojo.getLog().debug("Creating the target/pipeline directory : " + error.mkdirs());

    // Start the watching process.
    watcher = new FileAlterationMonitor(Integer.getInteger("watch.period", 2) * 1000);
    watcher.setThreadFactory(new DefensiveThreadFactory("wisdom-pipeline-watcher", mojo));
    FileAlterationObserver srcObserver = new FileAlterationObserver(new File(baseDir, "src"),
            TrueFileFilter.INSTANCE);
    PipelineWatcher listener = new PipelineWatcher(this);
    srcObserver.addListener(listener);
    watcher.addObserver(srcObserver);

    if (pomFileMonitoring) {
        FileAlterationObserver pomObserver = new FileAlterationObserver(baseDir, new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.equals(new File(baseDir, "pom.xml"));
            }
        });
        pomObserver.addListener(listener);
        watcher.addObserver(pomObserver);
    }

    try {
        mojo.getLog().info("Start watching " + baseDir.getAbsolutePath());
        watcher.start();
    } catch (Exception e) {
        mojo.getLog().error("Cannot start the watcher", e);
    }
    return this;
}
 
Example 6
Source File: DefaultTopologyService.java    From knox with Apache License 2.0 4 votes vote down vote up
private void initListener(String monitorName, FileAlterationMonitor monitor, File directory, FileFilter filter, FileAlterationListener listener) {
  monitors.put(monitorName, monitor);
  FileAlterationObserver observer = new FileAlterationObserver(directory, filter);
  observer.addListener(listener);
  monitor.addObserver(observer);
}