Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Automatically detect an update in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Automatically detect an update in a file" Watch "Automatically detect an update in a file" New topic
Author

Automatically detect an update in a file

Andres Delrotti
Ranch Hand

Joined: Aug 11, 2005
Posts: 136
Hi,

I just want to know what's the easiest way to do this? Let's have I have an XML file which is constantly being updated and the application needs to detect if this file is updated and transfer the updated contents of the file to a cache. My idea of doing this is running a thread which periodically checks the last update time of the file and if there is a change, then it will load the contents of the file into the cache, to update the cache. Is there a better and more efficient way of doing this? If my idea is the best idea, do you guys think spawning thread could handle this or should I stick to the more complex timing APIs such as Quartz?

Any feedback or help would be appreciated. Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

To do something periodically, one good tool is java.util.Timer. You don't have to write your own timing logic and you don't have to use a complex tool like Quartz.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41132
    
  45
I don't think Quartz would add much to using a background thread (possibly started and run by using java.util.Timer and TimerTask).

You'll need to think about how to avoid reading the file while it's being updated (or vice-versa).


Ping & DNS - my free Android networking tools app
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Ulf: You'll need to think about how to avoid reading the file while it's being updated.


Interesting point Ulf.
Is there a way to assert whether a file is being updated? (Of course by some other program/application out of our application)
This gets complex because i may have opened the file in a notepad/editor and keep saving periodically. Will it be correct to not read it for the entire period it is opened in an editor?
Is the write to a file guaranteed to be atomic by the OS? or i may get a half saved file while reading?


apigee, a better way to API!
Vilmantas Baranauskas
Ranch Hand

Joined: Dec 20, 2006
Posts: 89
One easy solution is to use "quiet" period before loading the file. You require that the file timestamp is at least X seconds old before loading the file.

This doesn't solve the problem with opening file in a text editor and saving it periodically.


Author of <a href="http://www.newsinjector.com" target="_blank" rel="nofollow">NewsInjector</a>
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Vilmantas:
One easy solution is to use "quiet" period before loading the file. You require that the file timestamp is at least X seconds old before loading the file.


What will we get by doing this? Are you saying that if the timestamp is not X seconds older, the file may still be in the process of being written? I thought timestamp will be the last thing that must be updated when updating a file. (Although this will depend on the file system implementation which i am not a guru of! but still that makes more sense to me)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Automatically detect an update in a file
 
Similar Threads
NX: When to update cached data
worker threads and file updates
Memory cache for file contents, is the right way to go ?
URLyBird: DB with 'deferred write' capabilities
dynamic update of JTable