permaculture playing cards*
The moose likes Java in General and the fly likes OS notifying file system changes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "OS notifying file system changes" Watch "OS notifying file system changes" New topic
Author

OS notifying file system changes

Santhosh Venkatesan
Greenhorn

Joined: Mar 28, 2009
Posts: 9
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


SCJP - 1.5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Mar 28, 2009
Posts: 9
Thanks guys for your quick reply. I noticed something about JNotify. Have anyone used it? I will look into JNI as well..

~Santhosh
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
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.


The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Does JNotify also poll?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
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
Ulas Ergin
Ranch Hand

Joined: Oct 10, 2002
Posts: 77
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Dude, cut the sarcasm
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Ulas Ergin
Ranch Hand

Joined: Oct 10, 2002
Posts: 77
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OS notifying file system changes