Created
October 13, 2019 00:32
-
-
Save acidlychee/2e58dda2f9076ed6094c16cf50e4e4c5 to your computer and use it in GitHub Desktop.
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
| public void watchservice() | |
| { | |
| Thread fileWatcher = new Thread(() -> | |
| { | |
| Path path = Paths.get(rootDir+"InputFiles/"+dirName+"/request"); | |
| Path dataDir = Paths.get(path); | |
| try | |
| { | |
| WatchService watcher = dataDir.getFileSystem().newWatchService(); | |
| dataDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); | |
| while (true) | |
| { | |
| WatchKey watckKey; | |
| try | |
| { | |
| watckKey = watcher.take(); | |
| } | |
| catch (Exception e) | |
| { | |
| logger.error("watchService interupted:", e); | |
| return; | |
| } | |
| List<WatchEvent<?>> events = watckKey.pollEvents(); | |
| for (WatchEvent<?> event : events) | |
| { | |
| logger.debug("Event Type : "+ event.kind() +" , File name found :" + event.context()); | |
| if (event.kind() != StandardWatchEventKinds.OVERFLOW) | |
| { | |
| // do your stuff | |
| } | |
| } | |
| } | |
| } | |
| catch (Exception e) | |
| { | |
| logger.error("Error: " , e); | |
| } | |
| }); | |
| fileWatcher.setName("File-Watcher"); | |
| fileWatcher.start(); | |
| fileWatcher.setUncaughtExceptionHandler((Thread t, Throwable throwable) -> | |
| { | |
| logger.error("Error ocurred in Thread " + t, throwable); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment