Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Automatically detect an update in a file

 
Andres Delrotti
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic