Java 7 WatchService-忽略同一事件的多次出现 [英] Java 7 WatchService - Ignoring multiple occurrences of the same event

查看:690
本文介绍了Java 7 WatchService-忽略同一事件的多次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StandardWatchEventKinds.ENTRY_MODIFY的Javadoc说:

The javadoc for StandardWatchEventKinds.ENTRY_MODIFY says:

目录条目已修改.当为此目录注册时 事件,然后当观察到其中的一个条目时,WatchKey将排队 目录已被修改.该事件的事件计数为1 或更高.

Directory entry modified. When a directory is registered for this event then the WatchKey is queued when it is observed that an entry in the directory has been modified. The event count for this event is 1 or greater.

通过编辑器编辑文件的内容时,它将同时修改日期(或其他元数据)和内容.因此,您将获得两个ENTRY_MODIFY事件,但是每个事件的count都为1(至少我就是这样).

When you edit the content of a file through an editor, it'll modify both date (or other metadata) and content. You therefore get two ENTRY_MODIFY events, but each will have a count of 1 (at least that's what I'm seeing).

我正在尝试监视使用以下代码手动更新(即通过命令行vi)的配置文件(先前在WatchService中注册的servers.cfg):

I'm trying to monitor a configuration file (servers.cfg previously registered with the WatchService) that is manually updated (ie. through command line vi) with the following code:

while(true) {
    watchKey = watchService.take(); // blocks

    for (WatchEvent<?> event : watchKey.pollEvents()) {
        WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
        WatchEvent.Kind<Path> kind = watchEvent.kind();

        System.out.println(watchEvent.context() + ", count: "+ watchEvent.count() + ", event: "+ watchEvent.kind());
        // prints (loop on the while twice)
        // servers.cfg, count: 1, event: ENTRY_MODIFY
        // servers.cfg, count: 1, event: ENTRY_MODIFY

        switch(kind.name()) {
            case "ENTRY_MODIFY":
                handleModify(watchEvent.context()); // reload configuration class
                break;
            case "ENTRY_DELETE":
                handleDelete(watchEvent.context()); // do something else
                break;              
        }
    }   

    watchKey.reset();       
}

由于您收到两个ENTRY_MODIFY事件,因此上述内容将在仅需要一次时重新加载配置两次.假设可能发生不止一个这样的事件,是否有任何方法可以忽略其中的所有事件?

Since you get two ENTRY_MODIFY events, the above would reload the configuration twice when only once is needed. Is there any way to ignore all but one of these, assuming there could be more than one such event?

如果WatchService API具有这样的实用程序,那就更好了. (我不想检查每个事件之间的时间.我代码中的所有处理程序方法都是同步的.

If the WatchService API has such a utility so much the better. (I kind of don't want to check times between each event. All the handler methods in my code are synchronous.

如果将文件从一个目录创建(复制/粘贴)到监视目录,则会发生相同的情况.您如何将两者结合成一个事件?

The same thing occurs if you create (copy/paste) a file from one directory to the watched directory. How can you combine both of those into one event?

推荐答案

我遇到了类似的问题-我正在使用WatchService API来保持目录同步,但是观察到在许多情况下,更新执行了两次.我似乎已经通过检查文件上的时间戳解决了该问题-这似乎屏蔽了第二个复制操作. (至少在Windows 7中-我不确定它是否可以在其他操作系统中正常工作)

I had a similar issue - I am using the WatchService API to keep directories in sync, but observed that in many cases, updates were being performed twice. I seem to have resolved the issue by checking the timestamp on the files - this seems to screen out the second copy operation. (At least in windows 7 - I can't be sure if it will work correctly in other operation systems)

也许您可以使用类似的东西?存储文件中的时间戳记,仅在更新时间戳记时重新加载吗?

Maybe you could use something similar? Store the timestamp from the file and reload only when the timestamp is updated?

这篇关于Java 7 WatchService-忽略同一事件的多次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆