-
-
Save Julianhm9612/7cc5cde49d984dc3f3ffd14ffa533b82 to your computer and use it in GitHub Desktop.
Watching a single file in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.IOException; | |
import java.nio.file.*; | |
public class FileWatcher { | |
private static final Logger log = LoggerFactory.getLogger(FileWatcher.class); | |
private Thread thread; | |
private WatchService watchService; | |
public interface Callback { | |
void run() throws Exception; | |
} | |
/** | |
* Starts watching a file and the given path and calls the callback when it is changed. | |
* A shutdown hook is registered to stop watching. To control this yourself, create an | |
* instance and use the start/stop methods. | |
*/ | |
public static void onFileChange(Path file, Callback callback) throws IOException { | |
FileWatcher fileWatcher = new FileWatcher(); | |
fileWatcher.start(file, callback); | |
Runtime.getRuntime().addShutdownHook(new Thread(fileWatcher::stop)); | |
} | |
public void start(Path file, Callback callback) throws IOException { | |
watchService = FileSystems.getDefault().newWatchService(); | |
Path parent = file.getParent(); | |
parent.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); | |
log.info("Going to watch " + file); | |
thread = new Thread(() -> { | |
while (true) { | |
WatchKey wk = null; | |
try { | |
wk = watchService.take(); | |
Thread.sleep(500); // give a chance for duplicate events to pile up | |
for (WatchEvent<?> event : wk.pollEvents()) { | |
Path changed = parent.resolve((Path) event.context()); | |
if (Files.exists(changed) && Files.isSameFile(changed, file)) { | |
log.info("File change event: " + changed); | |
callback.run(); | |
break; | |
} | |
} | |
} catch (InterruptedException e) { | |
log.info("Ending my watch"); | |
Thread.currentThread().interrupt(); | |
break; | |
} catch (Exception e) { | |
log.error("Error while reloading cert", e); | |
} finally { | |
if (wk != null) { | |
wk.reset(); | |
} | |
} | |
} | |
}); | |
thread.start(); | |
} | |
public void stop() { | |
thread.interrupt(); | |
try { | |
watchService.close(); | |
} catch (IOException e) { | |
log.info("Error closing watch service", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment