Reputation: 9
I have the following problem:
A java application has to be triggered by the placement of a file in a file system. I am trying to avoid a polling solution in which the application would search in regular time intervals the file system in order to find out if a new file lies in the system.
Is there any way to trigger application by the event of placing a file in the file system?
Upvotes: 1
Views: 351
Reputation: 506
Both jnotify or using the new new java file I/O (NIO.2) package will poll kernel events and notify you of any file system changes. Both solutions are kernel hooks and will not use up resources, while still giving you what you need.
Check out http://jnotify.sourceforge.net/linux.html for JNotify or NIO.2 for more info
Upvotes: 1
Reputation: 140427
Oracle actually has a nice tutorial around this topic:
https://docs.oracle.com/javase/tutorial/essential/io/notification.html
When to Use and Not Use This API
The Watch Service API is designed for applications that need to be notified about file change events. It is well suited for any application, like an editor or IDE, that potentially has many open files and needs to ensure that the files are synchronized with the file system. It is also well suited for an application server that watches a directory, perhaps waiting for .jsp or .jar files to drop, in order to deploy them.
Upvotes: 1