简体   繁体   English

如何在watchservice中正确处理pollEvents()溢出类型?

[英]How do I properly handle pollEvents() overflow kind in a watchservice?

I am using a watch service to monitor a directory and fire logic on newly created files. 我正在使用监视服务来监视目录并在新创建的文件上触发逻辑。 One of the challenges that I've recently experienced is an overflow being triggered when a high volume of files needs to be processed and gets copied into the watch directory too quick to handle. 我最近遇到的挑战之一是当需要处理大量文件并被快速复制到watch目录时触发溢出。

The API says this about overflow: API说溢出:

File systems may report events faster than they can be retrieved or processed and an implementation may impose an unspecified limit on the number of events that it may accumulate. 文件系统可以比检索或处理事件更快地报告事件,并且实现可以对其可能累积的事件的数量施加未指定的限制。 Where an implementation knowingly discards events then it arranges for the key's pollEvents method to return an element with an event type of OVERFLOW. 如果实现有意丢弃事件,那么它会安排密钥的pollEvents方法返回事件类型为OVERFLOW的元素。

My question is, how can I properly handle the overflow, without losing any of the events that need to be processed? 我的问题是,如何正确处理溢出,而不会丢失任何需要处理的事件?

My watchservice code looks like: 我的watchservice代码如下:

            Path myDir = Paths.get(srcDir);
            try(WatchService watcher = myDir.getFileSystem().newWatchService()){
                myDir.register(watcher, ENTRY_CREATE,ENTRY_MODIFY);
                int x = 0;
                for(;;){
                    x++;
                    WatchKey watchKey = watcher.take();
                    LOGGER.debug("Event # {}",x);
                    List<WatchEvent<?>> events = watchKey.pollEvents();
                    LOGGER.info("Events Size {}",events.size());
                    for (WatchEvent event : events) {
                        if(event.kind() == OVERFLOW){
                            LOGGER.error("The Maximum watchService events has been reached!");
                            System.exit(1); //I exit so I know there is a problem - but how should I handle this? 
                        }
                        if (event.kind() == ENTRY_CREATE) {
                            LOGGER.info("File created: " + event.context().toString());
                            LOGGER.info("Beginning Processing:" +event.context().toString());
                            ...business logic here...
                        }
                    }
                    watchKey.reset();
                }
          ...

I've never seen the overflow event in practice. 我在实践中从未见过溢出事件。 Its meant to inform you that you will need to reprocess whatever directory you're watching. 它意味着通知您,您将需要重新处理您正在观看的任何目录。 You don't need to exit the program, just crawl the directory using a single threaded File.list() call. 您不需要退出程序,只需使用单线程File.list()调用来爬网目录。 I've cut-n-pasted how I handle it below. 我已经贴上了下面的处理方式。 This code... 这段代码......

1) Logs an issue 1)记录问题

2) Sets a flag to trigger a directory reprocess that crawls all the files in a directory 2)设置一个标志以触发目录重新处理,该目录重新处理对目录中的所有文件进行爬网

3) Skips the rest of the processing of this WatchEvent 3)跳过此WatchEvent的其余处理

// log overflow events and trigger reprocess later.
if (kind == OVERFLOW) 
{
    logger.warn("File listener recieved an overflow event.  You should probably check into this");
    overflowTriggeredFlag = true;
    continue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM