本文整理汇总了Java中org.apache.commons.io.monitor.FileAlterationObserver类的典型用法代码示例。如果您正苦于以下问题:Java FileAlterationObserver类的具体用法?Java FileAlterationObserver怎么用?Java FileAlterationObserver使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileAlterationObserver类属于org.apache.commons.io.monitor包,在下文中一共展示了FileAlterationObserver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: registerDirectory
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
@Override
public synchronized void registerDirectory(final Path directory, final FileSystemListener listener) throws IOException {
log.debug("Registering " + directory);
final FileAlterationObserver observer = new FileAlterationObserver(directory.toFile(), this.fileNameFilter);
try {
// Force the registered directory to be read into memory before we start observing changes,
// otherwise the first run will generate a 'create' event for each file and directory.
observer.initialize();
} catch (Exception e) {
throw new IOException("Failed to initialize observer for " + directory, e);
}
observer.addListener(this);
listeners.put(directory, listener);
monitor.addObserver(observer);
}
示例2: addFileAlterationObserver
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
/**
* Add {@link org.apache.commons.io.monitor.FileAlterationMonitor}s for the given
* root directory and any nested subdirectories under the root directory to the given
* {@link org.apache.commons.io.monitor.FileAlterationMonitor}.
*
* @param monitor a {@link org.apache.commons.io.monitor.FileAlterationMonitor}
* @param listener a {@link org.apache.commons.io.monitor.FileAlterationListener}
* @param rootDir root directory
*/
public static void addFileAlterationObserver(FileAlterationMonitor monitor, FileAlterationListener listener,
File rootDir) {
// Add a observer for the current root directory
FileAlterationObserver observer = new FileAlterationObserver(rootDir);
observer.addListener(listener);
monitor.addObserver(observer);
// List subdirectories under the current root directory
File[] subDirs = rootDir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
if (subDirs == null || subDirs.length == 0) {
return;
}
// Recursively add a observer for each subdirectory
for (File subDir : subDirs) {
addFileAlterationObserver(monitor, listener, subDir);
}
}
示例3: check1
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
/**
* 只监控文件发送变化,如果是子目录的文件改变,则目录会变,由于没有过滤目录发生变化,则目录下的文件改变不会监控到
* @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();
}
示例4: check2
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
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后面加,则会触发所有文件创建
}
示例5: registerFileListener
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
/**
* Creates and registers a file listener with the help of the specified build command.
*
* @param command The build command.
*
* @throws Exception If any exception occurs.
*/
private final void registerFileListener(final BuildCommand command) throws Exception {
final DocsProject project = command.getProject();
final FileAlterationObserver observer = new FileAlterationObserver(project.getDirectory());
observer.addListener(new FileAlterationListenerAdaptor() {
@Override
public final void onDirectoryChange(final File directory) {
rebuildIfNeeded(command, directory);
}
@Override
public final void onFileChange(final File file) {
rebuildIfNeeded(command, file);
}
});
monitor.addObserver(observer);
monitor.start();
}
示例6: azkabanConfigFileMonitor
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
public static void azkabanConfigFileMonitor(String[] args, Props props1) {
try {
props = props1;
configFilePath = props.getString(AZKABAN_CONFIG_FILE_PATH);//配置文件路径
long interval = props.getLong(AZKABAN_CONFIG_FILE_MONIITE_INTERVAL, 5000);//监听间隔
String dirPath = System.getProperty("user.dir") + configFilePath;
logger.error("azkabanConfigFileMonitor method, monitor path= " + dirPath);
//构造观察类:主要提供要观察的文件或目录,当然还有详细信息的filter
FileAlterationObserver observer = new FileAlterationObserver(new File(dirPath), new FileFilterImpl());
//构造收听类
FileListenerAdaptor listener = new FileListenerAdaptor(args);
//为观察对象添加收听对象
observer.addListener(listener);
//配置Monitor,第一个参数单位是毫秒,是监听的间隔;第二个参数就是绑定我们之前的观察对象。
FileAlterationMonitor fileMonitor = new FileAlterationMonitor(interval, new FileAlterationObserver[]{observer});
//启动开始监听
fileMonitor.start();
} catch (Exception e) {
logger.error("azkabanConfigFileMonitor method error", e);
}
}
示例7: start
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
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();
}
}
示例8: activate
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
public synchronized void activate() throws MojoExecutionException
{
IOFileFilter logFilter = FileFilterUtils.and(
FileFilterUtils.fileFileFilter(),
FileFilterUtils.nameFileFilter(engineLog.getName()));
FileAlterationObserver fileObserver = new FileAlterationObserver(engineLog.getParent(), logFilter);
fileObserver.addListener(new LogModificationListener());
monitor = new FileAlterationMonitor(100);
monitor.addObserver(fileObserver);
try
{
monitor.start();
}
catch (Exception ex)
{
throw new MojoExecutionException("Failed to activate deploy log forwarder", ex);
}
}
示例9: ReloadableConfig
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
public ReloadableConfig() {
new Thread(()->{
long interval = TimeUnit.SECONDS.toMillis(30);
try {
ClassPathResource resource = new ClassPathResource(getClassPathFilePath());
File file = resource.getFile();
FileAlterationObserver observer = new FileAlterationObserver(file.getParentFile() , new NameFileFilter(file.getName()));
observer.addListener(this);
FileAlterationMonitor monitor = new FileAlterationMonitor(interval , observer);
LOGGER.debug("监听文件:{}" , file.getPath());
monitor.start();
} catch (Exception e) {
LOGGER.error("ClassPathFilePath:{}" , getClassPathFilePath());
LOGGER.error("" , e);
}
}).start();
}
示例10: onStart
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
@Override
public void onStart( final FileAlterationObserver observer ) {
if( this.alreadyStarted.getAndSet( true ))
return;
this.logger.fine("Initial provisioning of templates...");
final Collection<File> templateFiles = FileUtils.listFiles(
this.templateDir,
// Find readable template files.
FileFilterUtils.and(
FileFilterUtils.suffixFileFilter( ".tpl" ),
CanReadFileFilter.CAN_READ),
// Directory filter: go through the root template directory and its direct children.
new TemplateDirectoryFileFilter( this.templateDir ));
process( templateFiles );
}
示例11: unregisterDirectoryListener
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
@Override
@SuppressWarnings("PMD.EmptyCatchBlock")
public synchronized void unregisterDirectoryListener(File directory, DirectoryListener directoryListener) {
SFMF4JFileAlterationListener listener = new SFMF4JFileAlterationListener(directoryListener);
final FileAlterationObserver observer = directoryObservers.get(directory);
if (observer != null) {
boolean shouldDestroy = false;
observer.removeListener(listener);
if (!observer.getListeners().iterator().hasNext()) {
shouldDestroy = true;
directoryObservers.remove(directory);
fileMonitor.removeObserver(observer);
}
if (shouldDestroy) {
try {
observer.destroy();
} catch (Exception ex) {
//trap
}
}
}
}
示例12: watch
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
public static void watch(Config cfg) throws Exception {
logger.info("Starting asset watcher...");
long interval = 100;
PipelineEnvironment pe = new PipelineEnvironment(cfg);
AssetFileObserver fileObserver = new AssetFileObserver(pe);
FileAlterationObserver jsObserver = fileObserver.observeJs();
FileAlterationObserver cssObserver = fileObserver.observeCss();
FileAlterationObserver imageObserver = fileObserver.observeImage();
FileAlterationObserver templateObserver = fileObserver.observeTemplate();
FileAlterationObserver serverTemplateObserver = fileObserver.observeServerTemplate();
FileAlterationObserver staticAssetObserver = fileObserver.observeStaticAsset();
FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
monitor.addObserver(jsObserver);
monitor.addObserver(cssObserver);
monitor.addObserver(imageObserver);
monitor.addObserver(templateObserver);
monitor.addObserver(serverTemplateObserver);
monitor.addObserver(staticAssetObserver);
monitor.start();
}
示例13: OpenCmsConfiguration
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
/**
* Creates a new OpenCmsConfiguration instance, prepares the XML helper for parsing configuration files, starts
* a FileAlterationObserver used to handle changes to configuration files.
* @param config the project level plugin configuration data
*/
public OpenCmsConfiguration(OpenCmsPluginConfigurationData config) {
this.config = config;
File configurationFolder = new File(config.getWebappRoot() + CONFIGPATH);
this.moduleConfigurationFile = new File(config.getWebappRoot() + CONFIGPATH + MODULECONFIGFILE);
// Create an Observer for configuration changes
FileAlterationObserver configurationChangeObserver = new FileAlterationObserver(configurationFolder);
configurationChangeObserver.addListener(new ConfigurationAlterationListener());
configurationChangeMonitor = new FileAlterationMonitor(5000, configurationChangeObserver);
try {
xmlHelper = new XmlHelper();
}
catch (Exception e) {
LOG.warn("Exception during initialization of the module configuration: " + e);
}
parseConfiguration();
}
示例14: DirectoryMonitor
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
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);
}
示例15: addMonitor
import org.apache.commons.io.monitor.FileAlterationObserver; //导入依赖的package包/类
/**
* 增加监视器
*
* @param path 监听的文件夹路径
* @param listener 事件回调接口
* @return 监听器的id
* @throws JNotifyException {@link JNotifyException}
*/
public static int addMonitor(String path, FileAlterationListener listener) throws JNotifyException {
File file = new File(path);
FileAlterationObserver observer = new FileAlterationObserver(file);
observer.checkAndNotify();
observer.addListener(listener);
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
| JNotify.FILE_MODIFIED;
return JNotify.addWatch(path, mask, false, new FileEventListener(observer));
}