This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

OS notifying file system changes

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the Operating system is the second person after the person himself to know that a file is being created/deleted/modified residing in a file system. Is there by any means the OS will notify my application when there is any change (created/deleted/modified) to my application monitoring directory/file system ? I want to update my file in the application cache.. Previously i was polling to the directory at scheduled intervals to check for changes. but that seem to be a performance issue. So i came into this.. I don want to restrict with any OS as i always wanted to be Platform independent.


If there are any other solution which you feel better, you can tell me.


~Santhosh
 
Sheriff
Posts: 22667
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santhosh Venkatesan wrote:I don want to restrict with any OS as i always wanted to be Platform independent.


Then you're out of luck, because this probably isn't just OS dependent - it's probably file system dependent. NTFS may support it, whereas FAT32 may not (to be honest, I have no idea). Either way, you will need to use JNI for this.
 
Rancher
Posts: 4253
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this is supposed to be available in JDK 7 - if you're willing to wait, what, a year or so before it's officially released. Or if you're willing to build the OpenJDK releases yourself. The latter is not a bad option for developers, but may be a problem if you want to distribute the code to customers.
 
Santhosh Venkatesan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for your quick reply. I noticed something about JNotify. Have anyone used it? I will look into JNI as well..

~Santhosh
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are on windows, then you might have to hook certain windows messages (which windows always publish whenever handling with files or doing some specific IO, you have to digg into it) and to utilize this in java .. you have to write a JNI wrapper.

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may also wish to have a look at Apache Commons VFS - I found that it supports something called FileChangeEvent with ChangedEvent, CreateEvent and DeleteEvent as subclasses - seems like it's what you need. Though I haven't used it myself
 
Rob Spoor
Sheriff
Posts: 22667
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Something like this is supposed to be available in JDK 7


That's actually pretty cool, but it still uses polling if the file system does not support notifications.

Adam Michalik wrote:You may also wish to have a look at Apache Commons VFS - I found that it supports something called FileChangeEvent with ChangedEvent, CreateEvent and DeleteEvent as subclasses - seems like it's what you need. Though I haven't used it myself


That one also polls, something Santhosh is trying to avoid.


So the end conclusion is: it's possible for some file systems in native code, for others you need to poll.
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Years ago, I wrote an MBean that does this -- monitors and triggers notification on file changes. And yes, it did do it by polling.

The point is, it consumed very little of the system resources. Barely a few percents, and most of that was probably due to running the code that got triggered.

So, my question here is... is it really an issue? Is your current code, very inefficient?

Henry
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does JNotify also poll?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are just polling the directory "lastModified" value, wouldn't that typically be cached by the operating system and thus be quite quick?

Bill
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java7 offers such facility which is available as java download.
see API docs for FileSystem,FileRef,Watchable,WatchEvent and WatchService classes.
Also you might take a look at a sample i posted some time ago.
http://ulasergin.blogspot.com/2009/03/java7-directory-listener.html
 
Mike Simmons
Rancher
Posts: 4253
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really, they have that in JDK 7? I had no idea.

Rob: yeah, it may revert to polling if there's no alternative, but that's the best you can do in the face of an impossible requirement, I think. It uses the OS facilities if they're available.

Henry's point is also good - in general, polling really isn't a problem. Unless perhaps you need to react to file changes within a few milliseconds, or unless you have a directory with a huge number of old entries that you have to filter out while looking only for the new ones. In the latter case, you probably need to modify the process to delete or move the already-processed files, so they don't gum up the works.
 
Rob Spoor
Sheriff
Posts: 22667
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Really, they have that in JDK 7? I had no idea.


Dude, cut the sarcasm
 
Mike Simmons
Rancher
Posts: 4253
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ulas Ergin
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Really, they have that in JDK 7? I had no idea..


you are in the wrong business,you should be a comedian,such funny jokes,ohhh I am dying of laughter
 
Mike Simmons
Rancher
Posts: 4253
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but I'd be happy enough if people could just post in a manner which suggests they had actually read the thread to which they were responding.
reply
    Bookmark Topic Watch Topic
  • New Topic