I have a piece of code that monitors a directory for addition of files. Whenever a new file is added to the directory, the contents of the file are picked and published on kafka
It is always a better idea to add sleep time in WatchService events:
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// now do your intended jobs ...
I needed to add sleep time otherwise it wouldn't work for multiple requests and used to get the error:
The process cannot access the file because it is being used by another process