• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Automatically detect an update in a file

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic