Register StandardWatchEventKinds..ENTRY_DELETE StandardWatchEventKinds.ENTRY_CREATE StandardWatchEventKinds.ENTRY_MODIFY StandardWatchEventKinds.OVERFLOW Watch Service WatchService

Writing a simple file/folder monitor using the Watch Service API – andreINC

The Watch Service was launched in Java 7 as a “thread-safe” service chargeable for watching objects for modifications.

The preferred use of this API can be to use it as approach to monitor a folder of information for modifications resembling: addition of latest information, deletion of information, file renames, and so on.


Implementing a WatchService

The WatchService depends on the FileSystem, the first step of making a new Service is to acquire the underlying file system:

Every object must be explicitly registered with the newly created service. In our specific case we’ll register a Path instance (a folder).

After we register the object, we also have to specific events the service need to observe.

Eg.:

  • StandardWatchEventKinds.ENTRY_CREATE: This occasion triggers when a folder entry is created, or a new entry is moved or renamed.
  • StandardWatchEventKinds..ENTRY_DELETE: This event is triggered when a folder/file is deleted, moved or renamed.
  • StandardWatchEventKinds.ENTRY_MODIFY: This occasion is pretty-platform dependent. Often is triggered when the contents of a file is modified. But on some file techniques it will possibly additionally set off when the attributes of that specific file are modified.
  • StandardWatchEventKinds.OVERFLOW: Indicates that an event has been misplaced.

The following code will register ENTRY_CREATE, ENTRY_DELETE and ENTRY_MODIFY to the residence folder:

For each Path occasion we’re registering we’ll obtain an WatchKey instance.

To wait for incoming occasions we will need to write an infinite loop:

In this loop we will ballot for acquiring WatchKey situations.

We will additionally add a timing interval for polling (if we don’t need an immediate feedback on the events):

A WatchKey can have the following states:

  • Ready: The WatchKey is able to accept events.
  • Signaled: On this state the WatchKey has at the very least one event that occurred and it was queued.
  • Invalid: On this state the secret is thought-about to be not valid.

So the next step shall be to retrieve the pending occasions from the WatchKey. There could be multiple events that have been triggered. Those events are queued. The code to retrieve the pending occasions is:

To obtain the Path from the watchEvent we will need to do one thing like this:

The last step of the implementation is to place key again into it’s Prepared step.

Also you also needs to absorb consideration that if the loops break (eg.: because of an error) you could explicitly shut the Watch Service:

Or, since Java 7, embrace the opening of the WatchService in the new attempt() block:


Placing all the code togheter

Please absorb consideration that in our specific case we’re solely going to observe the House folder, and not the entire sub-tree of folders. For those who want to watch the entire sub-tree for modifications you will want to register a watch service for every folder in the tree.

To obtain the listing of sub-folders of a given folder, I like to recommend you to read my earlier article referred to as: Java 7 NIO.2 – Recursive folder walks

Additionally you will have to take care of the assortment of watchers in the case you’re creating/deleting new/present sub-folders.

The Watch Service API is a low-level strategy, so perhaps it’s greatest for you in the event you write your personal high-level mechanism, or use an already-existing answer.

I will create new information/folders inside my residence listing the output will seem like: