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

The following examples show how to use org.apache.commons.io.monitor.FileAlterationMonitor#start() . 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: ApplicationConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    File confDir = Util.getConfDirPath();
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
                FileFilterUtils.prefixFileFilter("application"),
                FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 2
Source File: HbaseConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void init(HbaseAdapter hbaseAdapter, Properties envProperties) {
    this.hbaseAdapter = hbaseAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 3
Source File: RdbConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void init(String key, RdbAdapter rdbAdapter, Properties envProperties) {
    this.key = key;
    this.rdbAdapter = rdbAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 4
Source File: ESConfigMonitor.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void init(ESAdapter esAdapter, Properties envProperties) {
        this.esAdapter = esAdapter;
        this.envProperties = envProperties;
        File confDir = Util.getConfDirPath(adapterName);
        try {
            FileAlterationObserver observer = new FileAlterationObserver(confDir,
                FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
//            文件变化监听采用的common io的类库
            FileListener listener = new FileListener();
            observer.addListener(listener);
            fileMonitor = new FileAlterationMonitor(3000, observer);
            fileMonitor.start();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
 
Example 5
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 扫描文件变化
 * @throws Exception
 */
public void monitorFileChanged() throws Exception {
	if (httpConfig.monitorFileChange) {
		if (httpConfig.getPageRoot() != null) {
			File directory = new File(httpConfig.getPageRoot());//需要扫描的文件夹路径
			// 测试采用轮询间隔 5 秒
			long interval = TimeUnit.SECONDS.toMillis(5);
			FileAlterationObserver observer = new FileAlterationObserver(directory, new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					return true;
				}
			});
			//设置文件变化监听器
			observer.addListener(new FileChangeListener(this));
			FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
			monitor.start();
		}
	}
}
 
Example 6
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 7
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 8
Source File: ESConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
public void init(ESAdapter esAdapter, Properties envProperties) {
    this.esAdapter = esAdapter;
    this.envProperties = envProperties;
    this.adapterName = envProperties.getProperty("es.version");
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 9
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 10
Source File: ApplicationConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
    File confDir = Util.getConfDirPath();
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(),
                FileFilterUtils.prefixFileFilter("application"),
                FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 11
Source File: HbaseConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
public void init(HbaseAdapter hbaseAdapter, Properties envProperties) {
    this.hbaseAdapter = hbaseAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 12
Source File: RdbConfigMonitor.java    From canal with Apache License 2.0 6 votes vote down vote up
public void init(String key, RdbAdapter rdbAdapter, Properties envProperties) {
    this.key = key;
    this.rdbAdapter = rdbAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 13
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 14
Source File: LocalFileDataConfigSource.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(Litchi litchi) {
    JSONObject config = litchi.config(Constants.Component.DATA_CONFIG);
    if (config == null) {
        LOGGER.error("'dataConfig' node not found in litchi.json");
        return;
    }

    String dataSourceKey = config.getString("dataSource");
    JSONObject fileConfig = config.getJSONObject(dataSourceKey);

    if (fileConfig == null || fileConfig.isEmpty()) {
        LOGGER.error("'dataConfig' node is null. check litchi.json please.");
        return;
    }

    this.filePath = FileUtils.combine(litchi.getRootConfigPath(), fileConfig.getString("filePath"));

    this.reloadFlushTime = fileConfig.getInteger("reloadFlushTime");

    this.litchi = litchi;

    this.fileDir = new File(this.filePath);
    if (!this.fileDir.isDirectory()) {
        LOGGER.error("current data config path is not directory: path:{}", this.filePath);
        return;
    }

    try {
        reloadMonitor = new FileAlterationMonitor(this.reloadFlushTime, createFileObserver());
        reloadMonitor.start();

        LOGGER.info("local data config flush monitor is start!");
        LOGGER.info("path:{}, flushTime:{}ms", this.filePath, this.reloadFlushTime);
    } catch (Exception ex) {
        LOGGER.error("{}", ex);
    }
}
 
Example 15
Source File: KuduConfigMonitor.java    From canal with Apache License 2.0 5 votes vote down vote up
public void init(KuduAdapter kuduAdapter, Properties envProperties) {
    this.kuduAdapter = kuduAdapter;
    this.envProperties = envProperties;
    File confDir = Util.getConfDirPath(adapterName);
    try {
        FileAlterationObserver observer = new FileAlterationObserver(confDir,
            FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter("yml")));
        FileListener listener = new FileListener();
        observer.addListener(listener);
        fileMonitor = new FileAlterationMonitor(3000, observer);
        fileMonitor.start();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 16
Source File: SensitiveWordMonitor.java    From everyone-java-blog with Apache License 2.0 5 votes vote down vote up
public void monitor(String directory, int interval) {
    FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(directory, FileFilterUtils.and(FileFilterUtils.nameFileFilter(SENSITIVE_WORD_FILE_NAME)), null);
    fileAlterationObserver.addListener(this);
    FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor(interval, fileAlterationObserver);

    try {
        fileAlterationMonitor.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 17
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 18
Source File: FileMonitorTest.java    From spring-boot with Apache License 2.0 3 votes vote down vote up
/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {


    // 监控根目录,会递归监控子文件夹变化

    String rootDir = "D:\\download";
    // 轮询间隔 5 秒,通过重新启动 FileAlterationMonitor 实现
    long interval = TimeUnit.SECONDS.toMillis(5);


    // 创建一个文件观察器用于处理文件的格式
    // Create a FileFilter
    IOFileFilter directories = FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE);
    IOFileFilter files = FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".java"));
    IOFileFilter filter = FileFilterUtils.or(directories, files);

    // Create the File system observer and register File Listeners
    FileAlterationObserver observer = new FileAlterationObserver(rootDir, filter, null);
    observer.addListener(new FileMonitorFileListener()); //设置文件变化监听器

    //创建文件变化监听器
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);

    // 开始监控
    monitor.start();

}