Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java WatchService – filesystem monitoring

farenda 2016-04-13 0

Java WatchService – filesystem monitoring

Since Java 7 there are many great APIs for working with files. In this tutorial we present WatchService that can be used to easily monitor filesystem directories for changes!

To use WatchService correctly one has to do the following things:

  1. Create a WatchService using FileSystem API.
  2. Register Watchable in the WatchService for selected event types.
    Path is Watchable, so can be registered.
  3. Wait for watch event(s).
    Each Watchable is identified by WatchKey, from which events can be taken.
  4. Process events.
    Note that we have to check the kind of event, because it may happen that too much has happened :-) and there was an overflow in events queue, which means that we need to re-check the resource. Also event.context() is the modified resource.
  5. Reset key state to Ready
    If you don’t – you won’t receive further events!

Implementing such functionality, before advent of Java 7 must have been a real pain! Now it is so simple.

The code:

In this example we’re monitoring modifications in /var/log directory:

package com.farenda.java.nio;

import java.io.IOException;
import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

public class WatchServiceExample {

    public static void main(String[] args) throws IOException {
        Path logs = Paths.get("/var/log");
        WatchServiceExample example = new WatchServiceExample();
        example.monitor(logs);
    }

    private final WatchService watcher;

    public WatchServiceExample() throws IOException {
        watcher = FileSystems.getDefault().newWatchService();
    }

    private void monitor(Path dir) throws IOException {
        // See StandardWatchEventKinds for more event types.
        dir.register(watcher, ENTRY_MODIFY);

        System.out.println("Starting monitoring of: " + dir);

        boolean monitor = true;
        while (monitor) {
            WatchKey key = waitForChange();

            if (key == null) {
                continue;
            }

            for (WatchEvent<?> event : key.pollEvents()) {
                if (event.kind() == OVERFLOW) {
                    System.err.println("Overflow event: rechecking.");
                    continue;
                }

                // Modified path is relative to parent:
                Path modifiedPath = (Path) event.context();
                System.out.printf("%s modified %d times.%n",
                        modifiedPath, event.count());

                // Reset to receive further events:
                // False means that the key became invalid.
                monitor = key.reset();
            }
        }

        System.err.printf("The key for %s is no longer valid!%n", dir);

        watcher.close();
    }

    private WatchKey waitForChange() {
        try {
            return watcher.take();
        } catch (InterruptedException e) {
            System.err.println("Error while waiting for key: "
                    + e.getMessage());
        }
        return null;
    }
}

Note that WatchService has three methods for getting watchables:

  1. poll()
    Immediately get and remove the first watchable or return null.
  2. poll(long timeout, TimeUnit unit)
    Like poll(), but wait for specified time if there are no changes.
  3. take()
    Get and remove the first watchable (wait forever if there are no changes).

Remember that key reset is very important, because without it the key won’t receive further notifications!

Sample results produced by the above program:

Starting monitoring of: /var/log
auth.log modified 1 times.
syslog modified 1 times.
auth.log modified 1 times.
auth.log modified 1 times.
auth.log modified 1 times.
auth.log modified 1 times.

You can do here any kind of processing – loading the files in GUI, sending emails, showing alerts to a user, reloading configurations, loading new classes, or whatever you can imagine! :-)

Share with the World!
Categories Java Tags java, java-io
Previous: Java 8 Find Files
Next: Java Path create

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok