org.apache.commons.io.monitor.FileAlterationMonitor Java Examples
The following examples show how to use
org.apache.commons.io.monitor.FileAlterationMonitor.
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 |
/** * 只监控文件发送变化,如果是子目录的文件改变,则目录会变,由于没有过滤目录发生变化,则目录下的文件改变不会监控到 * @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: DefaultTopologyService.java From knox with Apache License 2.0 | 6 votes |
@Override public void startMonitor() throws Exception { // Start the local configuration monitors for (Entry<String, FileAlterationMonitor> monitor : monitors.entrySet()) { monitor.getValue().start(); log.startedMonitor(monitor.getKey()); } // Start the remote configuration monitor, if it has been initialized if (remoteMonitor != null) { try { remoteMonitor.start(); } catch (Exception e) { log.remoteConfigurationMonitorStartFailure(remoteMonitor.getClass().getTypeName(), e.getLocalizedMessage()); } } // Trigger descriptor discovery (KNOX-2301) triggerDescriptorDiscovery(); }
Example #3
Source File: ApplicationConfigMonitor.java From canal with Apache License 2.0 | 6 votes |
@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 #4
Source File: FileMonitor.java From MercuryTrade with MIT License | 6 votes |
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 #5
Source File: HbaseConfigMonitor.java From canal with Apache License 2.0 | 6 votes |
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 #6
Source File: RdbConfigMonitor.java From canal with Apache License 2.0 | 6 votes |
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 #7
Source File: ESConfigMonitor.java From canal with Apache License 2.0 | 6 votes |
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 #8
Source File: FileMonitor.java From util4j with Apache License 2.0 | 6 votes |
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 #9
Source File: DefaultHttpRequestHandler.java From t-io with Apache License 2.0 | 6 votes |
/** * 扫描文件变化 * @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 #10
Source File: DirectoryMonitor.java From fuchsia with Apache License 2.0 | 6 votes |
public DirectoryMonitor(String directorypath, long polling, String classname) { this.directory = new File(directorypath); this.trackedClassName = classname; this.polling = polling; if (!directory.isDirectory()) { LOG.info("Monitored directory {} not existing - creating directory", directory.getAbsolutePath()); if (!this.directory.mkdirs()) { throw new IllegalStateException("Monitored directory doesn't exist and cannot be created."); } } // We observes all files. FileAlterationObserver observer = new FileAlterationObserver(directory, TrueFileFilter.INSTANCE); observer.checkAndNotify(); observer.addListener(new FileMonitor()); monitor = new FileAlterationMonitor(polling, observer); }
Example #11
Source File: ESConfigMonitor.java From canal-1.1.3 with Apache License 2.0 | 6 votes |
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 #12
Source File: RdbConfigMonitor.java From canal-1.1.3 with Apache License 2.0 | 6 votes |
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: HbaseConfigMonitor.java From canal-1.1.3 with Apache License 2.0 | 6 votes |
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 #14
Source File: ApplicationConfigMonitor.java From canal-1.1.3 with Apache License 2.0 | 6 votes |
@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 #15
Source File: Pipeline.java From wisdom with Apache License 2.0 | 5 votes |
/** * 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 #16
Source File: FileModificationPollingMonitor.java From pravega with Apache License 2.0 | 5 votes |
@VisibleForTesting FileModificationPollingMonitor(@NonNull Path fileToWatch, @NonNull Consumer<File> callback, int pollingInterval, boolean checkForFileExistence) throws FileNotFoundException { Exceptions.checkNotNullOrEmpty(fileToWatch.toString(), "fileToWatch"); if (checkForFileExistence && !fileToWatch.toFile().exists()) { throw new FileNotFoundException(String.format("File [%s] does not exist.", fileToWatch)); } this.pathOfFileToWatch = fileToWatch; this.callback = callback; this.pollingInterval = pollingInterval; monitor = new FileAlterationMonitor(this.pollingInterval); }
Example #17
Source File: KuduConfigMonitor.java From canal with Apache License 2.0 | 5 votes |
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 #18
Source File: DefaultTopologyService.java From knox with Apache License 2.0 | 5 votes |
@Override public void stopMonitor() throws Exception { // Stop the local configuration monitors for (Entry<String, FileAlterationMonitor> monitor : monitors.entrySet()) { monitor.getValue().stop(); log.stoppedMonitor(monitor.getKey()); } // Stop the remote configuration monitor, if it has been initialized if (remoteMonitor != null) { remoteMonitor.stop(); } }
Example #19
Source File: AsciidoctorRefreshMojo.java From asciidoctor-maven-plugin with Apache License 2.0 | 5 votes |
private void stopMonitor() throws MojoExecutionException { if (monitors != null) { for (final FileAlterationMonitor monitor : monitors) { try { monitor.stop(); } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } } } }
Example #20
Source File: LocalFileDataConfigSource.java From litchi with Apache License 2.0 | 5 votes |
@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 #21
Source File: SensitiveWordMonitor.java From everyone-java-blog with Apache License 2.0 | 5 votes |
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 #22
Source File: IncrementalBuilder.java From vertx-maven-plugin with Apache License 2.0 | 5 votes |
public IncrementalBuilder(Set<Path> inclDirs, List<Callable<Void>> chain, Log logger, long watchTimeInterval) { this.chain = chain; this.logger = logger; this.monitor = new FileAlterationMonitor(watchTimeInterval); inclDirs.forEach(this::buildObserver); }
Example #23
Source File: TorrentFileWatcher.java From joal with Apache License 2.0 | 5 votes |
TorrentFileWatcher(final FileAlterationListener listener, final Path monitoredFolder, final Integer interval) { Preconditions.checkNotNull(listener, "listener cannot be null"); Preconditions.checkNotNull(monitoredFolder, "monitoredFolder cannot be null"); Preconditions.checkArgument(Files.exists(monitoredFolder), "Folder '" + monitoredFolder.toAbsolutePath() + "' does not exists."); Preconditions.checkNotNull(interval, "interval cannot be null"); Preconditions.checkArgument(interval > 0, "interval cannot be less than 1"); this.listener = listener; this.monitoredFolder = monitoredFolder.toFile(); this.monitor = new FileAlterationMonitor(interval); this.monitor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("torrent-file-watcher-%d").build()); this.observer = new FileAlterationObserver(this.monitoredFolder, TORRENT_FILE_FILTER); this.observer.addListener(this.listener); monitor.addObserver(this.observer); }
Example #24
Source File: DefaultScriptSource.java From util4j with Apache License 2.0 | 5 votes |
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 #25
Source File: IncrementalBuilder.java From vertx-maven-plugin with Apache License 2.0 | 5 votes |
public IncrementalBuilder(Set<Path> inclDirs, List<Callable<Void>> chain, Log logger, long watchTimeInterval) { this.chain = chain; this.logger = logger; this.monitor = new FileAlterationMonitor(watchTimeInterval); inclDirs.forEach(this::buildObserver); }
Example #26
Source File: DefaultWatchService.java From arcusplatform with Apache License 2.0 | 5 votes |
public DefaultWatchService(long watchIntervalInMs) throws Exception { this.watches = new ConcurrentHashMap<>(); this.monitor = new FileAlterationMonitor(watchIntervalInMs); this.monitor.setThreadFactory(new Factory()); this.monitor.start(); }
Example #27
Source File: DefaultTopologyService.java From knox with Apache License 2.0 | 4 votes |
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); }
Example #28
Source File: DefaultTopologyService.java From knox with Apache License 2.0 | 4 votes |
private void initListener(String monitorName, File directory, FileFilter filter, FileAlterationListener listener) { // Increasing the monitoring interval to 5 seconds as profiling has shown // this is rather expensive in terms of generated garbage objects. initListener(monitorName, new FileAlterationMonitor(5000L), directory, filter, listener); }
Example #29
Source File: LiveBuildConfig.java From bonita-ui-designer with GNU General Public License v2.0 | 4 votes |
@Bean public FileAlterationMonitor monitor() { return new FileAlterationMonitor(1000); }
Example #30
Source File: Watcher.java From bonita-ui-designer with GNU General Public License v2.0 | 4 votes |
@Inject public Watcher(ObserverFactory observerFactory, FileAlterationMonitor monitor) { this.observerFactory = observerFactory; this.monitor = monitor; }