title: How do I properly handle pollEvents() overflow kind in a watchservice?
body: <p>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. </p>
  
  <p>The <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html">API</a> says this about overflow: </p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>My question is, how can I properly handle the overflow, without losing any of the events that need to be processed? </p>
  
  <p>My watchservice code looks like: </p>
  
  <pre><code>            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&lt;WatchEvent&lt;?&gt;&gt; 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();
                  }
            ...
  </code></pre>