[JAVA] Hinweise zur Verwendung von WatchService

Beachten Sie, wie Sie mit "WatchService" Ordner- und Dateiänderungen in Java überwachen.

Umgebung

OS Window 10

Java

java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Hello World

package sample.watch;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.Watchable;

import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.WatchEvent.*;

public class Main {

    public static void main(String[] args) {
        WatchService watcher;
        try {
            watcher = FileSystems.getDefault().newWatchService();

            Watchable path = Paths.get("./build");
            path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        while (true) {
            WatchKey watchKey;
            try {
                watchKey = watcher.take();
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
                return;
            }

            for (WatchEvent<?> event : watchKey.pollEvents()) {
                Kind<?> kind = event.kind();
                Object context = event.context();
                System.out.println("kind=" + kind + ", context=" + context);
            }
            
            if (!watchKey.reset()) {
                System.out.println("WatchKey ist deaktiviert");
                return;
            }
        }
    }
}

** Ausführungsergebnis **

watchservice.gif

Erläuterung

        WatchService watcher;
        try {
            watcher = FileSystems.getDefault().newWatchService();

            ...
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
import static java.nio.file.StandardWatchEventKinds.*;

...

            Watchable path = Paths.get("./build");
            path.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        while (true) {
            WatchKey watchKey;
            try {
                watchKey = watcher.take();
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
                return;
            }

            ...
        }
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                Kind<?> kind = event.kind();
                Object context = event.context();
                System.out.println("kind=" + kind + ", context=" + context);
            }
            if (!watchKey.reset()) {
                System.out.println("WatchKey ist deaktiviert");
                return;
            }
  1. Mit WatchKey.cancel () explizit abgebrochen
  2. Das überwachte Objekt (Verzeichnis usw.) wurde gelöscht und war nicht mehr zugänglich und wurde implizit abgebrochen.
  3. WatchService.close () hat die Überwachung von WatchService abgebrochen

WatchKey-Statusänderung

watchservice.png

Beziehung zwischen WatchService und WatchKey

watchservice.jpg

--WatchService speichert den WatchKey, der beim Ausführen von Watchable.register () erstellt wurde

Zusammenfassung,

Überlaufereignis

package sample.watch;

...

public class Main {

    public static void main(String[] args) {
        ...

        while (true) {
            WatchKey watchKey;
            try {
                watchKey = watcher.take();
                Thread.sleep(1000);★ Ergänzung
            } catch (InterruptedException e) {
                ...
            }

            ...
        }
    }
}

** Ausführungsergebnis **

watchservice.gif

Erläuterung

Überwachen Sie mehrere Ordner

package sample.watch;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.Watchable;

import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.WatchEvent.*;

public class Main {

    public static void main(String[] args) {
        WatchService watcher;
        WatchKey fooKey;
        WatchKey barKey;
        try {
            watcher = FileSystems.getDefault().newWatchService();

            Watchable foo = Paths.get("./build/foo");
            fooKey = foo.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

            Watchable bar = Paths.get("./build/bar");
            barKey = bar.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        while (true) {
            WatchKey watchKey;
            try {
                watchKey = watcher.take();
            } catch (InterruptedException e) {
                System.err.println(e.getMessage());
                return;
            }

            for (WatchEvent<?> event : watchKey.pollEvents()) {
                Kind<?> kind = event.kind();
                
                if (kind == OVERFLOW) {
                    continue;
                }

                Object context = event.context();

                String directory;
                if (watchKey == fooKey) {
                    directory = "foo";
                } else if (watchKey == barKey) {
                    directory = "bar";
                } else {
                    directory = "unknown";
                }

                System.out.println("directory=" + directory + ", kind=" + kind + ", context=" + context);
            }
            
            if (!watchKey.reset()) {
                System.out.println("WatchKey ist deaktiviert");
                return;
            }
        }
    }
}

** Ausführungsergebnis **

watchservice.gif

Erläuterung

Referenz

Recommended Posts

Hinweise zur Verwendung von WatchService
Verwendungshinweise zu JavaParser
PlantUML-Nutzungsnotiz
Verwendungshinweise zu JUnit5
Hinweise zur Verwendung von Spring Shell
Spring Security-Nutzungsnotiz CSRF
Sicherheit der Verwendungsnotizmethode für Spring Security
Spring Security-Nutzungsnotiz Remember-Me
Hinweise zur Verwendung des Abhängigkeitsmanagement-Plugins
Spring Security-Nutzungsnotiz CORS
Spring Security-Verwendungsnotiztest
Spring Security-Nutzungsnotiz Authentifizierung / Autorisierung
JCA-Verwendungsprotokoll (Java Encryption Architecture)
Antwortheader für die Verwendung von Spring Security
Sitzungsverwaltung für Spring Security-Nutzungsnotizen
Spring Security-Nutzungsnotiz Basic / Mechanismus
Ganzzahliges Memo
Docker-Memo
Spring Security Usage Memo Domänenobjektsicherheit (ACL)
Lombok Memo
Dockerfile-Memo
Iterator Memo
Corretto Memo
Java-Memo
AWS-Memo
Dcokerfile Memo
irb Verwendung
Memo Stream